ALTER SEQUENCE

Synopsis

Use the ALTER SEQUENCE statement to change the definition of an existing sequence in the current schema.

Syntax

  1. alter_sequence ::= ALTER SEQUENCE [ IF EXISTS ] name
  2. alter_sequence_options
  3. name ::= '<Text Literal>'
  4. alter_sequence_options ::= [ AS seq_data_type ]
  5. [ INCREMENT [ BY ] increment ]
  6. [ MINVALUE minvalue | NO MINVALUE ]
  7. [ MAXVALUE maxvalue | NO MAXVALUE ]
  8. [ START [ WITH ] start ]
  9. [ RESTART [ [ WITH ] restart ] ]
  10. [ CACHE cache ]
  11. [ OWNED BY table_name.table_column | NONE ]

alter_sequence

ALTER SEQUENCE - 图1

name

ALTER SEQUENCE - 图2

alter_sequence_options

ALTER SEQUENCE - 图3

Semantics

alter_sequence

ALTER SEQUENCE sequence_name [ IF EXISTS ]

Specify the name of the sequence (sequence_name). An error is raised if a sequence with that name does not exists in the current schema and IF EXISTS is not specified.

sequence_options

AS datatype

Changes the data type of a sequence. This automatically changes the minimum and maximum values of the sequence if the previous values were beyond what the new type allows. Valid types are smallint, integer, and bigint.

INCREMENT BY increment

Specify the difference between consecutive values in the sequence. Default is 1.

MINVALUE minvalue | NO MINVALUE

Specify the minimum value allowed in the sequence. If this value is reached (in a sequence with a negative increment), nextval() will return an error. If NO MINVALUE is specified, the default value will be used. Default is 1.

MAXVALUE maxvalue | NO MAXVALUE

Specify the maximum value allowed in the sequence. If this value is reached, nextval() will return an error. If NO MAXVALUE is specified, the default will be used. Default is 2<sup>63</sup> - 1.

START WITH start

Specify the first value in the sequence. start cannot be less than minvalue. Default is 1.

RESTART [ [ WITH ] restart ] ]

Change the current value of the sequence. If no value is specified, the current value will be set to the last value specified with START [ WITH ] when the sequence was created or altered.

CACHE cache

Specify how many numbers from the sequence to cache in the client. Default is 1.

OWNED BY table_name.table_column | NONE

It gives ownership of the sequence to the specified column (if any). This means that if the column (or the table to which it belongs to) is dropped, the sequence will be automatically dropped. If NONE is specified, any previous ownership will be deleted.

Examples

Create a simple sequence.

  1. yugabyte=# CREATE SEQUENCE s;
  1. CEATE SEQUENCE

Modify the increment value.

  1. yugabyte=# ALTER SEQUENCE s INCREMENT BY 5;
  1. ALTER SEQUENCE
  1. yugabyte=# SELECT nextval('s');

  1. nextval

  1. 1

(1 row)

  1. yugabyte=# SELECT nextval('s');

  1. nextval

  1. 6

(1 row)

Modify the starting value.

  1. yugabyte=# ALTER SEQUENCE s RESTART WITH 2;
  1. ALTER SEQUENCE
  1. yugabyte=# SELECT nextval('s');

  1. nextval

  1. 2

(1 row)

  1. yugabyte=# SELECT nextval('s');

  1. nextval

  1. 7

(1 row)

See also