FirebirdSQL logo

A sequence — or generator — is a database object used to get unique number values to fill a series.“Sequence” is the SQL-compliant term for the same thing which — in Firebird — has traditionally been known as “generator”.Firebird has syntax for both terms.

Sequences are always stored as 64-bit integers, regardless of the SQL dialect of the database.

Caution

If a client is connected using Dialect 1, the server handles sequence values as 32-bit integers.Passing a sequence value to a 32-bit field or variable will not cause errors as long as the current value of the sequence does not exceed the limits of a 32-bit number.However, as soon as the sequence value exceeds this limit, a database in Dialect 3 will produce an error.A database in Dialect 1 will truncate (overflow) the value, which could compromise the uniqueness of the series.

This section describes how to create, alter, set and drop sequences.

CREATE SEQUENCE

Creates a SEQUENCE (GENERATOR)

Available in

DSQL, ESQL

Syntax
CREATE {SEQUENCE | GENERATOR} seq_name
  [START WITH start_value]
  [INCREMENT [BY] increment]
Table 1. CREATE SEQUENCE Statement Parameters
Parameter Description

seq_name

Sequence (generator) name.The maximum length is 63 characters

start_value

Initial value of the sequence.Default is 1.

increment

Increment of the sequence (when using NEXT VALUE FOR seq_name);cannot be 0.Default is 1.

The statements CREATE SEQUENCE and CREATE GENERATOR are synonymous — both create a new sequence.Either can be used, but CREATE SEQUENCE is recommended as that is the syntax defined in the SQL standard.

When a sequence is created, its current value is set so that the next value obtained from NEXT VALUE FOR seq_name is equal to start_value.In other words, the current value of the sequence is set to (start_value - increment).By default, the start_value is 1 (one).

The optional INCREMENT [BY] clause allows you to specify an increment for the NEXT VALUE FOR seq_name expression.By default, the increment is 1 (one).The increment cannot be set to 0 (zero).The GEN_ID(seq_name, <step>) function can be called instead, to “step” the series by a different integer number.The increment specified through INCREMENT [BY] is not used for GEN_ID.

Note
Non-standard behaviour for negative increments

The SQL standard specifies that sequences with a negative increment should start at the maximum value of the sequence (263 - 1) and count down.Firebird does not do that, and instead starts at 1.

This may change in a future Firebird version.