DROP TYPE 是一个用于删除自定义数据类型的 DDL 命令。
DROP TYPE 始终存在于 PostgreSQL 中。
变更历史
- PostgreSQL 8.2
- 添加了
IF EXISTS子句(提交 daea4d8e)
- 添加了
- PostgreSQL 7.3
- 添加了
CASCADE和RESTRICT子句(提交 7c6df91d)
- 添加了
- PostgreSQL 7.1
- 可以使用同一个
DROP TYPE命令删除多个类型(提交 9ace0318)
- 可以使用同一个
示例
基本的 DROP TYPE 执行示例
postgres=# DROP TYPE unused_type; DROP TYPE
删除带有依赖项的类型
postgres=# DROP TYPE some_enum_type; ERROR: cannot drop type some_enum_type because other objects depend on it DETAIL: column thing_type of table thing depends on type some_enum_type postgres=# DROP TYPE some_enum_type CASCADE; NOTICE: drop cascades to column thing_type of table thing DROP TYPE
尝试删除不存在的类型
postgres=# DROP TYPE no_such_type; ERROR: type "no_such_type" does not exist
安全地尝试删除可能不存在的类型
postgres=# DROP TYPE IF EXISTS no_such_type; NOTICE: type "no_such_type" does not exist, skipping DROP TYPE
无法删除 PostgreSQL 的基本类型
postgres=# DROP TYPE pg_lsn; ERROR: cannot drop type pg_lsn because it is required by the database system
参考资料
- PostgreSQL 文档: DROP TYPE
