SlideShare a Scribd company logo
1 of 59
Download to read offline
New SQL Features
   in Firebird
Remember Firebird 2.1
●   Common SQL
    ●   COMMON TABLE EXPRESSIONS
    ●   INSERT OR UPDATE
    ●   MERGE
    ●   RETURNING
    ●   Built-in functions

●   Procedural SQL
    ●   Domains at procedures and triggers
        ● TYPE OF <domain name>

●   DDL
    ●   DATABASE TRIGGER's
        ●   CONNECT | DISCONNECT
        ●   TRANSACTION START | COMMIT | ROLLBACK
    ●   GLOBAL TEMPORARY TABLE

●   Monitoring
    ●   Monitoring tables
    ●   Ability to break execution of user query


    2               Luxembourg 2011 Whats new in Firebird SQL
Whats New in Firebird 2.5
●   Common SQL
    ●   SIMILAR TO
    ●   SQLSTATE
    ●   Hexadecimal constants
    ●   UUID binary tofrom char conversions

●   Procedural SQL
    ●   AUTONOMOUS TRANSACTIONS
    ●   EXECUTE STATEMENT
    ●   TYPE OF COLUMN

●   DDL
    ●   ALTER VIEW
    ●   ALTER computed fields
    ●   CREATEALTERDROP user
    ●   ALTER ROLE
    ●   GRANTED BY

●   Monitoring
    ●   New MONITORING TABLES
    ●   Terminate user connection

    3              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : SIMILAR TO
     Regular expressions support per SQL standard specification

New SIMILAR TO predicate
     More powerful version of LIKE with regexp syntax

Example : is given string represents a valid number ?

     Value SIMILAR TO '[+-]?[0-9]*([0-9].|.[0-9])?[[:DIGIT:]]*'
               ESCAPE ''

     [+-]                 + or -
     ?                      0 or 1 times
     [0-9]                  any digit
     *                      0 or more times
     ([0-9].|.[0-9])        <digit and point> or <point and digit>
     ?                      0 or 1 times
     [[:DIGIT:]]            any digit
     *                      0 or more times


 4             Luxembourg 2011 Whats new in Firebird SQL
Common SQL : SQLSTATE
SQLSTATE is standard compliant generic error code for use by generic applications

SQLCODE is deprecated (but still supported) and it is recommended to use SQLSTATE

To obtain SQLSTATE value use new API function : fb_sqlstate

SQLSTATE is not available (yet) for WHEN block of PSQL exception handling

isql since v2.5 used SQLSTATE in error messages :

FB25>isql
SQL> connect 'not_exists';
Statement failed, SQLSTATE = 08001
I/O error during "CreateFile (open)" operation for file "not_exists"
-Error while trying to open file
-The system cannot find the file specified.
FB21>isql
SQL> connect 'not_exists';
Statement failed, SQLCODE = -902
I/O error for file "...not_exists"
-Error while trying to open file
-The system cannot find the file specified.

 5              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : HEX Literals
              Hexadecimal numeric and binary string literals
Numeric literals : 0xHHHHHHHH
Prefix 0x or 0X
Up to 16 hexadecimal digits
1 - 8 digits : data type is - signed integer
9 - 16 digits : data type is - signed bigint

     SQL> SELECT 0xF0000000, 0x0F0000000 FROM RDB$DATABASE;

         CONSTANT              CONSTANT
     ============ =====================
       -268435456            4026531840




 6                Luxembourg 2011 Whats new in Firebird SQL
Common SQL : HEX literals
                Hexadecimal numeric and binary string literals
String literals : x'HH...H'
Prefix x or X
Data type - CHAR(N / 2) CHARACTER SET OCTETS, where N – number of digits

     SQL> SELECT 'First line' || _ASCII x'0D0A09' || 'Second line'
     CON> FROM RDB$DATABASE;

     CONCATENATION
     ========================
     First line
             Second line




 7               Luxembourg 2011 Whats new in Firebird SQL
Common SQL : UUID <-> CHAR
                     UUID binary tofrom CHAR conversion

CHAR_TO_UUID

     Converts the CHAR(32) ASCII representation of an UUID
          (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX)
     to the binary (CHAR(16) OCTETS) representation (optimized for storage)


SQL> SELECT CHAR_TO_UUID('A96B285B-4629-45A1-9A86-A8ECCF6561F4')
         FROM RDB$DATABASE;
CHAR_TO_UUID
================================
A96B285B462945A19A86A8ECCF6561F4




 8              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : UUID <-> CHAR
                    UUID binary tofrom CHAR conversion

UUID_TO_CHAR

     Converts a binary (CHAR(16) OCTETS) UUID to the string (CHAR(32) ASCII) representation


SQL> SELECT UUID_TO_CHAR(x'A96B285B462945A19A86A8ECCF6561F4')
        FROM RDB$DATABASE;
UUID_TO_CHAR
====================================
A96B285B-4629-45A1-9A86-A8ECCF6561F4




 9              Luxembourg 2011 Whats new in Firebird SQL
PSQL : Autonomous Transactions
Syntax
     IN AUTONOMOUS TRANSACTION DO
         <simple statement | compound statement>

Parameters ?
     Same as outer transaction (isolation level, readwrite, wait mode, etc)
     Configurable ? Not yet... may be later

How it ends ?
     if (statement executed ok)
     then commit
     else rollback

Notes
     Autonomous transaction and its outer transaction fully independent and isolated from each
     other as any other two transactions.

     Temporary BLOBs, created in autonomous transaction, attached to outer transaction. This is
     done to allow usage of such blobs after autonomous transaction ends. This behavior may
     be a source of unexpected additional memory consumption.



10               Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
●   New implementation of EXECUTE STATEMENT :
    ●    Input parameters
    ●    May run with privileges of caller PSQL object
    ●    Autonomous transactions
    ●    Query another Firebird database
    ●    Full backward compatibility

●   Syntax

     [FOR] EXECUTE STATEMENT <query_text> [(<input_parameters>)]
       [ON EXTERNAL [DATA SOURCE] <connection_string>]
       [WITH AUTONOMOUS | COMMON TRANSACTION]
       [AS USER <user_name>]
       [PASSWORD <password>]
       [ROLE <role_name>]
       [WITH CALLER PRIVILEGES]
       [INTO <variables>]



    11              Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
                         Input parameters

Named input parameters

     EXECUTE STATEMENT
        ('INSERT INTO TABLE VALUES (:a, :b, :a)')
        (a := 100, b := CURRENT_CONNECTION)


Not named input parameters

     EXECUTE STATEMENT
        ('INSERT INTO TABLE VALUES (?, ?, ?)')
        (100, CURRENT_CONNECTION, 100)




12           Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
                              Caller privileges
-- logon as SYSDBA
CREATE TABLE A (ID INT);
CREATE USER VLAD PASSWORD 'vlad';

CREATE PROCEDURE P1 RETURNS (CNT INT)
AS
BEGIN
   EXECUTE STATEMENT 'SELECT COUNT(*) FROM A' INTO :CNT;
   SUSPEND;
END;

GRANT SELECT ON TABLE A TO PROCEDURE P1;
GRANT EXECUTE ON PROCEDURE P1 TO USER VLAD;

-- logon as VLAD
SELECT * FROM P1;
Statement failed, SQLSTATE = 42000
Execute statement error at jrd8_prepare :
335544352 : no permission for read/select access to TABLE A
Statement : SELECT COUNT(*) FROM A
Data source : Internal::
-At procedure 'P1'


 13            Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
                                Caller privileges
-- logon as SYSDBA
CREATE PROCEDURE P2 RETURNS (CNT INT)
AS
BEGIN
   EXECUTE STATEMENT 'SELECT COUNT(*) FROM A'
     WITH CALLER PRIVILEGES
     INTO :CNT;
   SUSPEND;
END;

GRANT SELECT ON TABLE A TO PROCEDURE P2;
GRANT EXECUTE ON PROCEDURE P2 TO USER VLAD;

-- logon as VLAD
SELECT * FROM P2;

         CNT
============
           0

Dynamic statement is executed with that set of privileges as it would have
if its executed immediately by caller PSQL object (procedure or trigger)


 14             Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
                                  Transactions
Common transaction (default)

     statement executed in the current transaction

     EXECUTE STATEMENT '...'
         WITH COMMON TRANSACTION


Autonomous transaction

     statement executed in the separate new transaction

     EXECUTE STATEMENT '...'
         WITH AUTONOMOUS TRANSACTION
     parameters the same as current trancation, not (yet) configurable




15             Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXECUTE STATEMENT
                      Query another Firebird database
EXTERNAL DATA SOURCE clause :
     <connection_string> is usual Firebird's connection string

Query another database using userpassword
      EXECUTE STATEMENT '...'
          ON EXTERNAL DATA SOURCE 'host:path'
          USER 'VLAD' PASSWORD 'dontKnow'

When userpassword is not set
  Trusted autentication (Windows only) : Firebird's process account name is effective user
  name at remote database (if TA is supported by remote side)
      EXECUTE STATEMENT '...'
          ON EXTERNAL DATA SOURCE 'host:path'
  CURRENT_USER is effective user name at local database
      EXECUTE STATEMENT '...'
          ON EXTERNAL DATA SOURCE 'path_to_the_current_database'


