FirebirdSQL logo

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.

Who Can Create a Sequence?

The CREATE SEQUENCE (CREATE GENERATOR) statement can be executed by:

  • Administrators

  • Users with the CREATE SEQUENCE (CREATE GENERATOR) privilege

The user executing the CREATE SEQUENCE (CREATE GENERATOR) statement becomes its owner.