The RENAME TO
statement is part of ALTER SEQUENCE
, and changes the name of a sequence.
You cannot rename a sequence that's being used in a table. To rename the sequence, drop the DEFAULT
expressions that reference the sequence, rename the sequence, and add the DEFAULT
expressions back.
ALTER SEQUENCE ... RENAME TO
can be used to move a sequence from one database to another, but it cannot be used to move a sequence from one schema to another. To change a sequence's schema, use SET SCHEMA
.
Note that, in a future release, ALTER SEQUENCE ... RENAME TO
will be limited to changing the name of a sequence, and will not have to the ability to change a sequence's database.
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
Name changes do not propagate to the table(s) using the sequence. ## Examples ### Rename a Sequence In this example, we will change the name of sequence.
> CREATE SEQUENCE even_numbers INCREMENT 2 START 2;
> SHOW SEQUENCES;
sequence_schema | sequence_name
------------------+----------------
public | even_numbers
(1 row)
> ALTER SEQUENCE even_numbers RENAME TO even_sequence;
> SHOW SEQUENCES;
sequence_schema | sequence_name
------------------+----------------
public | even_sequence
(1 row)
Move a Sequence
In this example, we will move the sequence we renamed in the first example (even_sequence
) from defaultdb
(i.e., the default database) to a different database.
> SHOW SEQUENCES FROM defaultdb;
sequence_schema | sequence_name
------------------+----------------
public | even_sequence
(1 row)
> CREATE DATABASE mydb;
> ALTER SEQUENCE even_sequence RENAME TO newdb.even_sequence;
> SHOW SEQUENCES FROM defaultdb;
sequence_schema | sequence_name
------------------+----------------
(0 rows)
> SHOW SEQUENCES FROM mydb;
sequence_schema | sequence_name
------------------+----------------
public | even_sequence
(1 row)