PostgreSQL:SQL脚本以获取所有外键参考的列表
我有一个表格,品种
,带有breed_name
的主要键,并希望获取参考breeds
的所有表,列和约束列表不论breeds
中引用的列如何。如果还有另一个表,则cats
,其约束如下:
CREATE TABLE cats
(
cat_name text,
cat_breed text,
CONSTRAINT cat_breed_name
FOREIGN KEY (cat_breed) REFERENCES breeds(breed_name)
)
我应该返回一行,如下:
base_table base_col referencing_table referencing_col constraint_sql
breeds breed_name cats cat_breed CONSTRAINT cat_breed_name FOREIGN KEY (cat_breed) REFERENCES breeds(breed_name)
非主要密钥参考文献也应列出,并且应该处理复合键。
I have a table, breeds
with a primary key of breed_name
and would like to get a list of all tables, columns and constraints that reference breeds
regardless of column referenced in breeds
. If there is another table, cats
and that has a constraint as follows:
CREATE TABLE cats
(
cat_name text,
cat_breed text,
CONSTRAINT cat_breed_name
FOREIGN KEY (cat_breed) REFERENCES breeds(breed_name)
)
I should get back a row like the following:
base_table base_col referencing_table referencing_col constraint_sql
breeds breed_name cats cat_breed CONSTRAINT cat_breed_name FOREIGN KEY (cat_breed) REFERENCES breeds(breed_name)
Non-primary key references should also be listed and it should handle compound keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将
pg_constraint
加入pg_attribute
并嵌套列库阵列(可以是复合键),以在基础表中获取引用的列名。您必须使用
pg_class
获取表名称。pg_get_constraintdef
为您提供了用于创建约束的实际SQL行。You have to JOIN
pg_constraint
topg_attribute
and un nest the column arrays (could be compound keys) to get the referenced column name in the base table.You have to use
pg_class
to get the table names.pg_get_constraintdef
gives you the actual SQL line that is used to create the constraint.