ORA-02270 - 找不到错误

ORA-02270 - Can't find fault

遇到 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));

我一定是瞎了,但我已经检查了人们之前提出的所有建议。

CUSTACC 上的主键由两列组成:REFNOACCNO

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

我怀疑,事实上,您想要第一个 table 中的外键引用,像这样:

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 Fiddle.