Purpose
This statement is used to change the increment, minimum and maximum values, cache size, and behavior of an existing sequence. The changes only affect subsequent sequence numbers.
Note
Users can only modify sequences in their own schema or those for which they have the ALTER object privilege.
Syntax
ALTER SEQUENCE [ schema. ] sequence_name
[ INCREMENT BY int_value ]
[ START WITH int_value ]
[ MINVALUE int_value | NOMINVALUE ]
[ MAXVALUE int_value | NOMAXVALUE ]
[ CACHE int_value | NOCACHE ]
[ ORDER | NOORDER ]
[ CYCLE | NOCYCLE ]
[ RESTART ]
Parameters
| Parameter | Description |
|---|---|
| schema. | The schema that contains the sequence. If you omit the schema, the database changes the sequence in the schema where the current user is located. |
| sequence_name | The name of the sequence to be changed. |
| RESTART | Resets the sequence value. When ascending, it starts from MINVALUE; when descending, it starts from MAXVALUE. |
| START WITH int_value | The starting value of the sequence. The value of int_value ranges from [-(1027-1) ~ (1028-1)]. |
| MINVALUE int_value | The minimum value of the sequence. The value of int_value ranges from [-(1027-1) ~ (1027-1)]. |
| NOMINVALUE | If you specify NOMINVALUE, the minimum value is 1 when ascending and -(1027-1) when descending. |
| MAXVALUE int_value | The maximum value of the sequence. The value of int_value ranges from [(-1027+1) ~ (1028-1)]. The new MAXVALUE must be greater than the current sequence value. Note When both MINVALUE and MAXVALUE are specified, MINVALUE must be less than MAXVALUE. The difference between MAXVALUE and MINVALUE must be greater than or equal to the int_value specified in INCREMENT BY. |
| NOMAXVALUE | If you change it to NOMAXVALUE, the maximum value when the sequence is ascending is (1028-1), and the maximum value when the sequence is descending is -1. |
| INCREMENT BY int_value | Changes the increment step of the sequence. int_value cannot be 0. If you specify a positive number, the sequence is ascending; if you specify a negative number, the sequence is descending. The default value is 1 if you do not specify it. |
| CACHE int_value | Changes the number of increment values that are preallocated in memory. The default value of int_value is 20. The value of CACHE int_value must be greater than 1. If the value of CACHE INT_VALUE is 1, it is equivalent to NOCACHE. |
| NOCACHE | Changes the sequence value to not be preallocated. If you omit both CACHE and NOCACHE, the database preallocates 20 sequence numbers by default. |
| ORDER | Changes the sequence value to be generated in order. |
| NOORDER | If you change it to NOORDER, the sequence value is not guaranteed to be generated in order. |
| CYCLE | Changes the sequence value to be generated in a cycle. That is, the sequence continues to generate values after it reaches its maximum or minimum value. When the sequence reaches its maximum value in ascending order, it generates the minimum value. When the sequence reaches its minimum value in descending order, it generates the maximum value. Note
|
| NOCYCLE | NOCYCLE is the default value. If you change it to NOCYCLE, the sequence cannot generate more values after it reaches its maximum or minimum value. |
Examples
Modify the maximum value of the
seq1sequence and specify that the increment value is recycled.-- Create the sequence first: CREATE SEQUENCE seq1; obclient> ALTER SEQUENCE seq1 MAXVALUE 1024 CYCLE;Change the maximum value and increment value of the
seq3sequence, enableCYCLEandORDER, and specifyNOCACHEto indicate that the sequence values are not preallocated.-- Create the sequence first: CREATE SEQUENCE seq3; obclient> ALTER SEQUENCE seq3 MAXVALUE 1500 INCREMENT BY 5 CYCLE NOCACHE ORDER;Reset the sequence value.
-- Create the sequence first: CREATE SEQUENCE seq3; obclient> ALTER SEQUENCE seq3 RESTART;Set the starting value and reset the sequence.
-- Create the sequence first: CREATE SEQUENCE seq3; obclient> ALTER SEQUENCE seq3 START WITH 3 RESTART;The
RESTARToption can be placed anywhere in the clause. The following two SQL statements for resetting the sequence value are equivalent.-- Create the sequence first: CREATE SEQUENCE seq3; obclient> ALTER SEQUENCE seq3 START WITH 3 RESTART MINVALUE -100 CYCLE; obclient> ALTER SEQUENCE seq3 RESTART START WITH 3 MINVALUE -100 CYCLE;
