以下是2个表格。错误出现在表“Medical_Record”的最后两行:
CREATE TABLE Medical_Record(
Patient_SSN VARCHAR(10),
Record_ID VARCHAR(10),
Medical_Issue VARCHAR(45),
Medical_Status VARCHAR(15),
Doctor_ID VARCHAR(10),
Hosp_ID VARCHAR(10),
Event_Date DATE,
PRIMARY KEY (Patient_SSN,Record_ID),
FOREIGN KEY (Patient_SSN) REFERENCES Citizen(SSN) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (Doctor_ID,Hosp_ID) REFERENCES Employment_Status(Worker_ID) ON DELETE CASCADE ON UPDATE CASCADE,
);
CREATE TABLE Employment_Status(
Citizen_SSN VARCHAR(10),
Worker_ID VARCHAR(10),
Org_ID VARCHAR(10),
Employee_Position VARCHAR(15),
Join_Date DATE,
Leave_Date DATE,
Profession VARCHAR(30),
PRIMARY KEY (Citizen_SSN,Worker_ID,Org_ID),
FOREIGN KEY (Citizen_SSN) REFERENCES Citizen(SSN) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (Org_ID) REFERENCES Organization(Org_ID) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE Citizen(
SSN VARCHAR(10),
FName VARCHAR(15),
MName VARCHAR(15),
LName VARCHAR(15),
Gender VARCHAR(1),
Birth_Date DATE,
Death_Date DATE,
Spouse_SSN VARCHAR(10),
Blood_Group VARCHAR(5),
Pincode VARCHAR(7),
Address TEXT,
PRIMARY KEY (SSN),
FOREIGN KEY (Spouse_SSN) REFERENCES Citizen(SSN) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE Organization(
Org_ID VARCHAR(10),
Org_Name VARCHAR(45),
Email VARCHAR(45),
Phone_Number VARCHAR(15),
Established_Date DATE,
Closing_Date DATE,
PRIMARY KEY (Org_ID)
);
答案 0 :(得分:0)
<强> UPDATE1 强>:
下面是执行sql脚本的实际错误。
您在Employee_Status
表中定义了一个复合键
每列定义单个键,然后在子表中引用
它应该工作。
PRIMARY KEY (Citizen_SSN,Worker_ID,Org_ID),
KEY (Worker_ID), -- <-------- Add this line
KEY (Org_ID), -- <-------- Add this line
原始答案:
每个引用的父字段都必须在其上定义index
。这是外键添加的条件。
根据外键约束的文档:
REFERENCES parent_tbl_name (index_col_name,...)
在列Employment_Status.Worker_ID
和Employment_Status.Org_ID
上定义相关的键。那么你应该能够在子表中引用它们。
请参阅:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name](index_col_name,...)
参考文献tbl_name(index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]reference_option:
限制| CASCADE | SET NULL |没有行动