ORA-02270 - 无法找到错误

时间:2015-01-12 04:23:12

标签: sql oracle foreign-keys primary-key

遇到ORA-02270错误的问题。经过一次又一次的主键和外键后,我无法找到问题所在。

CREATE TABLE CUSTACC(
REFNO CHAR(4), 
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO));

CREATE TABLE CUST(
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK    PRIMARY KEY(CUSTNO),
CONSTRAINT CUSTNO_FK    FOREIGN KEY (CUSTNO)
    REFERENCES CUSTACC(REFNO));

CREATE TABLE ACC(
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK    PRIMARY KEY (ACCNO),
CONSTRAINT ACCNO_FK    FOREIGN KEY (ACCNO)
    REFERENCES CUSTACC(ACCNO));

我必须是盲人但我已经检查了之前人们建议的一切。

1 个答案:

答案 0 :(得分:2)

CUSTACC上的主键包含两列:REFNOACCNO

外键引用应包括它们。因此,您的表需要两列。

我怀疑,事实上,你想在第一个表中使用外键引用,如下所示:

CREATE TABLE CUST (
    CUSTNO CHAR(4),
    NAME VARCHAR2(30),
    ADDRESS VARCHAR2(50),
    AREA VARCHAR2(10),
    CONSTRAINT CUSTNO_PK    PRIMARY KEY(CUSTNO)
);

CREATE TABLE ACC (
    ACCNO NUMBER(7),
    BALANCE NUMBER(8,2),
    BRANCH VARCHAR2(10),
    OPENED DATE,
    BONUS NUMBER(8,2),
    CONSTRAINT ACCNO_PK    PRIMARY KEY (ACCNO)
);

CREATE TABLE CUSTACC (
    REFNO CHAR(4), 
    ACCNO NUMBER(7),
    CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO),
    CONSTRAINT CUSTNO_FK    FOREIGN KEY (REFNO)
        REFERENCES CUST(CUSTNO),
    CONSTRAINT ACCNO_FK    FOREIGN KEY (ACCNO)
        REFERENCES ACC(ACCNO)
);

Here是一个SQL小提琴。