DROP SEQUENCE
是用于删除序列的 DDL 命令。
DROP SEQUENCE
添加于 PostgreSQL 6.4。
更改历史记录
- PostgreSQL 8.2
- 添加了
IF EXISTS
子句(提交 daea4d8e)
- 添加了
- PostgreSQL 7.3
- 添加了
CASCADE
和RESTRICT
子句(提交 7c6df91d)
- 添加了
- PostgreSQL 6.1
- 添加(提交 9729f6ca)
示例
DROP SEQUENCE
的基本用法示例
postgres=# DROP SEQUENCE foo_id_seq; DROP SEQUENCE
删除可能不存在的序列
postgres=# DROP SEQUENCE IF EXISTS foo_id_seq; NOTICE: sequence "foo_id_seq" does not exist, skipping DROP SEQUENCE
尝试删除与表列关联的序列
postgres=# CREATE TABLE bar (id SERIAL); CREATE TABLE postgres=# \d bar Table "public.bar" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------------------------------- id | integer | | not null | nextval('bar_id_seq'::regclass) postgres=# DROP SEQUENCE bar_id_seq; ERROR: cannot drop sequence bar_id_seq because other objects depend on it DETAIL: default value for column id of table bar depends on sequence bar_id_seq HINT: Use DROP ... CASCADE to drop the dependent objects too.
强制删除序列及其依赖项
postgres=# DROP SEQUENCE bar_id_seq CASCADE; NOTICE: drop cascades to default value for column id of table bar DROP SEQUENCE postgres=# \d bar Table "public.bar" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- id | integer | | not null |
参考文献
- PostgreSQL 文档: DROP SEQUENCE