16              Luxembourg 2011 Whats new in Firebird SQL
PSQL : TYPE OF COLUMN
Syntax
     data_type ::= <builtin_data_type>
          | <domain_name>
          | TYPE OF <domain_name>
          | TYPE OF COLUMN <table_name>.<column_name>

Usage
     ●   Input and output parameters
     CREATE PROCEDURE
       MY_PROC (IN_PARAM TYPE OF COLUMN <table_name>.<column_name>)
       RETURNS (OUT_PARAM TYPE OF COLUMN <table_name>.<column_name>)
     ●   Variable declaration
         DECLARE VARIABLE VAR1 TYPE OF COLUMN <table_name>.<column_name>
     ●   CAST statement
         OUT_PARAM = CAST(VAR1 AS TYPE OF COLUMN <table_name>.<column_name>)


17               Luxembourg 2011 Whats new in Firebird SQL
ALTER VIEW  COMPUTED FIELD
Syntax
     {CREATE OR ALTER | ALTER} VIEW <view_name> [(<field list>)]
        AS <select statement>
     ALTER TABLE <table_name>
       ALTER <field_name> [TYPE <data type>] COMPUTED BY (<expression>)

Task
    ●    change view (or computed field) definition
●   Before Firebird 2.5
    ●    drop and create (or alter two times) again all dependent objects
    ●    restore all privileges granted to all dependent objects which was re-created
●   Firebird 2.5
    ●    just execute ALTER VIEW and enjoy :-)



    18             Luxembourg 2011 Whats new in Firebird SQL
ALTER VIEW  COMPUTED FIELD
Example
      CREATE TABLE T (NAME CHAR(20), CF COMPUTED BY (LEFT(NAME, 10)) );
      CREATE VIEW V AS SELECT CF FROM T;
      CREATE PROCEDURE P ... SELECT ... FROM V ...; -- dependent procedure

                     Try to change both view and computed field definitions

Before Firebird 2.5 :
      ALTER PROCEDURE P AS BEGIN END;      -- empty body for ALL dependent objects
      DROP VIEW V;
      ALTER TABLE T DROP CF;
      ALTER TABLE T
          ADD CF COMPUTED BY (SUBSTRING(NAME FOR 15));
      CREATE VIEW V AS
          SELECT NAME, CF FROM T;
      GRANT SELECT ON TABLE T TO VIEW V;                -- restore ALL dependent
      ALTER PROCEDURE P ... SELECT ... FROM V ...; -- objects and privileges
                      What if you have hundreds dependent objects ?

 19             Luxembourg 2011 Whats new in Firebird SQL
ALTER VIEW  COMPUTED FIELD
Example
      CREATE TABLE T (NAME CHAR(20), CF COMPUTED BY (LEFT(NAME, 10)) );
      CREATE VIEW V AS SELECT CF FROM T;
      CREATE PROCEDURE P ... SELECT ... FROM V ...; -- dependent procedure

                 Try to change both view and computed field definitions

Firebird 2.5 :
      ALTER TABLE T
          ALTER CF COMPUTED BY (RIGHT(NAME, 10));
      ALTER VIEW V AS
          SELECT NAME, CF FROM T;




 20               Luxembourg 2011 Whats new in Firebird SQL
DDL : USER MANAGEMENT
Syntax

     CREATE USER name PASSWORD 'password'
         [FIRSTNAME 'firstname']
         [MIDDLENAME 'middlename']
         [LASTNAME 'lastname']

     ALTER USER name PASSWORD 'password'
         [FIRSTNAME 'firstname']
         [MIDDLENAME 'middlename']
         [LASTNAME 'lastname']

     DROP USER name




21            Luxembourg 2011 Whats new in Firebird SQL
DDL : SYSDBA and SYSADMIN's
When Windows Administrator is connected using trusted authentication :

Firebird 2.1
     CURRENT_USER is a SYSDBA
     SYSDBA privileges

Firebird 2.5
     CURRENT_USER is always a DomainUser

     Auto-admin mapping (per database)
         ALTER ROLE RDB$ADMIN SET|DROP AUTO ADMIN MAPPING;

             Auto-admin mapping             ROLE        Privileges
                     ON             NONE or RDB$ADMIN    SYSDBA
                     OFF            NONE or RDB$ADMIN
                     ON                                  ordinary
                                            Other
                     OFF


22              Luxembourg 2011 Whats new in Firebird SQL
MONITORING TABLES
 MON$DATABASE                           Databases and objects
                                        Databases and objects
 MON$DATABASE_NAME
 ...
                                                          New !
     MON$ATTACHMENTS                                     ODS 11.2
     MON$ATTACHMENT_ID
     MON$ATTACHMENT_NAME            MON$CONTEXT_VARIABLES
     ...
                                    MON$ATTACHMENT_ID
                                    MON$TRANSACTION_ID
       MON$TRANSACTIONS             MON$VARIABLE_NAME
                                    MON$VARIABLE_VALUE
       MON$TRANSACTION_ID
       MON$ATTACHMENT_ID
       ...
                                      MON$CALL_STACK
             MON$STATEMENTS           MON$CALL_ID
             MON$STATEMENT_ID         MON$STATEMENT_ID
                                      MON$CALLER_ID
             MON$ATTACHMENT_ID
                                      ...
             MON$TRANSACTION_ID
             ...

23          Luxembourg 2011 Whats new in Firebird SQL
MONITORING TABLES
                                         Objects and statistics
                                         Objects and statistics
 MON$DATABASE
 ...
 MON$STAT_ID                               MON$IO_STATS
                                          MON$STAT_ID
 MON$ATTACHMENTS                          MON$STAT_GROUP
 ...                                      ...
 MON$STAT_ID

 MON$TRANSACTIONS                         MON$RECORD_STATS
 ...
 MON$STAT_ID                              MON$STAT_ID
                                          MON$STAT_GROUP
 MON$STATEMENTS                           ...
 ...
 MON$STAT_ID
                                          MON$MEMORY_USAGE
 MON$CALL_STACK
 ...                                      MON$STAT_ID
 MON$STAT_ID                              MON$STAT_GROUP
                               New !      ...
                              ODS 11.2

24         Luxembourg 2011 Whats new in Firebird SQL
MONITORING TABLES
                          Some monitoring tables is “active”
1) Cancel current activity of connection № 32:
      DELETE FROM MON$STATEMENTS
        WHERE MON$ATTACHMENT_ID = 32


2) Cancel all queries running more than 20 minutes:
      DELETE FROM MON$STATEMENTS
        WHERE DATEADD(20 MINUTE TO MON$TIMESTAMP) < CURRENT_TIMESTAMP
          AND MON$STATE <> 0;


3) Disconnect everybody but ourselves (new in Firebird 2.5):
      DELETE FROM MON$ATTACHMENTS
        WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION




 25             Luxembourg 2011 Whats new in Firebird SQL
What's next ?




26   Luxembourg 2011 Whats new in Firebird SQL
Whats new in Firebird 3
●   Common SQL
    ●   Full syntax of MERGE (per SQL 2008)
        ●   support for DELETE substatement
        ●   additional condition in WHEN clause
        ●   multiply WHEN clauses
    ●   MERGE ... RETURNING
    ●   Window (analytical) functions
    ●   SUBSTRING with regular expressions
    ●   BOOLEAN data type
    ●   Cursor stability with data modification queries
    ●   Global temporary tables are improved




27                  Luxembourg 2011 Whats new in Firebird SQL
Whats new in Firebird 3
●   Procedural SQL
    ●   SQL functions
    ●   Sub-routines
    ●   External functions, procedures and triggers on CC++PascalJava etc.
    ●   Packages
    ●   Exceptions with parameters : EXCEPTION ... USING (...)
    ●   SQLSTATE in WHEN handler
    ●   CONTINUE in loops




28                 Luxembourg 2011 Whats new in Firebird SQL
Whats new in Firebird 3
●   DDL
    ●   Manage nullability of column
        ● ALTER DOMAIN ... {NULL | NOT NULL}

        ● ALTER COLUMN ... {NULL | NOT NULL}



    ●   ALTER DATABASE ... SET DEFAULT CHARACTER SET
    ●   IDENTITY columns
         (hello, migrants from MSSQLMySQL ;)
    ●   RECREATE SEQUENCE, RECREATE GENERATOR
    ●   DDL triggers




29                Luxembourg 2011 Whats new in Firebird SQL
Whats new in Firebird 3
●   More complete system of SQL grants
    ●   GRANT CREATE | ALTER | DROP <object> TO <user> | <role>
    ●   GRANT ROLE TO ROLE

●   Monitoring
    ●   Extended statistics, queries plan's, ...

●   To be continued :-)




30                 Luxembourg 2011 Whats new in Firebird SQL
Common SQL : MERGE
Full SQL 2008 syntax
MERGE INTO <table>
     USING <table_or_join>
           ON <search_condition>

     [WHEN MATCHED [AND <search_condition>] THEN
           UPDATE SET col1 = val1, ..., colN = valN
       |
           DELETE]

     [WHEN NOT MATCHED [AND <search_condition>] THEN
           INSERT [(col1, ..., colN)] VALUES (val1, ..., valN)]




