Postgresql查询获取外键多列

时间:2017-09-04 07:42:23

标签: postgresql

我有这个架构:

create table "cat" (
    "name" varchar(64),
    "owner" varchar(64),
    primary key ("name", "owner")
);

create table "comment" (
    "name" varchar(45),
    "owner" varchar(45),
    "id" uuid,
    "comment" text,
    primary key ("id"),
    foreign key ("name", "owner") references "cat"("name", "owner")
);

我想从表格中获取外键列表"评论"到" cat",所以我用:

SELECT
    tc.constraint_name, tc.table_name, kcu.column_name, 
    ccu.table_name AS foreign_table_name,
    ccu.column_name AS foreign_column_name 
FROM 
    information_schema.table_constraints AS tc 
    JOIN information_schema.key_column_usage AS kcu
      ON tc.constraint_name = kcu.constraint_name
    JOIN information_schema.constraint_column_usage AS ccu
      ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY' 

;

得到了我想要的东西:

  constraint_name  | table_name | column_name | foreign_table_name | foreign_column_name 
-------------------+------------+-------------+--------------------+---------------------
 comment_name_fkey | comment    | owner       | cat                | name
 comment_name_fkey | comment    | name        | cat                | name
 comment_name_fkey | comment    | owner       | cat                | owner
 comment_name_fkey | comment    | name        | cat                | owner

但是有第1行和第4行,我想在结果中消除它,因为它不会镜像列的依赖关系。我怎么能在Postgresql中做到这一点?

3 个答案:

答案 0 :(得分:1)

我认为你不能只使用information_schema,但你可以直接查询表:

SELECT conname AS constraint_name, conrelid::regclass AS table_name, ta.attname AS column_name,
       confrelid::regclass AS foreign_table_name, fa.attname AS foreign_column_name
  FROM (
   SELECT conname, conrelid, confrelid,
          unnest(conkey) AS conkey, unnest(confkey) AS confkey
     FROM pg_constraint
    WHERE conname = 'comment_name_fkey'
      --and contype = 'f'
  ) sub
  JOIN pg_attribute AS ta ON ta.attrelid = conrelid AND ta.attnum = conkey
  JOIN pg_attribute AS fa ON fa.attrelid = confrelid AND fa.attnum = confkey

结果

  constraint_name  | table_name | column_name | foreign_table_name | foreign_column_name
-------------------+------------+-------------+--------------------+---------------------
 comment_name_fkey | comment    | name        | cat                | name
 comment_name_fkey | comment    | owner       | cat                | owner

答案 1 :(得分:1)

我认为你应该选择一个整体引用中使用的列名列表:

0.0000025

但是,不确定列名的顺序是否正确,这取决于它们在information_schema中列出的方式。

更可靠的解决方案是查询system catalog pg_constraint. 函数SELECT tc.constraint_name, tc.table_name, string_agg(distinct kcu.column_name, ', ') AS column_names, ccu.table_name AS foreign_table_name, string_agg(distinct ccu.column_name, ', ') AS foreign_column_names FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = 'comment' GROUP BY 1, 2, 4; constraint_name | table_name | column_names | foreign_table_name | foreign_column_names -------------------+------------+--------------+--------------------+---------------------- comment_name_fkey | comment | name, owner | cat | name, owner (1 row) 在此处定义:List all foreign keys PostgresSQL.

get_col_names()

答案 2 :(得分:0)

referential_constraints.unique_constraint_*key_column_usage.ordinal_position列可用于将外部列正确地连接到它们的引用列。在https://stackoverflow.com/a/48824659/9093051中查看此答案。

下面是我的精简版本:

SELECT
  rc.constraint_schema,
  rc.constraint_name,
  kcu.table_name,
  kcu.column_name,
  rcu.table_name AS referenced_table,
  rcu.column_name AS referenced_column
FROM information_schema.referential_constraints rc
LEFT JOIN information_schema.key_column_usage kcu
  ON rc.constraint_catalog = kcu.constraint_catalog
  AND rc.constraint_schema = kcu.constraint_schema
  AND rc.constraint_name = kcu.constraint_name
LEFT JOIN information_schema.key_column_usage rcu -- referenced columns
  ON rc.unique_constraint_catalog = rcu.constraint_catalog
  AND rc.unique_constraint_schema = rcu.constraint_schema
  AND rc.unique_constraint_name = rcu.constraint_name
  AND rcu.ordinal_position = kcu.position_in_unique_constraint;

编辑修复了ordinal_position的加入条件