我有一个名为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'

在现实世界里我该怎么做?

最佳答案

知道了。

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/

10-13 04:32