Postgresql

如何列出 DOMAIN 的約束?

  • June 2, 2018

考慮以下場景:

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');

引用自:https://dba.stackexchange.com/questions/83502