PostgreSQL:SQL脚本以获取所有外键参考的列表

发布于 2025-02-09 07:45:40 字数 649 浏览 2 评论 0原文

我有一个表格,品种,带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-02-16 07:45:40

您必须将pg_constraint加入pg_attribute并嵌套列库阵列(可以是复合键),以在基础表中获取引用的列名。
您必须使用pg_class获取表名称。
pg_get_constraintdef为您提供了用于创建约束的实际SQL行。

SELECT (select  r.relname from pg_class r where r.oid = c.confrelid) as base_table,
       a.attname as base_col,
       (select r.relname from pg_class r where r.oid = c.conrelid) as referencing_table,
       UNNEST((select array_agg(attname) from pg_attribute where attrelid = c.conrelid and array[attnum] <@ c.conkey)) as referencing_col,
       pg_get_constraintdef(c.oid) contraint_sql
  FROM pg_constraint c join pg_attribute a on c.confrelid=a.attrelid and a.attnum = ANY(confkey)
 WHERE c.confrelid = (select oid from pg_class where relname = 'breeds')
   AND c.confrelid!=c.conrelid;

You have to JOIN pg_constraint to pg_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.

SELECT (select  r.relname from pg_class r where r.oid = c.confrelid) as base_table,
       a.attname as base_col,
       (select r.relname from pg_class r where r.oid = c.conrelid) as referencing_table,
       UNNEST((select array_agg(attname) from pg_attribute where attrelid = c.conrelid and array[attnum] <@ c.conkey)) as referencing_col,
       pg_get_constraintdef(c.oid) contraint_sql
  FROM pg_constraint c join pg_attribute a on c.confrelid=a.attrelid and a.attnum = ANY(confkey)
 WHERE c.confrelid = (select oid from pg_class where relname = 'breeds')
   AND c.confrelid!=c.conrelid;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文