Modify a sequence

2023-07-21 09:11:01  Updated

You can use the ALTER SEQUENCE statement to modify a sequence. This topic describes how to modify a sequence.

Syntax

This statement is used to modify the increment step, minimum value, maximum value, cache size, and action of the current sequence. The modification affects only subsequent sequence values.

Note

You can modify only sequences in your schema or those for which you have the ALTER privilege.

ALTER SEQUENCE [ schema. ] sequence_name
{ RESTART [ START WITH int_value ]
|[ MINVALUE int_value | NOMINVALUE ]
|[ MAXVALUE int_value | NOMAXVALUE ]
|[ INCREMENT BY int_value ]
|[ CACHE int_value | NOCACHE ]
|[ ORDER | NOORDER ]
|[ CYCLE | NOCYCLE]
};

Parameters

Field Description
schema. The schema where the sequence to be created resides. If this parameter is not specified, the database modifies the sequence in the schema of the current user.
sequence_name The name of the sequence to be modified.
RESTART Restarts the generation of sequence values. When the sequence ascends, new values start from MINVALUE. When the sequence descends, new values start from MAXVALUE. Or, START WITH int_value is used to generate values starting from int_value.
MINVALUE int_value The minimum value of the sequence. The value range of int_value is [-9223372036854775808,9223372036854775807].
NOMINVALUE If NOMINVALUE is specified, the minimum value is 1 when the sequence ascends, and is -9223372036854775808 when the sequence descends.
MAXVALUE int_value The maximum value of the sequence. The value range of int_value is [-9223372036854775808,9223372036854775807]. The specified MAXVALUE value cannot be smaller than the current sequence value.
Notice
  • When MINVALUE and MAXVALUE are both specified, MINVALUE must be smaller than MAXVALUE.
  • The difference between MAXVALUE and MINVALUE must be greater than or equal to INCREMENT BY int_value.
NOMAXVALUE If NOMAXVALUE is specified, the maximum value is 9223372036854775807 when the sequence ascends or -1 when the sequence descends.
INCREMENT BY int_value The increment step of the sequence. int_value cannot be 0. If the value is positive, the sequence ascends. If the value is negative, the sequence descends. If this parameter is not specified, the default value is 1.
CACHE int_value The number of sequence values pre-allocated in the memory. By default, 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 Specifies that no sequence values are pre-allocated. If neither CACHE nor NOCACHE is specified, the database caches 20 sequence values by default.
ORDER Specifies that sequence values are generated in order.
NOORDER If NOORDER is specified, it is not assured that sequence values are generated in order.
CYCLE Specifies that sequence values are generated cyclically. That is, when the sequence reaches its maximum or minimum value, it continues to generate values. If the sequence ascends and reaches its maximum value, the minimum sequence value will be generated. If the sequence descends and reaches its minimum value, the maximum sequence value will be generated.
Notice
  • If CYCLE is specified and the value of INCREMENT BY int_value is smaller than 0, MINVALUE must be specified.
  • If CYCLE is specified, the number of values in the cache`` cannot be greater than the number of values in the cycle.
NOCYCLE NOCYCLE is the default value. If NOCYCLE is specified, no more values are generated when the sequence reaches its maximum or minimum value.

Examples

  1. View the maximum value of the seq1 sequence.

    obclient> SELECT sequence_name,max_value FROM oceanbase.__all_sequence_object WHERE sequence_name='seq1';
    +---------------+-----------+
    | sequence_name | max_value |
    +---------------+-----------+
    | seq1          |         5 |
    +---------------+-----------+
    1 row in set
    
  2. Modify the maximum value of the sequence to 1024.

    obclient> ALTER SEQUENCE seq1 MAXVALUE 1024 CYCLE;
    Query OK, 0 rows affected
    
  3. Query the maximum value of the seq1 sequence again.

    obclient> SELECT sequence_name,max_value FROM oceanbase.__all_sequence_object WHERE sequence_name='seq1';
    +---------------+-----------+
    | sequence_name | max_value |
    +---------------+-----------+
    | seq1          |      1024 |
    +---------------+-----------+
    1 row in set
    

Contact Us