DROP SEQUENCE

Synopsis

Use the DROP SEQUENCE statement to delete a sequence in the current schema.

Syntax

  1. drop_sequence ::= DROP SEQUENCE [ IF EXISTS ] sequence_name
  2. [ CASCADE | RESTRICT ]

drop_sequence

DROP SEQUENCE - 图1

Semantics

sequence_name

Specify the name of the sequence.

  • An error is raised if a sequence with that name does not exist in the current schema unless IF EXISTS is specified.
  • An error is raised if any object depends on this sequence unless the CASCADE option is specified.

CASCADE

Remove also all objects that depend on this sequence (for example a DEFAULT value in a table’s column).

RESTRICT

Do not remove this sequence if any object depends on it. This is the default behavior even if it’s not specified.

Examples

Dropping a sequence that has an object depending on it, fails.

  1. yugabyte=# CREATE TABLE t(k SERIAL, v INT);
  1. CREATE TABLE
  1. \d t
  1. Table "public.t"
  2. Column | Type | Collation | Nullable | Default
  3. --------+---------+-----------+----------+------------------------------
  4. k | integer | | not null | nextval('t_k_seq'::regclass)
  5. v | integer | | |
  1. yugabyte=# DROP SEQUENCE t_k_seq;
  1. ERROR: cannot drop sequence t_k_seq because other objects depend on it
  2. DETAIL: default for table t column k depends on sequence t_k_seq
  3. HINT: Use DROP ... CASCADE to drop the dependent objects too.

Dropping the sequence with the CASCADE option solves the problem and also deletes the default value in table t.

  1. yugabyte=# DROP SEQUENCE t_k_seq CASCADE;
  1. NOTICE: drop cascades to default for table t column k
  2. DROP SEQUENCE
  1. \d t
  1. Table "public.t"
  2. Column | Type | Collation | Nullable | Default
  3. --------+---------+-----------+----------+---------
  4. k | integer | | not null |
  5. v | integer | | |

See also