【问题标题】: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)   
 );

【讨论】:

    猜你喜欢
    • 2020-07-21
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 2020-12-30
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多