一尘不染

SQL。如何引用复合主键Oracle?

sql

我有两个父表:TreatmentVisit

处理表:

 create table Treatment (
    TreatCode CHAR(6) constraint cTreatCodeNN not null,
    Name VARCHAR2(20),
    constraint cTreatCodePK primary key (TreatCode),
    );

访问表:

create table Visit (
SlotNum NUMBER(2),
DateVisit DATE,
ActualArrivalTime DATE,
constraint cVisitSlotDatePK primary key (SlotNum, DateVisit)
);

现在,我尝试创建一个子表:

create table Visit_Treat (
TreatCode constraint cTreatCodeFK references Treatment(TreatCode),
SlotNum constraint cSlotNumFK references Visit(SlotNum),
DateVisit constraint cDateFK references Visit(DateVisit),
constraint cVisitTreatPK primary key (SlotNum, TreatCode, DateVisit)
);

一切正常,直到第3行。从第三行开始,即SlotNum constraint ...显示一条消息:no matching unique orprimary key


阅读 206

收藏
2021-03-08

共1个答案

一尘不染

CONSTRAINT fk_column
FOREIGN KEY (column1, column2, … column_n)
REFERENCES parent_table (column1, column2, … column_n)

在你的情况下

create table Visit_Treat (
TreatCode CHAR(6) constraint cTreatCodeFK references Treatment(TreatCode),
SlotNum NUMBER(2),
DateVisit DATE,
constraint cVisitTreatPK primary key (SlotNum, TreatCode, DateVisit),
constraint fk_slotnumDatevisit FOREIGN KEY(SlotNum,DateVisit) 
references Visit(SlotNum,DateVisit)
);
2021-03-08