RENAME SEQUENCE

On this page Carat arrow pointing down

The RENAME TO statement is part of ALTER SEQUENCE, and changes the name of a sequence.

Warning:

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.

Note:

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.

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 current_name RENAME TO new_name
## Parameters Parameter | Description -----------|------------ `IF EXISTS` | Rename the sequence only if it exists; if it does not exist, do not return an error. `current_name` | The current name of the sequence you want to modify. `new_name` | The new name of the sequence, which must be unique to its database and follow these [identifier rules](keywords-and-identifiers.html#identifiers).

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.
icon/buttons/copy
> CREATE SEQUENCE even_numbers INCREMENT 2 START 2;
icon/buttons/copy
> SHOW SEQUENCES;
  sequence_schema | sequence_name
------------------+----------------
  public          | even_numbers
(1 row)
icon/buttons/copy
> ALTER SEQUENCE even_numbers RENAME TO even_sequence;
icon/buttons/copy
> 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.

icon/buttons/copy
> SHOW SEQUENCES FROM defaultdb;
  sequence_schema | sequence_name
------------------+----------------
  public          | even_sequence
(1 row)
icon/buttons/copy
> CREATE DATABASE mydb;
icon/buttons/copy
> ALTER SEQUENCE even_sequence RENAME TO newdb.even_sequence;
icon/buttons/copy
> SHOW SEQUENCES FROM defaultdb;
  sequence_schema | sequence_name
------------------+----------------
(0 rows)
icon/buttons/copy
> SHOW SEQUENCES FROM mydb;
  sequence_schema | sequence_name
------------------+----------------
  public          | even_sequence
(1 row)

See also


Yes No
On this page

Yes No