创建表时出现ORA-02270错误

时间:2014-08-04 13:02:55

标签: sql database oracle

这是我用于创建表employee的create table命令,但即使我已将ssn声明为主键,我也无法弄清楚为什么会出现此错误

CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN)
REFERENCES EMPLOYEE(SSSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER)
REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);

但我收到错误:

ORA-02270: no matching unique or primary key for this column-list

2 个答案:

答案 0 :(得分:1)

CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN)更改为CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSN),如下所示:

CREATE TABLE EMPLOYEE(
F_NAME VARCHAR2(15) NOT NULL,
M_NAME CHAR(2),
L_NAME VARCHAR2(15) NOT NULL,
SSN CHAR(9) PRIMARY KEY,
BIRTHDAY DATE,
ADDRESS VARCHAR2(50),
SEX CHAR(1) CHECK(SEX IN('M','F','m','f')),
SALARY NUMBER(7) DEFAULT 800,
SSSN CHAR(9),
DEPARTMENT_NUMBER NUMBER(5),
CONSTRAINT EMP_SSSN_FK FOREIGN KEY(SSSN) REFERENCES EMPLOYEE(SSN) ON DELETE SET NULL,
CONSTRAINT EMP_DEPTNO_FK FOREIGN KEY(DEPARTMENT_NUMBER) REFERENCES DEPARTMENT(DEPT_NO) ON DELETE CASCADE);

答案 1 :(得分:1)

当且仅当它是另一个表中的主键时,才能在表中声明外键。 您需要立即执行的操作是确保SSN和DEPARTMENT_NUMBER是其各自表中的主键。

访问此链接,您将很容易找到错误。 http://www.techonthenet.com/oracle/errors/ora02270.php

如果您不遵循,请继续学习 http://www.w3schools.com/sql/sql_foreignkey.asp

希望有所帮助