为什么缺少关键字错误?

时间:2013-02-18 10:58:17

标签: sql oracle11g foreign-keys constraints ora-00905

CREATE TABLE SORDER (
    C_NO CHAR(4),
    P_NO CHAR(4),
    QUANTITY SMALLINT,
    PRIMARY KEY (C_NO, P_NO),
    CONSTRAINT CNO_FK FOREIGN KEY (C_NO) REFERENCES CUSTOMER ON DELETE RESTRICT,
    CONSTRAINT PNO_FK FOREIGN KEY (P_NO) REFERENCES PRODUCT2 ON DELETE RESTRICT
);
  

ORA-00905:缺少关键字

这是另外两张表btw。

CREATE TABLE PRODUCT2 (
    P_NO CHAR(4) PRIMARY KEY,
    P_NAME CHAR(6),
    PRICES DECIMAL(3,2),
    STOCK SMALLINT
);

CREATE TABLE CUSTOMER2 (
    C_NO CHAR(4) PRIMARY KEY,
    C_NAME CHAR(6),
    CITY CHAR(11)
);

2 个答案:

答案 0 :(得分:5)

Oracle仅支持ON DELETE SET NULLON DELETE CASCADE作为关键字。 ON DELETE RESTRICT的功能是通过省略ON DELETE

来实现的
CREATE TABLE SORDER (
    C_NO CHAR(4),
    P_NO CHAR(4),
    QUANTITY SMALLINT,
    PRIMARY KEY (C_NO, P_NO),
    CONSTRAINT CNO_FK FOREIGN KEY (C_NO) REFERENCES CUSTOMER2,
    CONSTRAINT PNO_FK FOREIGN KEY (P_NO) REFERENCES PRODUCT2
);

有关详细信息,请参阅手册:http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#CJABCJJF

手册中的引用:

  

ON DELETE子句使您可以确定在删除引用的主键或唯一键值时Oracle数据库如何自动维护参照完整性。如果省略此子句,则Oracle不允许您删除父表中具有子表中的从属行的引用键值

这是一个SQLFiddle:http://sqlfiddle.com/#!4/07e93/1

答案 1 :(得分:-6)

尝试以下代码

CREATE TABLE SORDER (
    C_NO CHAR(4),
    P_NO CHAR(4),
    QUANTITY SMALLINT,
    PRIMARY KEY (C_NO, P_NO),
    CONSTRAINT CNO_FK FOREIGN KEY (C_NO) REFERENCES CUSTOMER ON DELETE RESTRICT,
    CONSTRAINT PNO_FK FOREIGN KEY (P_NO) REFERENCES PRODUCT2 ON DELETE RESTRICT
);