FirebirdSQL logo

ALTER SEQUENCE

Sets the next value of a sequence, or changes its increment

Available in

DSQL

Syntax
ALTER {SEQUENCE | GENERATOR} seq_name
  [RESTART [WITH newvalue]]
  [INCREMENT [BY] increment]
Table 1. ALTER SEQUENCE Statement Parameters
Parameter Description

seq_name

Sequence (generator) name

newvalue

New sequence (generator) value.A 64-bit integer from -2-63 to 263-1.

increment

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

The ALTER SEQUENCE statement sets the current value of a sequence to the specified valueand/or changes the increment of the sequence.

The RESTART WITH newvalue clause allows you to set the next value generated by NEXT VALUE FOR seq_name.To achieve this, the current value of the sequence is set to (newvalue - increment) with increment either as specified in the statement, or stored in the metadata of the sequence.The RESTART clause (without WITH) restarts the sequence with the initial value stored in the metadata of the sequence.

Note

Contrary to Firebird 3.0, since Firebird 4.0 RESTART WITH newvalue only restarts the sequence with the specified value, and does not store newvalue as the new initial value of the sequence.A subsequent ALTER SEQUENCE RESTART will use the initial value specified when the sequence was created, and not the newvalue of this statement.This behaviour is specified in the SQL standard.

It is currently not possible to change the initial value stored in the metadata.

Warning

Incorrect use of the ALTER SEQUENCE statement (changing the current value of the sequence or generator) is likely to break the logical integrity of data, or result in primary key or unique constraint violations.

INCREMENT [BY] allows you to change the sequence increment for the NEXT VALUE FOR expression.

Note

Changing the increment value takes effect for all queries that run after the transaction commits.Procedures that are called for the first time after changing the commit, will use the new value if they use NEXT VALUE FOR.Procedures that were already used (and cached in the metadata cache) will continue to use the old increment.You may need to close all connections to the database for the metadata cache to clear, and the new increment to be used.Procedures using NEXT VALUE FOR do not need to be recompiled to see the new increment.Procedures using GEN_ID(gen, expression) are not affected when the increment is changed.

Who Can Alter a Sequence?

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

  • Administrators

  • The owner of the sequence

  • Users with the ALTER ANY SEQUENCE (ALTER ANY GENERATOR) privilege

docnext count = 11

Examples of ALTER SEQUENCE

  1. Setting the value of the EMP_NO_GEN sequence so the next value is 145.

    ALTER SEQUENCE EMP_NO_GEN RESTART WITH 145;
  2. Resetting the base value of the sequence EMP_NO_GEN to the initial value stored in the metadata

    ALTER SEQUENCE EMP_NO_GEN RESTART;
  3. Changing the increment of sequence EMP_NO_GEN to 10

    ALTER SEQUENCE EMP_NO_GEN INCREMENT BY 10;

CREATE OR ALTER SEQUENCE

Creates a sequence if it doesn’t exist, or alters a sequence

Available in

DSQL, ESQL

Syntax
CREATE OR ALTER {SEQUENCE | GENERATOR} seq_name
  {RESTART | START WITH start_value}
  [INCREMENT [BY] increment]
Table 1. CREATE OR ALTER 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.

If the sequence does not exist, it will be created.An existing sequence will be changed:

  • If RESTART is specified, the sequence will restart with the initial value stored in the metadata

  • If the START WITH clause is specified, the sequence is restarted with start_value, but the start_value is not stored.In other words, it behaves as RESTART WITH in [fblangref50-ddl-sequence-alter].

  • If the INCREMENT [BY] clause is specified, increment is stored as the increment in the metadata, and used for subsequent calls to NEXT VALUE FOR

DROP SEQUENCE

Drops a SEQUENCE (GENERATOR)

Available in

DSQL, ESQL

Syntax
DROP {SEQUENCE | GENERATOR} seq_name
Table 1. DROP SEQUENCE Statement Parameter
Parameter Description

seq_name

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

The statements DROP SEQUENCE and DROP GENERATOR statements are equivalent: both drop (delete) an existing sequence (generator).Either is valid but DROP SEQUENCE, being defined in the SQL standard, is recommended.

The statements will fail if the sequence (generator) has dependencies.

Who Can Drop a Sequence?

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

  • Administrators

  • The owner of the sequence

  • Users with the DROP ANY SEQUENCE (DROP ANY GENERATOR) privilege

RECREATE SEQUENCE

Drops a sequence if it exists, and creates a sequence (generator)

Available in

DSQL, ESQL

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

seq_name

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

start_value

Initial value of the sequence

increment

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

See [fblangref50-ddl-sequence-create] for the full syntax of CREATE SEQUENCE and descriptions of defining a sequences and its options.

RECREATE SEQUENCE creates or recreates a sequence.If a sequence with this name already exists, the RECREATE SEQUENCE statement will try to drop it and create a new one.Existing dependencies will prevent the statement from executing.

SET GENERATOR

Sets the current value of a sequence (generator)

Available in

DSQL, ESQL

Syntax
SET GENERATOR seq_name TO new_val
Table 1. SET GENERATOR Statement Parameters
Parameter Description

seq_name

Generator (sequence) name

new_val

New sequence (generator) value.A 64-bit integer from -2-63 to 263-1.

The SET GENERATOR statement sets the current value of a sequence or generator to the specified value.

Note

Although SET GENERATOR is considered outdated, it is retained for backward compatibility.Use of the standards-compliant ALTER SEQUENCE is recommended.

Who Can Use a SET GENERATOR?

The SET GENERATOR statement can be executed by:

  • Administrators

  • The owner of the sequence (generator)

  • Users with the ALTER ANY SEQUENCE (ALTER ANY GENERATOR) privilege

Example of SET GENERATOR

Setting the value of the EMP_NO_GEN sequence to 145:
SET GENERATOR EMP_NO_GEN TO 145;
Note

Similar effects can be achieved with [fblangref50-ddl-sequence-alter]:

ALTER SEQUENCE EMP_NO_GEN
  RESTART WITH 145 + increment;

Here, the value of increment is the current increment of the sequence.We need add it as ALTER SEQUENCE calculates the current value to set based on the next value it should produce.