SERIAL data types

Informix®

Informix supports the SERIAL, BIGSERIAL data types to produce automatic integer sequences:
  • SERIAL can produce 32 bit integers (INTEGER)
  • BIGSERIAL can produced 64 bit integers (BIGINT)
  • SERIAL8 is a synonym for BIGSERIAL
Steps to use serials with Informix:
  1. Create the table with a column using SERIAL, or BIGSERIAL.
  2. To generate a new serial, no value or a zero value is specified in the INSERT statement:
    INSERT INTO tab1 ( c ) VALUES ( 'aa' )
    INSERT INTO tab1 ( k, c ) VALUES ( 0, 'aa' )
  3. After INSERT, the new value of a SERIAL column is provided in SQLCA.SQLERRD[2], while the new value of a BIGSERIAL value must be fetched with a SELECT dbinfo('bigserial') query.

Informix allows you to insert rows with a value different from zero for a serial column. Using an explicit value will automatically increment the internal serial counter, to avoid conflicts with future INSERT statements that are using a zero value:

CREATE TABLE tab ( k SERIAL); -- internal counter = 0
INSERT INTO tab VALUES ( 0 ); -- internal counter = 1
INSERT INTO tab VALUES ( 10 ); -- internal counter = 10
INSERT INTO tab VALUES ( 0 ); -- internal counter = 11
DELETE FROM tab; -- internal counter = 11
INSERT INTO tab VALUES ( 0 ); -- internal counter = 12

IBM® DB2®

IBM DB2 supports IDENTITY columns:
CREATE TABLE tab ( k INTEGER GENERATED ALWAYS AS IDENTITY, name VARCHAR(50) )

To get the last generated IDENTITY value after an INSERT, DB2 provides the IDENTITY_VAL_LOCAL() function.

IBM DB2 supports SEQUENCES:
CREATE SEQUENCE sq1 START WITH 100
To create a new sequence number, you must use the NEXTVAL FOR operator:
INSERT INTO table VALUES ( NEXTVAL FOR sq1, ... )
To get the last generated sequence number, you must use the PREVVAL FOR operator:
SELECT PREVVAL FOR sq1 ...

Solution

Note: For best SQL portability when using different type of databases, consider using sequences as described in Solution 3: Use native SEQUENCE database objects.

To emulate Informix serials with IBM DB2, you can use IDENTITY columns (1), or insert triggers using sequences (2). The first solution is faster, but does not allow explicit serial value specification in insert statements; the second solution is slower but allows explicit serial value specification.

Important: The trigger-based solution is provided to simplify the conversion from Informix, but is slower as the solution using identity columns. We strongly recommend that you use native IDENTITY columns instead to get best performances.
The method used to emulate SERIAL types is defined by the ifxemul.datatype.serial.emulation FGLPROFILE parameter:
dbi.database.dbname.ifxemul.datatype.serial.emulation = {"native"|"trigseq"}
  • native: uses IDENTITY columns.
  • trigseq: uses insert triggers with sequences.

The default emulation technique is "native".

The serial types emulation can be enabled or disabled with the folllowing FGLPROFILE entries:
dbi.database.dbname.ifxemul.datatype.serial = {true|false}
dbi.database.dbname.ifxemul.datatype.serial8 = {true|false}
dbi.database.dbname.ifxemul.datatype.bigserial = {true|false}

Using the native serial emulation

In database creation scripts, all SERIAL[(n)] data types must be converted by hand to:

INTEGER GENERATED ALWAYS AS IDENTITY[( START WITH n, INCREMENT BY 1)]
while the SERIAL8 and BIGSERIAL[(n)] types must be converted to:
BIGINT GENERATED ALWAYS AS IDENTITY[( START WITH n, INCREMENT BY 1)]

Tables created from the BDL programs can use the SERIAL data type: When a BDL program executes a CREATE [TEMP] TABLE with a SERIAL column, the database interface automatically converts the SERIAL[(n)] data type to an IDENTITY specification.

In BDL, the new generated SERIAL value is available from the SQLCA.SQLERRD[2] variable. The database interface performs a call to the IDENTITY_VAL_LOCAL() function. However, SQLCA.SQLERRD[2] is defined as an INTEGER, it cannot hold values from BIGINT identity columns. If you are using BIGINT IDENTITY columns, you must use the IDENTITY_VAL_LOCAL() function.

Since IBM DB2 does not allow you to specify the value of IDENTITY columns, it is mandatory to convert all INSERT statements to remove the SERIAL column from the list. For example, the following statement:
INSERT INTO tab (col1,col2) VALUES (0, p_value)
must be converted to:
INSERT INTO tab (col2) VALUES (p_value)

Static SQL INSERT using records defined from the schema file must also be reviewed:

DEFINE rec LIKE tab.*
INSERT INTO tab VALUES ( rec.* ) -- will use the serial column 
must be converted to:
INSERT INTO tab VALUES rec.* -- without parentheses, serial column is removed

Using the trigseq serial emulation

In database creation scripts, all SERIAL[(n)] data types must be converted to INTEGER data types, SERIAL8/BIGSERIAL must be converted to BIGINT, and you must create a sequence and a trigger for each table using a SERIAL.

To know how to write those triggers, you can create a small Genero program that creates a table with a SERIAL column. Set the FGLSQLDEBUG environment variable and run the program. The debug output will show you the native SQL commands to create the sequence and the trigger.

Tables created from the BDL programs can use the SERIAL data type: When a BDL program executes a CREATE [TEMP] TABLE with a SERIAL column, the database interface automatically converts the SERIAL[(n)] data type to INTEGER and creates the sequence and the insert trigger.

Note:

IBM DB2 performs NOT NULL data controls before the execution of triggers. If the serial column must be NOT NULL (for example, because it is part of the primary key), you cannot specify a NULL value for that column in INSERT statements.

For example, the following statement:
INSERT INTO tab VALUES (NULL,p_value)
must be converted to:
INSERT INTO tab (col2) VALUES (p_value)
Important:
  • IBM DB2 triggers are not automatically dropped when the corresponding table is dropped. They become inoperative instead. Database administrators must take care of this behavior when managing schemas.
  • With IBM DB2, INSERT statements using NULL for the SERIAL column will produce a new serial value:
    INSERT INTO tab ( col_serial, col_data ) VALUES ( NULL, 'data' )
    This behavior is mandatory in order to support INSERT statements which do not use the serial column:
    INSERT INTO tab (col_data) VALUES ('data')

    Check if your application uses tables with a SERIAL column that can contain a NULL value.

  • With DB2, trigger creation is not allowed on temporary tables. Therefore, the "trigseq" method cannot work with temporary tables using serials.