【问题标题】:Is there a way to have a table having 2 columns that are foreign keys referencing 2 other tables?有没有办法让一个表有 2 列是引用其他 2 个表的外键?
【发布时间】:2016-11-21 14:53:20
【问题描述】:
我有 3 张表 Customer、Store 和 Receipt。 Receipt 表必须有一个receiptno,它是主键。 Receipt 表还有属性 customerid 和 storeid,我如何指定 customerid 是引用 customer 表中 customerid 的外键,而 storeid 是引用 Stores 表中 storeid 的外键?
【问题讨论】:
标签:
sql
postgresql
foreign-keys
【解决方案1】:
create table Customer (customerid int primary key);
create table Store (storeid int primary key);
create table Receipt
(
receiptno int primary key
,customerid int references Customer (customerid)
,storeid int references Store (storeid)
);