ALTER SEQUENCE

On this page Carat arrow pointing down

The ALTER SEQUENCE statement applies a schema change to a sequence.

Note:

This page documents all supported sequence changes except for changing the name of a sequence and changing the schema of a sequence. For information about changing the name of a sequence, see RENAME SEQUENCE. For information about changing the schema of a sequence, see SET SCHEMA.

Note:

This statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Required privileges

The user must have the CREATE privilege on the parent database.

Synopsis

ALTER SEQUENCE IF EXISTS sequence_name NO CYCLE MINVALUE MAXVALUE OWNED BY NONE column_name INCREMENT BY MINVALUE MAXVALUE START WITH integer VIRTUAL

Parameters

Parameter Description
IF EXISTS Modify the sequence only if it exists; if it does not exist, do not return an error.
sequence_name The name of the sequence you want to modify.
INCREMENT The new value by which the sequence is incremented. A negative number creates a descending sequence. A positive number creates an ascending sequence.
MINVALUE The new minimum value of the sequence.

Default: 1
MAXVALUE The new maximum value of the sequence.

Default: 9223372036854775807
START The value the sequence starts at if you RESTART or if the sequence hits the MAXVALUE and CYCLE is set.

RESTART and CYCLE are not implemented yet.
CYCLE The sequence will wrap around when the sequence value hits the maximum or minimum value. If NO CYCLE is set, the sequence will not wrap.
OWNED BY column_name Associates the sequence to a particular column. If that column or its parent table is dropped, the sequence will also be dropped.

Specifying an owner column with OWNED BY replaces any existing owner column on the sequence. To remove existing column ownership on the sequence and make the column free-standing, specify OWNED BY NONE.

Default: NONE

Examples

Change the increment value of a sequence

In this example, we're going to change the increment value of a sequence from its current state (i.e., 1) to 2.

icon/buttons/copy
> CREATE SEQUENCE customer_seq;
icon/buttons/copy
> SHOW CREATE customer_seq;
   table_name  |                                     create_statement
---------------+-------------------------------------------------------------------------------------------
  customer_seq | CREATE SEQUENCE customer_seq MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1
(1 row)
icon/buttons/copy
> ALTER SEQUENCE customer_seq INCREMENT 2;
   table_name  |                                        create_statement
---------------+--------------------------------------------------------------------------------------------------
  customer_seq | CREATE SEQUENCE public.customer_seq MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 2 START 1
(1 row)

See also


Yes No
On this page

Yes No