0

想象一下,你有两张桌子。每个表都有很多列。

我没有创建数据库,所以我真的不知道哪些列是相关的。一个问题是两个表之间没有相同的列名,所以我无法通过列名弄清楚。

如何找到与两个不同表相关的列?

4

1 回答 1

1

如果表之间有外键,那么你可以找到它们之间的关系。为此,您可以调用\d表并查看与其列关联的外键。如果没有显式的外键,则无法确定表之间的关系,除非与开发人员和/或技术决策者交谈。

外键关系如何显示的示例psql

postgres=# alter table pgbench_accounts add constraint "pgb_accounts_branches_fk" foreign key (bid) references pgbench_branches (bid);
ALTER TABLE
postgres=# \d pgbench_accounts
              Table "public.pgbench_accounts"
  Column  |     Type      | Collation | Nullable | Default 
----------+---------------+-----------+----------+---------
 aid      | integer       |           | not null | 
 bid      | integer       |           |          | 
 abalance | integer       |           |          | 
 filler   | character(84) |           |          | 
Indexes:
    "pgbench_accounts_pkey" PRIMARY KEY, btree (aid)
Foreign-key constraints:
    "pgb_accounts_branches_fk" FOREIGN KEY (bid) REFERENCES pgbench_branches(bid)

postgres=# \d pgbench_branches
              Table "public.pgbench_branches"
  Column  |     Type      | Collation | Nullable | Default 
----------+---------------+-----------+----------+---------
 bid      | integer       |           | not null | 
 bbalance | integer       |           |          | 
 filler   | character(88) |           |          | 
Indexes:
    "pgbench_branches_pkey" PRIMARY KEY, btree (bid)
Referenced by:
    TABLE "pgbench_accounts" CONSTRAINT "pgb_accounts_branches_fk" FOREIGN KEY (bid) REFERENCES pgbench_branches(bid)

postgres=# 
于 2020-03-05T19:02:24.897 回答