这是两个现有表的结构:
表格:位置
-------------------------
PK varchar | Symbol
PK int | LocationID
FK int | Shop
- varchar | Address
信息:
Composite key of: Symbol and LocationID
Foreign key to Shop's table: Shop
表格:商店
-------------------------
PK varchar | Symbol
PK int | ShopID
timestamp | SomeDate
信息:
Composite key of: Symbol and ShopID
这是我可以联接两个表的方式:
SELECT * FROM Location As location
INNER JOIN Shop As shop
ON location.Shop = Shop.ShopID AND location.Symbol = Shop.Symbol
但是在Postgres中,我需要在这些表之间建立关系。在ER图的DbEaver中,没有绘制关系的图形可能性。
创建这种关系的命令是什么?
答案 0 :(得分:1)
As documented in the manual,您声明外键约束:
alter table location
add constraint fk_location_shop
foreign key (shop, symbol)
references shop (shopid, symbol);
外键的列必须包括所引用表的主键的所有列。这就是为什么您需要foreign key (shop, symbol)