pg_get_constraintdef()
是一个用于获取约束定义的系统函数。
pg_get_constraintdef()
在 PostgreSQL 7.3 中被添加。
用法
pg_get_constraintdef (constraint
oid
[,pretty
boolean
] ) → text
返回的定义是从元数据中重构的,而不是原始定义的文本。
变更历史
- PostgreSQL 9.3
- 输出始终缩进 (commit 62e66640)
- PostgreSQL 7.4
- 添加了
pretty
选项 (commit 52347b66)
- 添加了
- PostgreSQL 7.3
- 添加 (commit a208ea72)
示例
使用带有单个约束的表的 pg_get_constraintdef()
用法的简单示例
postgres=# CREATE TABLE foo (id INT NOT NULL CHECK (id BETWEEN 1 AND 2)); CREATE TABLE postgres=# \d foo Table "public.foo" Column | Type | Collation | Nullable | Default --------+---------+-----------+----------+--------- id | integer | | not null | Check constraints: "foo_id_check" CHECK (id >= 1 AND id <= 2)
此查询使用 pg_get_constraintdef()
返回表中所有约束的定义
postgres=# SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid='foo'::regclass; pg_get_constraintdef ----------------------------------- CHECK (((id >= 1) AND (id <= 2))) (1 row)
使用 pretty
选项
postgres=# SELECT pg_get_constraintdef(oid, TRUE) FROM pg_constraint WHERE conrelid='foo'::regclass; pg_get_constraintdef ----------------------------- CHECK (id >= 1 AND id <= 2) (1 row)
参考资料
- PostgreSQL 文档: 系统目录信息函数
分类
另请参阅
pg_constraint, pg_get_functiondef(), pg_get_indexdef(), pg_get_ruledef(), pg_get_statisticsobjdef(), pg_get_triggerdef(), pg_get_viewdef()