31              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : MERGE
DELETE substatement and multiply WHEN clauses
MERGE INTO TABLE
     USING LOG
        ON TABLE.PK = LOG.PK

     WHEN MATCHED AND LOG.ACTION = 'D' THEN
        DELETE

     WHEN MATCHED THEN               -- second WHEN MATCHED clause
        UPDATE SET col1 = LOG.val1, ...

     WHEN NOT MATCHED THEN
        INSERT (col1, ...) VALUES (LOG.val1, ...)




32           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : MERGE
RETURNING clause
MERGE INTO <table>
     USING <table_or_join>
           ON <search_condition>

     [WHEN MATCHED [AND <search_condition>] THEN
           UPDATE SET col1 = val1, ..., colN = valN
       |
           DELETE]

     [WHEN NOT MATCHED [AND <search_condition>] THEN
           INSERT [(col1, ..., colN)] VALUES (val1, ..., valN)]

     [RETURNING ... [INTO ...]]




33              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : WINDOW FUNCTIONS
Syntax
<window function> ::=
     <window function type> OVER (<window specification>)

<window function type> ::=
     <aggregate function>            –- aggregate
 | <rank function type>              –- ranking
 | ROW_NUMBER                        –- yes, it is row number ;)
 | <lead or lag function>            –- navigational
 | <first or last value function>
 | <nth value function>

<window specification> ::=
     [PARTITION BY column1, ...]
     [ORDER BY column1 [ASC|DESC] [NULLS {FIRST|LAST}], ... ]

34           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : WINDOW FUNCTIONS
Syntax
<aggregate function> ::=
     AVG | MAX | MIN | SUM | COUNT | LIST

<rank function type> ::=
     RANK | DENSE_RANK

<lead or lag function> ::=
     LEAD | LAG

<first or last value function> ::=
     {FIRST_VALUE | LAST_VALUE} (<value>)

<nth value function> ::=
     NTH_VALUE (<value>, <nth row>)



35           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : WINDOW FUNCTIONS
Example

     SELECT A,   B, C,
        SUM(C)   OVER(),
        SUM(C)   OVER(ORDER BY A, B),
        SUM(C)   OVER(PARTITION BY A),
        SUM(C)   OVER(PARTITION BY A ORDER BY B)

            A         B      C     SUM    SUM1     SUM2   SUM3
            1         1      30    141      30      60     30
            1         2      20    141      50      60     50
            1         3      10    141      60      60     60
            2         1      25    141      85      40     25
            2         2      15    141     100      40     40
            3         1      41    141     141      41     41



36          Luxembourg 2011 Whats new in Firebird SQL
Common SQL : substring search using REGEXP

Syntax
     SUBSTRING(<string> SIMILAR <pattern> ESCAPE <char>)

Rules
 ●   Pattern
     ● R = <R1> <E> " <R2> <E> " <R3>


 ●   Search
     ● S = <S1> <S2> <S3>


         1)<S>       SIMILAR TO <R1> <R2> <R3> ESCAPE <E>
         2)<S1>     SIMILAR TO <R1>        ESCAPE <E> AND
          <S2> <S3> SIMILAR TO <R2> <R3>   ESCAPE <E>
         3)<S2>      SIMILAR TO <R2>       ESCAPE <E> AND
          <S3>       SIMILAR TO <R3>       ESCAPE <E>
 ●   Result
     ●   S2


37                Luxembourg 2011 Whats new in Firebird SQL
Common SQL : substring search using REGEXP

Syntax
     SUBSTRING(<string> SIMILAR <pattern> ESCAPE <char>)

Example
     SUBSTRING('abc-12b34xyz' SIMILAR '%"[+-]?[0-9]+"%' ESCAPE '')
     R1 = %
     R2 = [+-]?[0-9]+
     R3 = %
     1)   'abc-12b34xyz' SIMILAR TO '%[+-]?[0-9]+%' ESCAPE ''
     2)   'abc'           SIMILAR TO '%'                ESCAPE '' AND
          '-12b34xyz'     SIMILAR TO '[+-]?[0-9]+%'   ESCAPE ''
     3)   '-12'           SIMILAR TO '[+-]?[0-9]+'    ESCAPE '' AND
          'b34xyz'        SIMILAR TO '%'                ESCAPE ''

Result
   '-12'


38             Luxembourg 2011 Whats new in Firebird SQL
Common SQL : BOOLEAN data type

Syntax
     <data_type> ::= BOOLEAN
     <boolean_literal> ::= TRUE | FALSE | UNKNOWN


Storage
     1 byte


Client support (XSQLDA)
     #define SQL_BOOLEAN 32764




39            Luxembourg 2011 Whats new in Firebird SQL
Common SQL : BOOLEAN data type

Truth tables

     AND      True     False   Unknown     OR       True      False    Unknown
     True     True     False   Unknown    True      True      True      True
     False    False    False    False     False     True      False    Unknown
Unknown      Unknown   False   Unknown   Unknown    True     Unknown   Unknown



      IS      True     False   Unknown    NOT
     True     True     False    False     True      False
     False    False    True     False     False     True
Unknown       False    False    True     Unknown   Unknown




40             Luxembourg 2011 Whats new in Firebird SQL
Common SQL : BOOLEAN data type

Examples
CREATE TABLE TBOOL (ID INT, BVAL BOOLEAN);
COMMIT;

INSERT INTO TBOOL VALUES (1, TRUE);
INSERT INTO TBOOL VALUES (2, 2 = 4);
INSERT INTO TBOOL VALUES (3, NULL = 1);
COMMIT;

  SELECT * FROM TBOOL

            ID      BVAL
  ============   =======
             1   <true>
             2   <false>
             3   <null>




41           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : BOOLEAN data type

Examples
1. Test for TRUE value             3. Tests for UNKNOWN value
     SELECT * FROM TBOOL               SELECT * FROM TBOOL
      WHERE BVAL                        WHERE BVAL IS UNKNOWN

               ID    BVAL                       ID    BVAL
     ============ =======             ============ =======
                1 <true>                         3 <null>

2. Test for FALSE value
     SELECT * FROM TBOOL              SELECT * FROM TBOOL
      WHERE BVAL IS FALSE              WHERE BVAL = UNKNOWN

               ID    BVAL
     ============ =======             SELECT * FROM TBOOL
                2 <false>              WHERE BVAL <> UNKNOWN




42           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : BOOLEAN data type

Examples
4. Boolean values in SELECT list
     SELECT ID, BVAL, BVAL AND ID < 2
       FROM TBOOL

               ID      BVAL
     ============   =======   =======
                1   <true>    <true>
                2   <false>   <false>
                3   <null>    <false>




43           Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

The issue
 ●   Famous infinite insertion circle (CORE-92)
      INSERT INTO T
      SELECT * FROM T

 ●   DELETE more rows than expected (CORE-634)
      DELETE FROM T
        WHERE ID IN (SELECT FIRST 1 ID FROM T)

 ●   All DML statements is affected (INSERT, UPDATE, DELETE, MERGE)

 ●   Common ticket at tracker CORE-3362




44             Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

The reason of the issue
 ●   DML statements used implicit cursors
     ●   INSERT INTO T SELECT ... FROM T
         works as
          FOR SELECT <values> FROM T INTO <tmp_vars>
            DO INSERT INTO T VALUES (<tmp_vars>)

     ●   UPDATE T SET <fields> = <values> WHERE <conditions>
         works as
          FOR SELECT <values> FROM T WHERE <conditions> INTO <tmp_vars>
              AS CURSOR <cursor>
          DO UPDATE T SET <fields> = <tmp_vars>
                 WHERE CURRENT OF <cursor>

     ●   DELETE works like UPDATE




45              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

The “standard” way
 ●   Rows to be insertedupdateddeleted should be marked first
 ●   Marked rows is insertedupdateddeleted then
 ●   Pros
     ●   rowset is stable and is not affected by DML statement itself
 ●   Cons
     ●   Marks should be saved somewhere and rows will be visited again, or
     ●   Set of marked rows should be saved somewhere and this store will
         be visited again

 ●   Note : this could be reached in Firebird using (well known) workaround:
         force query to have SORT in PLAN - it will materialize implicit cursor and
         make it stable



46               Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

The Firebird 3 way
 ●   Use undo-log to see if record was already modified by current cursor
     ●   if record was inserted - ignore it
     ●   if record was updated or deleted - read backversion
 ●   Pros
     ●   No additional bookkeeping required
     ●   No additional storage required
     ●   Relatively easy to implement
 ●   Cons
     ●   Inserted records could be visited (but ignored, of course)
     ●   Backversions of updateddeleted records should be read
     ●   Not works with SUSPEND in PSQL



47                Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

PSQL notes
 ●   PSQL cursors with SUSPEND inside still not stable !

      This query still produced infinite circle

      FOR SELECT ID FROM T INTO :ID
         DO BEGIN
           INSERT INTO T (ID) VALUES (:ID);
           SUSPEND;
         END




48             Luxembourg 2011 Whats new in Firebird SQL
Common SQL : cursor stability

PSQL notes
 ●   PSQL cursors without SUSPEND inside is stable now, this could change old
     behavior


      FOR SELECT ID FROM T WHERE VAL IS NULL INTO :ID
         DO BEGIN
           UPDATE T SET VAL = 1
            WHERE ID = :ID;
         END




49              Luxembourg 2011 Whats new in Firebird SQL
Common SQL : improvements in GTT

