gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:30:52 25 4
gpt4 key购买 nike

我有两个父表: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 行,一切都执行得很好。从第 3 行开始,即 SlotNum 约束... 有一条消息:没有匹配的唯一键或主键。已经有一个 similar question ,但我不太明白适用于我的案例的逻辑。我一一引用每一列,它适用于 Treatment 表父级。我应该如何正确引用Visit表父项?

最佳答案

 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)
);

关于SQL。 Oracle如何引用复合主键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17204845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com