我有以下SQL代码
CREATE TABLE EMPLOYEES
(
empID NUMBER NOT NULL,
ssn CHAR(10) NOT NULL,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(2),
email VARCHAR(40),
street VARCHAR(40),
postalCode NUMBER,
city VARCHAR(20),
country VARCHAR(20),
job VARCHAR(20),
salary NUMBER(*,2),
birthdate DATE,
specialization VARCHAR(30),
username VARCHAR(25),
password VARCHAR(25),
levelOfClearance VARCHAR(20),
PRIMARY KEY (empID),
UNIQUE (ssn)
);
CREATE TABLE PATIENTS
(
patientID NUMBER NOT NULL,
healthInsID NUMBER,
fname VARCHAR(20) NOT NULL,
minit VARCHAR(15),
lname VARCHAR(30) NOT NULL,
gender CHAR(1),
email VARCHAR(40),
street VARCHAR(40),
postalCode CHAR(4),
city VARCHAR(20),
country VARCHAR(20),
PRIMARY KEY (patientID),
FOREIGN KEY (healthInsID) REFERENCES HEALTH_INSURANCES (healthInsID)
ON DELETE SET NULL
);
CREATE TABLE APPOINTMENTS
(
appointmentID NUMBER NOT NULL,
empID NUMBER NOT NULL,
appointmentDate DATE,
cost NUMBER(*,2),
patientID NUMBER,
PRIMARY KEY (appointmentID),
FOREIGN KEY (empID) REFERENCES EMPLOYEES (empID)
ON DELETE SET NULL,
FOREIGN KEY (patientID) REFERENCES PATIENTS
ON DELETE SET NULL
);
CREATE TABLE SYMPTOMS
(
appointmentID NUMBER NOT NULL,
description VARCHAR(100),
PRIMARY KEY (appointmentID),
FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
ON DELETE SET NULL
);
创建前3个表没有问题,但最后一个表给出了错误“ORA-02270:此列列表没有匹配的唯一或主键”。我已经搜索了很多,但还没找到任何解决方案。
答案 0 :(得分:0)
不确定,但我认为问题可能出在symptoms
表
CREATE TABLE SYMPTOMS
(
appointmentID NUMBER NOT NULL, <-- it's NOT NULL
description VARCHAR(100),
PRIMARY KEY (appointmentID),
**FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
ON DELETE SET NULL** <-- here you are saying -- set to null on delete
);
您正在尝试null
您声明为appointmentID
的{{1}}。这看起来不正确。我尝试使用non null
创建相同的表,但它运行正常。
(OR)
尝试创建您的on delete cascade
表,如下所示
SYMPTOMS
请参阅此示例小提琴
答案 1 :(得分:0)
问题在于SYMPTOMS表,你的PK可以设置为null(这是不可能的) 我建议你做以下事情:
sql fiddle:http://sqlfiddle.com/#!4/67d4b
CREATE TABLE SYMPTOMS
(
SymptomID NUMBER NOT NULL,
appointmentID NUMBER NOT NULL,
description VARCHAR(100),
PRIMARY KEY (appointmentID),
FOREIGN KEY (appointmentID) REFERENCES APPOINTMENTS (appointmentID)
);