我有一个名为users
的父表,其中具有多个关系emails
和phone_numbers
。 users
表位于名为it
的模式中,表emails
和phone_numbers
位于名为da
的模式中。现在我需要查询属于 it.users
表的表的所有约束定义。在伪代码中,这将是:
SELECT relational_table_name, constraint_def
FROM some_information_tables
WHERE parent_schema_name = 'users'
AND parent_schema_name = 'it'
我如何在现实世界中这样做?
答案 0 :(得分:1)
知道了:
SELECT c.relname as relation, c2.relname as parent, pg_get_constraintdef(cons.oid) as condef
FROM pg_class c
LEFT OUTER JOIN pg_constraint cons ON cons.conrelid = c.oid
LEFT OUTER JOIN pg_class c2 ON cons.confrelid = c2.oid
WHERE c.relkind = 'r'
AND (cons.contype = 'f')
AND c2.relname in ('users')
GROUP BY relation, parent, condef