●    Global temporary tables is writable even in read-only transactions
     ●   Read-only transaction in read-write database
         ●   Both ON COMMIT PRESERVE ROWS and ON COMMIT DELETE ROWS
     ●   Read-only transaction in read-only database
         ●   GTT ON COMMIT DELETE ROWS only

●    Faster rollback for GTT ON COMMIT DELETE ROWS
     ●   No need to backout records on rollback

●    Garbage collection in GTT is not delayed by active transactions of another
     connections

●    All this improvements is backported into v2.5.1 too




50                Luxembourg 2011 Whats new in Firebird SQL
PSQL : SQL functions

Syntax
     {CREATE [OR ALTER] | ALTER | RECREATE} FUNCTION <name>
         [(param1 [, ...])]
         RETURNS <type>
     AS
     BEGIN
         ...
     END

Example
     CREATE FUNCTION F(X INT) RETURNS INT
     AS
     BEGIN
        RETURN X+1;
     END;

     SELECT F(5) FROM RDB$DATABASE;



51           Luxembourg 2011 Whats new in Firebird SQL
PSQL : SQL sub-routines

Syntax
 ●   Sub-procedures
        DECLARE PROCEDURE <name> [(param1 [, ...])]
            [RETURNS (param1 [, ...])]
        AS
        ...

 ●   Sub-functions
        DECLARE FUNCTION <name> [(param1 [, ...])]
            RETURNS <type>
        AS
        ...




52            Luxembourg 2011 Whats new in Firebird SQL
PSQL : SQL sub-routines

Example
     EXECUTE BLOCK RETURNS (N INT)
     AS
       DECLARE FUNCTION F(X INT) RETURNS INT
       AS
       BEGIN
        RETURN X+1;
       END;
     BEGIN
       N = F(5);
       SUSPEND;
     END




53           Luxembourg 2011 Whats new in Firebird SQL
PSQL : Packages

-– package header, declarations only
CREATE OR ALTER PACKAGE TEST
AS
BEGIN
  PROCEDURE P1(I INT) RETURNS (O INT);   -– public procedure
END
-– package body, implementation
RECREATE PACKAGE BODY TEST
AS
BEGIN
  FUNCTION F1(I INT) RETURNS INT;   –- private function

 PROCEDURE P1(I INT) RETURNS (O INT)
 AS
 BEGIN
 END;

  FUNCTION F1(I INT) RETURNS INT
  AS
  BEGIN
     RETURN 0;
  END;
END

54           Luxembourg 2011 Whats new in Firebird SQL
DDL : IDENTITY columns
Syntax
<column definition> ::=
     <name> <type> GENERATED BY DEFAULT AS IDENTITY <constraints>


Rules
 ●   Column type – INTEGER or NUMERIC(P, 0)
 ●   Implicit NOT NULL
 ●   Not guarantees uniqueness
 ●   Can not have DEFAULT clause
 ●   Can not be COMPUTED BY




55            Luxembourg 2011 Whats new in Firebird SQL
DDL : DDL triggers
Syntax
<ddl-trigger> ::=
     {CREATE | RECREATE | CREATE OR ALTER} TRIGGER <name>
        [ACTIVE | INACTIVE] {BEFORE | AFTER} <ddl event>
        [POSITION <n>]

<ddl event> ::=
  ANY DDL STATEMENT
| <ddl event item> [{OR <ddl event item>}...]

<ddl event item> ::=
     {CREATE | ALTER | DROP}
     {TABLE | PROCEDURE | FUNCTION | TRIGGER | EXCEPTION |
      VIEW | DOMAIN     | SEQUENCE | INDEX   | ROLE      |
      USER | COLLATION | PACKAGE | PACKAGE BODY          |
      CHARACTER SET }



56           Luxembourg 2011 Whats new in Firebird SQL
DDL : DDL triggers
New context variables (RDB$GET_CONTEXT)
 ●   Namespace DDL_TRIGGER
 ●   Defined inside DDL trigger only
 ●   Read only
 ●   Predefined variables:
     ●   DDL_EVENT     - kind of DDL event
     ●   OBJECT_NAME – name of metadata object
     ●   SQL_TEXT      - text of SQL query




57               Luxembourg 2011 Whats new in Firebird SQL
PSQL : EXCEPTION with parameters

Syntax
     EXEPTION <name> USING (param1 [, ...]);


Example
     CREATE EXCEPTION EX_BAD_SP_NAME
        'Name of procedures must start with ''@1'' : ''@2''';

     CREATE TRIGGER TRG_SP_CREATE BEFORE CREATE PROCEDURE
     AS
     DECLARE SP_NAME VARCHAR(255);
     BEGIN
        SP_NAME = RDB$GET_CONTEXT('DDL_TRIGGER', 'OBJECT_NAME');

       IF (SP_NAME NOT STARTING 'SP_')
       THEN EXCEPTION EX_BAD_SP_NAME USING ('SP_', SP_NAME);
     END;



58           Luxembourg 2011 Whats new in Firebird SQL
THANK YOU FOR ATTENTION



             Questions ?


     Firebird official web site

                        Firebird tracker

                                     hvlad@users.sf.net

59   Luxembourg 2011 Whats new in Firebird SQL

More Related Content

What's hot

Data Modeling PPT
Data Modeling PPTData Modeling PPT
Data Modeling PPTTrinath
 
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...Michael Herzog
 
Data encryption in database management system
Data encryption in database management systemData encryption in database management system
Data encryption in database management systemRabin BK
 
The Big Data Stack
The Big Data StackThe Big Data Stack
The Big Data StackZubair Nabi
 
Security technologies
Security technologiesSecurity technologies
Security technologiesDhani Ahmad
 
Transport Layer Security (TLS)
Transport Layer Security (TLS)Transport Layer Security (TLS)
Transport Layer Security (TLS)Arun Shukla
 
Primary Key & Foreign Key part10
Primary Key & Foreign Key part10Primary Key & Foreign Key part10
Primary Key & Foreign Key part10DrMohammed Qassim
 
Application layer security protocol
Application layer security protocolApplication layer security protocol
Application layer security protocolKirti Ahirrao
 
Association rule mining
Association rule miningAssociation rule mining
Association rule miningAcad
 
Data warehouse
Data warehouseData warehouse
Data warehouseRajThakuri
 
Acik Anahtarli Kripto Sistemler
Acik Anahtarli Kripto SistemlerAcik Anahtarli Kripto Sistemler
Acik Anahtarli Kripto SistemlerMustafa GOCMEN
 
End to End Encryption in 10 minutes -
End to End Encryption in 10 minutes - End to End Encryption in 10 minutes -
End to End Encryption in 10 minutes - Thomas Seropian
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining Sulman Ahmed
 
Security and Integrity of Data
Security and Integrity of DataSecurity and Integrity of Data
Security and Integrity of DataAdeel Riaz
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationGuru Ji
 

What's hot (20)

SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Data Modeling PPT
Data Modeling PPTData Modeling PPT
Data Modeling PPT
 
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...
PURL and vers: The Mostly Universal Package URL and Version Ranges Identifier...
 
Data encryption in database management system
Data encryption in database management systemData encryption in database management system
Data encryption in database management system
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
 
SQL - מודל הנתונים
SQL - מודל הנתוניםSQL - מודל הנתונים
SQL - מודל הנתונים
 
The Big Data Stack
The Big Data StackThe Big Data Stack
The Big Data Stack
 
Security technologies
Security technologiesSecurity technologies
Security technologies
 
Transport Layer Security (TLS)
Transport Layer Security (TLS)Transport Layer Security (TLS)
Transport Layer Security (TLS)
 
Primary Key & Foreign Key part10
Primary Key & Foreign Key part10Primary Key & Foreign Key part10
Primary Key & Foreign Key part10
 
Application layer security protocol
Application layer security protocolApplication layer security protocol
Application layer security protocol
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
Data warehouse
Data warehouseData warehouse
Data warehouse
 
Database Normalization by Dr. Kamal Gulati
Database Normalization by Dr. Kamal GulatiDatabase Normalization by Dr. Kamal Gulati
Database Normalization by Dr. Kamal Gulati
 
Acik Anahtarli Kripto Sistemler
Acik Anahtarli Kripto SistemlerAcik Anahtarli Kripto Sistemler
Acik Anahtarli Kripto Sistemler
 
End to End Encryption in 10 minutes -
End to End Encryption in 10 minutes - End to End Encryption in 10 minutes -
End to End Encryption in 10 minutes -
 
Classification in data mining
Classification in data mining Classification in data mining
Classification in data mining
 
Security and Integrity of Data
Security and Integrity of DataSecurity and Integrity of Data
Security and Integrity of Data
 
CBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL PresentationCBSE XII Database Concepts And MySQL Presentation
CBSE XII Database Concepts And MySQL Presentation
 

Similar to New features of SQL in Firebird

New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunMind The Firebird
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesKeshav Murthy
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Alex Zaballa
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsAlex Zaballa
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Ziaur Rahman
 
12c Database new features
12c Database new features12c Database new features
12c Database new featuresSandeep Redkar
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programmingRushdi Shams
 
