ORA-02270:此列列表没有匹配的唯一键或主键

时间:2014-01-02 09:46:07

标签: sql oracle

我有以下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:此列列表没有匹配的唯一或主键”。我已经搜索了很多,但还没找到任何解决方案。

2 个答案:

答案 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

请参阅此示例小提琴

http://sqlfiddle.com/#!3/826dd

答案 1 :(得分:0)

问题在于SYMPTOMS表,你的PK可以设置为null(这是不可能的) 我建议你做以下事情:

  1. 将症状ID列添加为PK(或将description和appointmentID的组合设置为PK)
  2. 设置删除级联或不设置删除策略:如果删除约会,这将导致错误,并引发您需要在代码中处理的异常。
  3. 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)
    );