为特定父表的关系选择所有外键约束defs

时间:2013-12-04 21:18:31

标签: postgresql

我有一个名为users的父表,其中具有多个关系emailsphone_numbersusers表位于名为it的模式中,表emailsphone_numbers位于名为da的模式中。现在我需要查询属于 it.users表的表的所有约束定义。在伪代码中,这将是:

SELECT relational_table_name, constraint_def 
  FROM some_information_tables 
  WHERE parent_schema_name = 'users' 
    AND parent_schema_name = 'it'

我如何在现实世界中这样做?

1 个答案:

答案 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