Distributed Queries in IDS: New features.
Distributed Queries in IDS: New features.Distributed Queries in IDS: New features.
Distributed Queries in IDS: New features.Keshav Murthy
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1MariaDB plc
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Netwebhostingguy
 
War of the Indices- SQL Server and Oracle
War of the Indices-  SQL Server and OracleWar of the Indices-  SQL Server and Oracle
War of the Indices- SQL Server and OracleKellyn Pot'Vin-Gorman
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...Jürgen Ambrosi
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLJerry Yang
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Proceduressqlserver content
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Proceduressqlserver content
 
Apache Sqoop: A Data Transfer Tool for Hadoop
Apache Sqoop: A Data Transfer Tool for HadoopApache Sqoop: A Data Transfer Tool for Hadoop
Apache Sqoop: A Data Transfer Tool for HadoopCloudera, Inc.
 

Similar to New features of SQL in Firebird (20)

New SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad KhorsunNew SQL Features in Firebird 3, by Vlad Khorsun
New SQL Features in Firebird 3, by Vlad Khorsun
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql Features
 
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
Oracle Database 12c - The Best Oracle Database 12c Tuning Features for Develo...
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012Developers' New features of Sql server express 2012
Developers' New features of Sql server express 2012
 
12c Database new features
12c Database new features12c Database new features
12c Database new features
 
L9 l10 server side programming
L9 l10  server side programmingL9 l10  server side programming
L9 l10 server side programming
 
Unit 3
Unit 3Unit 3
Unit 3
 
Distributed Queries in IDS: New features.
Distributed Queries in IDS: New features.Distributed Queries in IDS: New features.
Distributed Queries in IDS: New features.
 
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
Die Neuheiten in MariaDB 10.2 und MaxScale 2.1
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
War of the Indices- SQL Server and Oracle
War of the Indices-  SQL Server and OracleWar of the Indices-  SQL Server and Oracle
War of the Indices- SQL Server and Oracle
 
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
2° Ciclo Microsoft CRUI 3° Sessione: l'evoluzione delle piattaforme tecnologi...
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
 
Apache Sqoop: A Data Transfer Tool for Hadoop
Apache Sqoop: A Data Transfer Tool for HadoopApache Sqoop: A Data Transfer Tool for Hadoop
Apache Sqoop: A Data Transfer Tool for Hadoop
 

More from Mind The Firebird

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tablesMind The Firebird
 
Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyMind The Firebird
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerMind The Firebird
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions workMind The Firebird
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceMind The Firebird
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLMind The Firebird
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in detailsMind The Firebird
 
Understanding Numbers in Firebird SQL
Understanding Numbers in Firebird SQLUnderstanding Numbers in Firebird SQL
Understanding Numbers in Firebird SQLMind The Firebird
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondMind The Firebird
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingMind The Firebird
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Mind The Firebird
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Mind The Firebird
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databasesMind The Firebird
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in FirebirdMind The Firebird
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Mind The Firebird
 

More from Mind The Firebird (20)

Tips for using Firebird system tables
Tips for using Firebird system tablesTips for using Firebird system tables
Tips for using Firebird system tables
 
Using Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easilyUsing Azure cloud and Firebird to develop applications easily
Using Azure cloud and Firebird to develop applications easily
 
A year in the life of Firebird .Net provider
A year in the life of Firebird .Net providerA year in the life of Firebird .Net provider
A year in the life of Firebird .Net provider
 
How Firebird transactions work
How Firebird transactions workHow Firebird transactions work
How Firebird transactions work
 
SuperServer in Firebird 3
SuperServer in Firebird 3SuperServer in Firebird 3
SuperServer in Firebird 3
 
Copycat presentation
Copycat presentationCopycat presentation
Copycat presentation
 
Using ТРСС to study Firebird performance
Using ТРСС to study Firebird performanceUsing ТРСС to study Firebird performance
Using ТРСС to study Firebird performance
 
Overview of RedDatabase 2.5
Overview of RedDatabase 2.5Overview of RedDatabase 2.5
Overview of RedDatabase 2.5
 
Creating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQLCreating logs for data auditing in FirebirdSQL
Creating logs for data auditing in FirebirdSQL
 
Firebird Performance counters in details
Firebird Performance counters in detailsFirebird Performance counters in details
Firebird Performance counters in details
 
Understanding Numbers in Firebird SQL
Understanding Numbers in Firebird SQLUnderstanding Numbers in Firebird SQL
Understanding Numbers in Firebird SQL
 
Threading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyondThreading through InterBase, Firebird, and beyond
Threading through InterBase, Firebird, and beyond
 
Orphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and LoggingOrphans, Corruption, Careful Write, and Logging
Orphans, Corruption, Careful Write, and Logging
 
Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016Firebird release strategy and roadmap for 2015/2016
Firebird release strategy and roadmap for 2015/2016
 
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
Nbackup and Backup: Internals, Usage strategy and Pitfalls, by Dmitry Kuzmenk...
 
Working with Large Firebird databases
Working with Large Firebird databasesWorking with Large Firebird databases
Working with Large Firebird databases
 
Stored procedures in Firebird
Stored procedures in FirebirdStored procedures in Firebird
Stored procedures in Firebird
 
Firebird on Linux
Firebird on LinuxFirebird on Linux
Firebird on Linux
 
Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...Superchaging big production systems on Firebird: transactions, garbage, maint...
Superchaging big production systems on Firebird: transactions, garbage, maint...
 
Firebird meets NoSQL
Firebird meets NoSQLFirebird meets NoSQL
Firebird meets NoSQL
 

Recently uploaded

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

