ALTER SEQUENCE

ALTER SEQUENCE — change the definition of a sequence generator

Syntax

ALTER SEQUENCE [schema.]sequence [ sequence_option [,... ] ]

where sequence_option can be one of the following:

START WITH value
QUANTUM SIZE size

Description

ALTER SEQUENCE is used to specify a new starting value of an existing sequence. Future values of the sequence will not be less than the value specified. This operation can not be rolled back and its effects are instantly visible to all other transactions.

The ALTER SEQUENCE command can also change the QUANTUM SIZE for the sequence. This is the size of the block of integer values that will be reserved for a transaction engine (TE) to be later assigned.

For details about sequence behavior, see CREATE SEQUENCE.

By default, only the user that creates a sequence database object can ALTER the sequence. This user can GRANT the ALTER privilege on the sequence to other users (see GRANT). The SELECT privilege needs to be granted to other users to SELECT from the sequence.

Parameters

Example

The following example creates a sequence and creates a table that uses the new sequence to generate ID column values.

USE SCHEMA1
CREATE SEQUENCE seq_test START WITH 1;
CREATE TABLE tst_table (id INTEGER GENERATED ALWAYS AS IDENTITY (seq_test), column2 STRING);
INSERT INTO tst_table (column2) VALUES ('row1'),('row2');
SELECT * FROM tst_table;
 ID  COLUMN2  
 --- -------- 
  1    row1   
  2    row2  
 
SELECT NEXT VALUE FOR seq_test FROM DUAL;
 TST_SEQ  
 -------- 
    3   
 
INSERT INTO tst_table (column2) VALUES ('row3'),('row4');
SELECT * FROM tst_table;
 ID  COLUMN2  
 --- -------- 
  1    row1   
  2    row2   
  4    row3   
  5    row4   

The ALTER SEQUENCE command can use the START WITH option to change the new starting value of a sequence, but can only increase it. In the following example, the START WITH option value of one (1) is less than the current value 6. Therefore, the sequence is not altered.

ALTER SEQUENCE seq_test START WITH 1000;
SELECT NEXT VALUE FOR seq_test FROM DUAL;
 SEQ_TEST  
 --------- 
   1001     

The ALTER SEQUENCE command is not transactional. Its effects become visible immediately to all transactions and can't be rolled back.