我有一个名为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'
在现实世界里我该怎么做?
最佳答案
知道了。
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
关于postgresql - 为特定父表的关系选择所有外键约束defs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20386186/