主键由两个外键组成?神谕

时间:2013-05-05 05:43:36

标签: sql oracle database-design foreign-keys primary-key

我对表创建有疑问。我想将“Ono”和“Pno”的属性组合成新表的主键。这些都是外键,每个都来自不同的表。我只使用CONSTRAINT Ono_Pno_PK PRIMARY KEY(Ono,Pno)吗?

到目前为止我用过的东西:

CREATE TABLE ODetails
(
    Ono Number Not Null,
    Pno Number Not Null,
    Qty Number(3) Not Null,
    Creation_Date Date Not Null,
    Created_By VARCHAR(10) Not Null,
    Last_Update_Date Date Not Null,
    Last_Updated_By VARCHAR2(10) Not Null,
    CONSTRAINT Ono_FK FOREIGN KEY (Ono) REFERENCES Orders (Ono),
    CONSTRAINT Pno_FK FOREIGN KEY (Pno) REFERENCES Parts (Pno)
);

1 个答案:

答案 0 :(得分:6)

只需在约束后添加此行,

CONSTRAINT tb_PK PRIMARY KEY (Ono, Pno)