Postgresql
如何列出 DOMAIN 的約束?
考慮以下場景:
CREATE DOMAIN dom_zipcode AS text; ALTER DOMAIN dom_zipcode ADD CONSTRAINT zipchk CHECK (char_length(VALUE) = 5);
現在,如果我想用 放棄該約束
ALTER DOMAIN
,手冊說:ALTER DOMAIN name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ]
但是我們怎麼能找到
constraint_name
呢?\dD
僅顯示約束的定義(CHECK
語句)。\dD dom_zipcode ; List of domains Schema | Name | Type | Modifier | Check --------+-------------+------+----------+-------------------------------- public | dom_zipcode | text | | CHECK (char_length(VALUE) = 5) (1 row)
我可以使用 轉儲模式
pg_dump
,但我相信必須存在一種更優雅的方式來使用psql
終端建立它。
PostgreSQL 的系統目錄
SELECT conname FROM pg_constraint WHERE contypid = 'dom_zipcode'::regtype;
SQL 規範
INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS
也可以在
domain_constraints
表中查詢information_schema
SELECT FORMAT('%I.%I.%I', constraint_schema, constraint_catalog, constraint_name) FROM information_schema.domain_constraints WHERE (domain_catalog,domain_schema,domain_name) = ('test','public','dom_zipcode');