有没有办法让一个表有2列是外键引用2个其他表?

时间:2016-11-21 05:46:15

标签: sql postgresql foreign-keys

我有3个表Customer,Store和Receipt。 Receipt表必须有一个receiptno,它是主键。 Receipt表还有属性customerid和storeid,如何指定customerid是引用customer表中customerid的外键,storeid是在Stores表中引用storeid的外键?

1 个答案:

答案 0 :(得分: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)   
 );