New features of SQL in Firebird

  • 1. New SQL Features in Firebird
  • 2. Remember Firebird 2.1 ● Common SQL ● COMMON TABLE EXPRESSIONS ● INSERT OR UPDATE ● MERGE ● RETURNING ● Built-in functions ● Procedural SQL ● Domains at procedures and triggers ● TYPE OF <domain name> ● DDL ● DATABASE TRIGGER's ● CONNECT | DISCONNECT ● TRANSACTION START | COMMIT | ROLLBACK ● GLOBAL TEMPORARY TABLE ● Monitoring ● Monitoring tables ● Ability to break execution of user query 2 Luxembourg 2011 Whats new in Firebird SQL
  • 3. Whats New in Firebird 2.5 ● Common SQL ● SIMILAR TO ● SQLSTATE ● Hexadecimal constants ● UUID binary tofrom char conversions ● Procedural SQL ● AUTONOMOUS TRANSACTIONS ● EXECUTE STATEMENT ● TYPE OF COLUMN ● DDL ● ALTER VIEW ● ALTER computed fields ● CREATEALTERDROP user ● ALTER ROLE ● GRANTED BY ● Monitoring ● New MONITORING TABLES ● Terminate user connection 3 Luxembourg 2011 Whats new in Firebird SQL
  • 4. Common SQL : SIMILAR TO Regular expressions support per SQL standard specification New SIMILAR TO predicate More powerful version of LIKE with regexp syntax Example : is given string represents a valid number ? Value SIMILAR TO '[+-]?[0-9]*([0-9].|.[0-9])?[[:DIGIT:]]*' ESCAPE '' [+-] + or - ? 0 or 1 times [0-9] any digit * 0 or more times ([0-9].|.[0-9]) <digit and point> or <point and digit> ? 0 or 1 times [[:DIGIT:]] any digit * 0 or more times 4 Luxembourg 2011 Whats new in Firebird SQL
  • 5. Common SQL : SQLSTATE SQLSTATE is standard compliant generic error code for use by generic applications SQLCODE is deprecated (but still supported) and it is recommended to use SQLSTATE To obtain SQLSTATE value use new API function : fb_sqlstate SQLSTATE is not available (yet) for WHEN block of PSQL exception handling isql since v2.5 used SQLSTATE in error messages : FB25>isql SQL> connect 'not_exists'; Statement failed, SQLSTATE = 08001 I/O error during "CreateFile (open)" operation for file "not_exists" -Error while trying to open file -The system cannot find the file specified. FB21>isql SQL> connect 'not_exists'; Statement failed, SQLCODE = -902 I/O error for file "...not_exists" -Error while trying to open file -The system cannot find the file specified. 5 Luxembourg 2011 Whats new in Firebird SQL
  • 6. Common SQL : HEX Literals Hexadecimal numeric and binary string literals Numeric literals : 0xHHHHHHHH Prefix 0x or 0X Up to 16 hexadecimal digits 1 - 8 digits : data type is - signed integer 9 - 16 digits : data type is - signed bigint SQL> SELECT 0xF0000000, 0x0F0000000 FROM RDB$DATABASE; CONSTANT CONSTANT ============ ===================== -268435456 4026531840 6 Luxembourg 2011 Whats new in Firebird SQL
  • 7. Common SQL : HEX literals Hexadecimal numeric and binary string literals String literals : x'HH...H' Prefix x or X Data type - CHAR(N / 2) CHARACTER SET OCTETS, where N – number of digits SQL> SELECT 'First line' || _ASCII x'0D0A09' || 'Second line' CON> FROM RDB$DATABASE; CONCATENATION ======================== First line Second line 7 Luxembourg 2011 Whats new in Firebird SQL
  • 8. Common SQL : UUID <-> CHAR UUID binary tofrom CHAR conversion CHAR_TO_UUID Converts the CHAR(32) ASCII representation of an UUID (XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX) to the binary (CHAR(16) OCTETS) representation (optimized for storage) SQL> SELECT CHAR_TO_UUID('A96B285B-4629-45A1-9A86-A8ECCF6561F4') FROM RDB$DATABASE; CHAR_TO_UUID ================================ A96B285B462945A19A86A8ECCF6561F4 8 Luxembourg 2011 Whats new in Firebird SQL
  • 9. Common SQL : UUID <-> CHAR UUID binary tofrom CHAR conversion UUID_TO_CHAR Converts a binary (CHAR(16) OCTETS) UUID to the string (CHAR(32) ASCII) representation SQL> SELECT UUID_TO_CHAR(x'A96B285B462945A19A86A8ECCF6561F4') FROM RDB$DATABASE; UUID_TO_CHAR ==================================== A96B285B-4629-45A1-9A86-A8ECCF6561F4 9 Luxembourg 2011 Whats new in Firebird SQL
  • 10. PSQL : Autonomous Transactions Syntax IN AUTONOMOUS TRANSACTION DO <simple statement | compound statement> Parameters ? Same as outer transaction (isolation level, readwrite, wait mode, etc) Configurable ? Not yet... may be later How it ends ? if (statement executed ok) then commit else rollback Notes Autonomous transaction and its outer transaction fully independent and isolated from each other as any other two transactions. Temporary BLOBs, created in autonomous transaction, attached to outer transaction. This is done to allow usage of such blobs after autonomous transaction ends. This behavior may be a source of unexpected additional memory consumption. 10 Luxembourg 2011 Whats new in Firebird SQL
  • 11. PSQL : EXECUTE STATEMENT ● New implementation of EXECUTE STATEMENT : ● Input parameters ● May run with privileges of caller PSQL object ● Autonomous transactions ● Query another Firebird database ● Full backward compatibility ● Syntax [FOR] EXECUTE STATEMENT <query_text> [(<input_parameters>)] [ON EXTERNAL [DATA SOURCE] <connection_string>] [WITH AUTONOMOUS | COMMON TRANSACTION] [AS USER <user_name>] [PASSWORD <password>] [ROLE <role_name>] [WITH CALLER PRIVILEGES] [INTO <variables>] 11 Luxembourg 2011 Whats new in Firebird SQL
  • 12. PSQL : EXECUTE STATEMENT Input parameters Named input parameters EXECUTE STATEMENT ('INSERT INTO TABLE VALUES (:a, :b, :a)') (a := 100, b := CURRENT_CONNECTION) Not named input parameters EXECUTE STATEMENT ('INSERT INTO TABLE VALUES (?, ?, ?)') (100, CURRENT_CONNECTION, 100) 12 Luxembourg 2011 Whats new in Firebird SQL
  • 13. PSQL : EXECUTE STATEMENT Caller privileges -- logon as SYSDBA CREATE TABLE A (ID INT); CREATE USER VLAD PASSWORD 'vlad'; CREATE PROCEDURE P1 RETURNS (CNT INT) AS BEGIN EXECUTE STATEMENT 'SELECT COUNT(*) FROM A' INTO :CNT; SUSPEND; END; GRANT SELECT ON TABLE A TO PROCEDURE P1; GRANT EXECUTE ON PROCEDURE P1 TO USER VLAD; -- logon as VLAD SELECT * FROM P1; Statement failed, SQLSTATE = 42000 Execute statement error at jrd8_prepare : 335544352 : no permission for read/select access to TABLE A Statement : SELECT COUNT(*) FROM A Data source : Internal:: -At procedure 'P1' 13 Luxembourg 2011 Whats new in Firebird SQL
  • 14. PSQL : EXECUTE STATEMENT Caller privileges -- logon as SYSDBA CREATE PROCEDURE P2 RETURNS (CNT INT) AS BEGIN EXECUTE STATEMENT 'SELECT COUNT(*) FROM A' WITH CALLER PRIVILEGES INTO :CNT; SUSPEND; END; GRANT SELECT ON TABLE A TO PROCEDURE P2; GRANT EXECUTE ON PROCEDURE P2 TO USER VLAD; -- logon as VLAD SELECT * FROM P2; CNT ============ 0 Dynamic statement is executed with that set of privileges as it would have if its executed immediately by caller PSQL object (procedure or trigger) 14 Luxembourg 2011 Whats new in Firebird SQL
  • 15. PSQL : EXECUTE STATEMENT Transactions Common transaction (default) statement executed in the current transaction EXECUTE STATEMENT '...' WITH COMMON TRANSACTION Autonomous transaction statement executed in the separate new transaction EXECUTE STATEMENT '...' WITH AUTONOMOUS TRANSACTION parameters the same as current trancation, not (yet) configurable 15 Luxembourg 2011 Whats new in Firebird SQL
  • 16. PSQL : EXECUTE STATEMENT Query another Firebird database EXTERNAL DATA SOURCE clause : <connection_string> is usual Firebird's connection string Query another database using userpassword EXECUTE STATEMENT '...' ON EXTERNAL DATA SOURCE 'host:path' USER 'VLAD' PASSWORD 'dontKnow' When userpassword is not set Trusted autentication (Windows only) : Firebird's process account name is effective user name at remote database (if TA is supported by remote side) EXECUTE STATEMENT '...' ON EXTERNAL DATA SOURCE 'host:path' CURRENT_USER is effective user name at local database EXECUTE STATEMENT '...' ON EXTERNAL DATA SOURCE 'path_to_the_current_database' 16 Luxembourg 2011 Whats new in Firebird SQL
  • 17. PSQL : TYPE OF COLUMN Syntax data_type ::= <builtin_data_type> | <domain_name> | TYPE OF <domain_name> | TYPE OF COLUMN <table_name>.<column_name> Usage ● Input and output parameters CREATE PROCEDURE MY_PROC (IN_PARAM TYPE OF COLUMN <table_name>.<column_name>) RETURNS (OUT_PARAM TYPE OF COLUMN <table_name>.<column_name>) ● Variable declaration DECLARE VARIABLE VAR1 TYPE OF COLUMN <table_name>.<column_name> ● CAST statement OUT_PARAM = CAST(VAR1 AS TYPE OF COLUMN <table_name>.<column_name>) 17 Luxembourg 2011 Whats new in Firebird SQL
  • 18. ALTER VIEW COMPUTED FIELD Syntax {CREATE OR ALTER | ALTER} VIEW <view_name> [(<field list>)] AS <select statement> ALTER TABLE <table_name> ALTER <field_name> [TYPE <data type>] COMPUTED BY (<expression>) Task ● change view (or computed field) definition ● Before Firebird 2.5 ● drop and create (or alter two times) again all dependent objects ● restore all privileges granted to all dependent objects which was re-created ● Firebird 2.5 ● just execute ALTER VIEW and enjoy :-) 18 Luxembourg 2011 Whats new in Firebird SQL
  • 19. ALTER VIEW COMPUTED FIELD Example CREATE TABLE T (NAME CHAR(20), CF COMPUTED BY (LEFT(NAME, 10)) ); CREATE VIEW V AS SELECT CF FROM T; CREATE PROCEDURE P ... SELECT ... FROM V ...; -- dependent procedure Try to change both view and computed field definitions Before Firebird 2.5 : ALTER PROCEDURE P AS BEGIN END; -- empty body for ALL dependent objects DROP VIEW V; ALTER TABLE T DROP CF; ALTER TABLE T ADD CF COMPUTED BY (SUBSTRING(NAME FOR 15)); CREATE VIEW V AS SELECT NAME, CF FROM T; GRANT SELECT ON TABLE T TO VIEW V; -- restore ALL dependent ALTER PROCEDURE P ... SELECT ... FROM V ...; -- objects and privileges What if you have hundreds dependent objects ? 19 Luxembourg 2011 Whats new in Firebird SQL
  • 20. ALTER VIEW COMPUTED FIELD Example CREATE TABLE T (NAME CHAR(20), CF COMPUTED BY (LEFT(NAME, 10)) ); CREATE VIEW V AS SELECT CF FROM T; CREATE PROCEDURE P ... SELECT ... FROM V ...; -- dependent procedure Try to change both view and computed field definitions Firebird 2.5 : ALTER TABLE T ALTER CF COMPUTED BY (RIGHT(NAME, 10)); ALTER VIEW V AS SELECT NAME, CF FROM T; 20 Luxembourg 2011 Whats new in Firebird SQL
  • 21. DDL : USER MANAGEMENT Syntax CREATE USER name PASSWORD 'password' [FIRSTNAME 'firstname'] [MIDDLENAME 'middlename'] [LASTNAME 'lastname'] ALTER USER name PASSWORD 'password' [FIRSTNAME 'firstname'] [MIDDLENAME 'middlename'] [LASTNAME 'lastname'] DROP USER name 21 Luxembourg 2011 Whats new in Firebird SQL
  • 22. DDL : SYSDBA and SYSADMIN's When Windows Administrator is connected using trusted authentication : Firebird 2.1 CURRENT_USER is a SYSDBA SYSDBA privileges Firebird 2.5 CURRENT_USER is always a DomainUser Auto-admin mapping (per database) ALTER ROLE RDB$ADMIN SET|DROP AUTO ADMIN MAPPING; Auto-admin mapping ROLE Privileges ON NONE or RDB$ADMIN SYSDBA OFF NONE or RDB$ADMIN ON ordinary Other OFF 22 Luxembourg 2011 Whats new in Firebird SQL
  • 23. MONITORING TABLES MON$DATABASE Databases and objects Databases and objects MON$DATABASE_NAME ... New ! MON$ATTACHMENTS ODS 11.2 MON$ATTACHMENT_ID MON$ATTACHMENT_NAME MON$CONTEXT_VARIABLES ... MON$ATTACHMENT_ID MON$TRANSACTION_ID MON$TRANSACTIONS MON$VARIABLE_NAME MON$VARIABLE_VALUE MON$TRANSACTION_ID MON$ATTACHMENT_ID ... MON$CALL_STACK MON$STATEMENTS MON$CALL_ID MON$STATEMENT_ID MON$STATEMENT_ID MON$CALLER_ID MON$ATTACHMENT_ID ... MON$TRANSACTION_ID ... 23 Luxembourg 2011 Whats new in Firebird SQL
  • 24. MONITORING TABLES Objects and statistics Objects and statistics MON$DATABASE ... MON$STAT_ID MON$IO_STATS MON$STAT_ID MON$ATTACHMENTS MON$STAT_GROUP ... ... MON$STAT_ID MON$TRANSACTIONS MON$RECORD_STATS ... MON$STAT_ID MON$STAT_ID MON$STAT_GROUP MON$STATEMENTS ... ... MON$STAT_ID MON$MEMORY_USAGE MON$CALL_STACK ... MON$STAT_ID MON$STAT_ID MON$STAT_GROUP New ! ... ODS 11.2 24 Luxembourg 2011 Whats new in Firebird SQL
  • 25. MONITORING TABLES Some monitoring tables is “active” 1) Cancel current activity of connection № 32: DELETE FROM MON$STATEMENTS WHERE MON$ATTACHMENT_ID = 32 2) Cancel all queries running more than 20 minutes: DELETE FROM MON$STATEMENTS WHERE DATEADD(20 MINUTE TO MON$TIMESTAMP) < CURRENT_TIMESTAMP AND MON$STATE <> 0; 3) Disconnect everybody but ourselves (new in Firebird 2.5): DELETE FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION 25 Luxembourg 2011 Whats new in Firebird SQL
  • 26. What's next ? 26 Luxembourg 2011 Whats new in Firebird SQL
  • 27. Whats new in Firebird 3 ● Common SQL ● Full syntax of MERGE (per SQL 2008) ● support for DELETE substatement ● additional condition in WHEN clause ● multiply WHEN clauses ● MERGE ... RETURNING ● Window (analytical) functions ● SUBSTRING with regular expressions ● BOOLEAN data type ● Cursor stability with data modification queries ● Global temporary tables are improved 27 Luxembourg 2011 Whats new in Firebird SQL
  • 28. Whats new in Firebird 3 ● Procedural SQL ● SQL functions ● Sub-routines ● External functions, procedures and triggers on CC++PascalJava etc. ● Packages ● Exceptions with parameters : EXCEPTION ... USING (...) ● SQLSTATE in WHEN handler ● CONTINUE in loops 28 Luxembourg 2011 Whats new in Firebird SQL
  • 29. Whats new in Firebird 3 ● DDL ● Manage nullability of column ● ALTER DOMAIN ... {NULL | NOT NULL} ● ALTER COLUMN ... {NULL | NOT NULL} ● ALTER DATABASE ... SET DEFAULT CHARACTER SET ● IDENTITY columns (hello, migrants from MSSQLMySQL ;) ● RECREATE SEQUENCE, RECREATE GENERATOR ● DDL triggers 29 Luxembourg 2011 Whats new in Firebird SQL
  • 30. Whats new in Firebird 3 ● More complete system of SQL grants ● GRANT CREATE | ALTER | DROP <object> TO <user> | <role> ● GRANT ROLE TO ROLE ● Monitoring ● Extended statistics, queries plan's, ... ● To be continued :-) 30 Luxembourg 2011 Whats new in Firebird SQL
  • 31. Common SQL : MERGE Full SQL 2008 syntax MERGE INTO <table> USING <table_or_join> ON <search_condition> [WHEN MATCHED [AND <search_condition>] THEN UPDATE SET col1 = val1, ..., colN = valN | DELETE] [WHEN NOT MATCHED [AND <search_condition>] THEN INSERT [(col1, ..., colN)] VALUES (val1, ..., valN)] 31 Luxembourg 2011 Whats new in Firebird SQL
  • 32. Common SQL : MERGE DELETE substatement and multiply WHEN clauses MERGE INTO TABLE USING LOG ON TABLE.PK = LOG.PK WHEN MATCHED AND LOG.ACTION = 'D' THEN DELETE WHEN MATCHED THEN -- second WHEN MATCHED clause UPDATE SET col1 = LOG.val1, ... WHEN NOT MATCHED THEN INSERT (col1, ...) VALUES (LOG.val1, ...) 32 Luxembourg 2011 Whats new in Firebird SQL
  • 33. Common SQL : MERGE RETURNING clause MERGE INTO <table> USING <table_or_join> ON <search_condition> [WHEN MATCHED [AND <search_condition>] THEN UPDATE SET col1 = val1, ..., colN = valN | DELETE] [WHEN NOT MATCHED [AND <search_condition>] THEN INSERT [(col1, ..., colN)] VALUES (val1, ..., valN)] [RETURNING ... [INTO ...]] 33 Luxembourg 2011 Whats new in Firebird SQL
  • 34. Common SQL : WINDOW FUNCTIONS Syntax <window function> ::= <window function type> OVER (<window specification>) <window function type> ::= <aggregate function> –- aggregate | <rank function type> –- ranking | ROW_NUMBER –- yes, it is row number ;) | <lead or lag function> –- navigational | <first or last value function> | <nth value function> <window specification> ::= [PARTITION BY column1, ...] [ORDER BY column1 [ASC|DESC] [NULLS {FIRST|LAST}], ... ] 34 Luxembourg 2011 Whats new in Firebird SQL
  • 35. Common SQL : WINDOW FUNCTIONS Syntax <aggregate function> ::= AVG | MAX | MIN | SUM | COUNT | LIST <rank function type> ::= RANK | DENSE_RANK <lead or lag function> ::= LEAD | LAG <first or last value function> ::= {FIRST_VALUE | LAST_VALUE} (<value>) <nth value function> ::= NTH_VALUE (<value>, <nth row>) 35 Luxembourg 2011 Whats new in Firebird SQL
  • 36. Common SQL : WINDOW FUNCTIONS Example SELECT A, B, C, SUM(C) OVER(), SUM(C) OVER(ORDER BY A, B), SUM(C) OVER(PARTITION BY A), SUM(C) OVER(PARTITION BY A ORDER BY B) A B C SUM SUM1 SUM2 SUM3 1 1 30 141 30 60 30 1 2 20 141 50 60 50 1 3 10 141 60 60 60 2 1 25 141 85 40 25 2 2 15 141 100 40 40 3 1 41 141 141 41 41 36 Luxembourg 2011 Whats new in Firebird SQL
  • 37. Common SQL : substring search using REGEXP Syntax SUBSTRING(<string> SIMILAR <pattern> ESCAPE <char>) Rules ● Pattern ● R = <R1> <E> " <R2> <E> " <R3> ● Search ● S = <S1> <S2> <S3> 1)<S> SIMILAR TO <R1> <R2> <R3> ESCAPE <E> 2)<S1> SIMILAR TO <R1> ESCAPE <E> AND <S2> <S3> SIMILAR TO <R2> <R3> ESCAPE <E> 3)<S2> SIMILAR TO <R2> ESCAPE <E> AND <S3> SIMILAR TO <R3> ESCAPE <E> ● Result ● S2 37 Luxembourg 2011 Whats new in Firebird SQL
  • 38. Common SQL : substring search using REGEXP Syntax SUBSTRING(<string> SIMILAR <pattern> ESCAPE <char>) Example SUBSTRING('abc-12b34xyz' SIMILAR '%"[+-]?[0-9]+"%' ESCAPE '') R1 = % R2 = [+-]?[0-9]+ R3 = % 1) 'abc-12b34xyz' SIMILAR TO '%[+-]?[0-9]+%' ESCAPE '' 2) 'abc' SIMILAR TO '%' ESCAPE '' AND '-12b34xyz' SIMILAR TO '[+-]?[0-9]+%' ESCAPE '' 3) '-12' SIMILAR TO '[+-]?[0-9]+' ESCAPE '' AND 'b34xyz' SIMILAR TO '%' ESCAPE '' Result '-12' 38 Luxembourg 2011 Whats new in Firebird SQL
  • 39. Common SQL : BOOLEAN data type Syntax <data_type> ::= BOOLEAN <boolean_literal> ::= TRUE | FALSE | UNKNOWN Storage 1 byte Client support (XSQLDA) #define SQL_BOOLEAN 32764 39 Luxembourg 2011 Whats new in Firebird SQL
  • 40. Common SQL : BOOLEAN data type Truth tables AND True False Unknown OR True False Unknown True True False Unknown True True True True False False False False False True False Unknown Unknown Unknown False Unknown Unknown True Unknown Unknown IS True False Unknown NOT True True False False True False False False True False False True Unknown False False True Unknown Unknown 40 Luxembourg 2011 Whats new in Firebird SQL
  • 41. Common SQL : BOOLEAN data type Examples CREATE TABLE TBOOL (ID INT, BVAL BOOLEAN); COMMIT; INSERT INTO TBOOL VALUES (1, TRUE); INSERT INTO TBOOL VALUES (2, 2 = 4); INSERT INTO TBOOL VALUES (3, NULL = 1); COMMIT; SELECT * FROM TBOOL ID BVAL ============ ======= 1 <true> 2 <false> 3 <null> 41 Luxembourg 2011 Whats new in Firebird SQL
  • 42. Common SQL : BOOLEAN data type Examples 1. Test for TRUE value 3. Tests for UNKNOWN value SELECT * FROM TBOOL SELECT * FROM TBOOL WHERE BVAL WHERE BVAL IS UNKNOWN ID BVAL ID BVAL ============ ======= ============ ======= 1 <true> 3 <null> 2. Test for FALSE value SELECT * FROM TBOOL SELECT * FROM TBOOL WHERE BVAL IS FALSE WHERE BVAL = UNKNOWN ID BVAL ============ ======= SELECT * FROM TBOOL 2 <false> WHERE BVAL <> UNKNOWN 42 Luxembourg 2011 Whats new in Firebird SQL
  • 43. Common SQL : BOOLEAN data type Examples 4. Boolean values in SELECT list SELECT ID, BVAL, BVAL AND ID < 2 FROM TBOOL ID BVAL ============ ======= ======= 1 <true> <true> 2 <false> <false> 3 <null> <false> 43 Luxembourg 2011 Whats new in Firebird SQL
  • 44. Common SQL : cursor stability The issue ● Famous infinite insertion circle (CORE-92) INSERT INTO T SELECT * FROM T ● DELETE more rows than expected (CORE-634) DELETE FROM T WHERE ID IN (SELECT FIRST 1 ID FROM T) ● All DML statements is affected (INSERT, UPDATE, DELETE, MERGE) ● Common ticket at tracker CORE-3362 44 Luxembourg 2011 Whats new in Firebird SQL
  • 45. Common SQL : cursor stability The reason of the issue ● DML statements used implicit cursors ● INSERT INTO T SELECT ... FROM T works as FOR SELECT <values> FROM T INTO <tmp_vars> DO INSERT INTO T VALUES (<tmp_vars>) ● UPDATE T SET <fields> = <values> WHERE <conditions> works as FOR SELECT <values> FROM T WHERE <conditions> INTO <tmp_vars> AS CURSOR <cursor> DO UPDATE T SET <fields> = <tmp_vars> WHERE CURRENT OF <cursor> ● DELETE works like UPDATE 45 Luxembourg 2011 Whats new in Firebird SQL
  • 46. Common SQL : cursor stability The “standard” way ● Rows to be insertedupdateddeleted should be marked first ● Marked rows is insertedupdateddeleted then ● Pros ● rowset is stable and is not affected by DML statement itself ● Cons ● Marks should be saved somewhere and rows will be visited again, or ● Set of marked rows should be saved somewhere and this store will be visited again ● Note : this could be reached in Firebird using (well known) workaround: force query to have SORT in PLAN - it will materialize implicit cursor and make it stable 46 Luxembourg 2011 Whats new in Firebird SQL
  • 47. Common SQL : cursor stability The Firebird 3 way ● Use undo-log to see if record was already modified by current cursor ● if record was inserted - ignore it ● if record was updated or deleted - read backversion ● Pros ● No additional bookkeeping required ● No additional storage required ● Relatively easy to implement ● Cons ● Inserted records could be visited (but ignored, of course) ● Backversions of updateddeleted records should be read ● Not works with SUSPEND in PSQL 47 Luxembourg 2011 Whats new in Firebird SQL
  • 48. Common SQL : cursor stability PSQL notes ● PSQL cursors with SUSPEND inside still not stable ! This query still produced infinite circle FOR SELECT ID FROM T INTO :ID DO BEGIN INSERT INTO T (ID) VALUES (:ID); SUSPEND; END 48 Luxembourg 2011 Whats new in Firebird SQL
  • 49. Common SQL : cursor stability PSQL notes ● PSQL cursors without SUSPEND inside is stable now, this could change old behavior FOR SELECT ID FROM T WHERE VAL IS NULL INTO :ID DO BEGIN UPDATE T SET VAL = 1 WHERE ID = :ID; END 49 Luxembourg 2011 Whats new in Firebird SQL
  • 50. Common SQL : improvements in GTT ● Global temporary tables is writable even in read-only transactions ● Read-only transaction in read-write database ● Both ON COMMIT PRESERVE ROWS and ON COMMIT DELETE ROWS ● Read-only transaction in read-only database ● GTT ON COMMIT DELETE ROWS only ● Faster rollback for GTT ON COMMIT DELETE ROWS ● No need to backout records on rollback ● Garbage collection in GTT is not delayed by active transactions of another connections ● All this improvements is backported into v2.5.1 too 50 Luxembourg 2011 Whats new in Firebird SQL
  • 51. PSQL : SQL functions Syntax {CREATE [OR ALTER] | ALTER | RECREATE} FUNCTION <name> [(param1 [, ...])] RETURNS <type> AS BEGIN ... END Example CREATE FUNCTION F(X INT) RETURNS INT AS BEGIN RETURN X+1; END; SELECT F(5) FROM RDB$DATABASE; 51 Luxembourg 2011 Whats new in Firebird SQL
  • 52. PSQL : SQL sub-routines Syntax ● Sub-procedures DECLARE PROCEDURE <name> [(param1 [, ...])] [RETURNS (param1 [, ...])] AS ... ● Sub-functions DECLARE FUNCTION <name> [(param1 [, ...])] RETURNS <type> AS ... 52 Luxembourg 2011 Whats new in Firebird SQL
  • 53. PSQL : SQL sub-routines Example EXECUTE BLOCK RETURNS (N INT) AS DECLARE FUNCTION F(X INT) RETURNS INT AS BEGIN RETURN X+1; END; BEGIN N = F(5); SUSPEND; END 53 Luxembourg 2011 Whats new in Firebird SQL
  • 54. PSQL : Packages -– package header, declarations only CREATE OR ALTER PACKAGE TEST AS BEGIN PROCEDURE P1(I INT) RETURNS (O INT); -– public procedure END -– package body, implementation RECREATE PACKAGE BODY TEST AS BEGIN FUNCTION F1(I INT) RETURNS INT; –- private function PROCEDURE P1(I INT) RETURNS (O INT) AS BEGIN END; FUNCTION F1(I INT) RETURNS INT AS BEGIN RETURN 0; END; END 54 Luxembourg 2011 Whats new in Firebird SQL
  • 55. DDL : IDENTITY columns Syntax <column definition> ::= <name> <type> GENERATED BY DEFAULT AS IDENTITY <constraints> Rules ● Column type – INTEGER or NUMERIC(P, 0) ● Implicit NOT NULL ● Not guarantees uniqueness ● Can not have DEFAULT clause ● Can not be COMPUTED BY 55 Luxembourg 2011 Whats new in Firebird SQL
  • 56. DDL : DDL triggers Syntax <ddl-trigger> ::= {CREATE | RECREATE | CREATE OR ALTER} TRIGGER <name> [ACTIVE | INACTIVE] {BEFORE | AFTER} <ddl event> [POSITION <n>] <ddl event> ::= ANY DDL STATEMENT | <ddl event item> [{OR <ddl event item>}...] <ddl event item> ::= {CREATE | ALTER | DROP} {TABLE | PROCEDURE | FUNCTION | TRIGGER | EXCEPTION | VIEW | DOMAIN | SEQUENCE | INDEX | ROLE | USER | COLLATION | PACKAGE | PACKAGE BODY | CHARACTER SET } 56 Luxembourg 2011 Whats new in Firebird SQL
  • 57. DDL : DDL triggers New context variables (RDB$GET_CONTEXT) ● Namespace DDL_TRIGGER ● Defined inside DDL trigger only ● Read only ● Predefined variables: ● DDL_EVENT - kind of DDL event ● OBJECT_NAME – name of metadata object ● SQL_TEXT - text of SQL query 57 Luxembourg 2011 Whats new in Firebird SQL
  • 58. PSQL : EXCEPTION with parameters Syntax EXEPTION <name> USING (param1 [, ...]); Example CREATE EXCEPTION EX_BAD_SP_NAME 'Name of procedures must start with ''@1'' : ''@2'''; CREATE TRIGGER TRG_SP_CREATE BEFORE CREATE PROCEDURE AS DECLARE SP_NAME VARCHAR(255); BEGIN SP_NAME = RDB$GET_CONTEXT('DDL_TRIGGER', 'OBJECT_NAME'); IF (SP_NAME NOT STARTING 'SP_') THEN EXCEPTION EX_BAD_SP_NAME USING ('SP_', SP_NAME); END; 58 Luxembourg 2011 Whats new in Firebird SQL
  • 59. THANK YOU FOR ATTENTION Questions ? Firebird official web site Firebird tracker hvlad@users.sf.net 59 Luxembourg 2011 Whats new in Firebird SQL