Oracle中的外键约束问题

eli*_*rar 3 oracle

在Oracle 9i中声明FK时出现问题.我已经在SO和一些在线文档(例如http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php)中查看了一些示例,没有任何真正的运气; 尝试与链接中的语法类似的语法会生成相同的错误:

Error at Command Line:19 Column:4
Error report:
SQL Error: ORA-02253: constraint specification not allowed here
02253. 00000 -  "constraint specification not allowed here"
*Cause:    Constraint specification is not allowed here in the statement.
*Action:   Remove the constraint specification from the statement.
Run Code Online (Sandbox Code Playgroud)

SQL本身的摘录如下."第19行"是指以第一行开头的行CONSTRAINT

CREATE TABLE Flight (
flight_no varchar2(10) NOT NULL,
airplane_id varchar2(20) NOT NULL
    CONSTRAINT flight_airplane_id_fk FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id)
    ON UPDATE RESTRICT ON DELETE RESTRICT,
dept_date date NOT NULL,
...
Run Code Online (Sandbox Code Playgroud)

或者,在没有CONSTRAINT关键字的情况下尝试它会产生关于右括号的错误,我似乎无法看到它.

PS:我理解ON UPDATE RESTRICT是Oracle中的默认行为,但我希望尽可能明确.

Jus*_*ave 5

首先,在Oracle中,没有ON UPDATE RESTRICT或没有ON DELETE RESTRICT选择.这些似乎在其他数据库引擎中有效,但它们不存在于约束语法图中,并且似乎无效.有一个ON DELETE条款,但只有两个有效选项是CASCADESET NULL.没有ON UPDATE条款.

如果我们airplane_id在constriant定义之前的定义末尾添加一个逗号并删除两个无效子句,那么你的DDL应该是有效的

CREATE TABLE Flight (
  flight_no varchar2(10) NOT NULL,
  airplane_id varchar2(20) NOT NULL,
  CONSTRAINT flight_airplane_id_fk 
    FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id),
  dept_date date NOT NULL,
  <<more columns>>
);
Run Code Online (Sandbox Code Playgroud)