Answers:
可以通过检索约束pg_catalog.pg_constraint
。
SELECT con.*
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp
ON nsp.oid = connamespace
WHERE nsp.nspname = '<schema name>'
AND rel.relname = '<table name>';
用<schema name>
您的模式<table name>
名称和表名称替换。
pg_catalog.pg_constraint
不包含NOT NULL
约束。
在psql
命令行中,此信息位于通过\d+
命令获得的表格中。d+
还告知NOT NULL
约束,pg_catalog.pg_constraint
表中未包含某些内容。一个例子:
# \d+ observations.stream
Table "observations.stream"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+-------------------+-----------+----------+---------+----------+--------------+---------------------------------------------
id | integer | | not null | | plain | |
name | character varying | | not null | | extended | | This should be a table in the import schema
min_id | integer | | not null | | plain | |
about | character varying | | not null | | extended | |
Indexes:
"stream_pkey" PRIMARY KEY, btree (id)
"stream_name_key" UNIQUE CONSTRAINT, btree (name)
Check constraints:
"stream_id_check" CHECK (id > 0)
Referenced by:
TABLE "profile" CONSTRAINT "profile_id_stream_fkey" FOREIGN KEY (id_stream) REFERENCES stream(id)
需要注意的是,您不能通过这种方式获得所有约束的名称。