将多个外键添加到 Oracle 中的现有表

问题描述

我想向 Oracle 数据库中的现有表添加多个外键。以下 sql 查询给了我一个错误。我可以一一添加外键约束。但我想在下面的一个语句中做到这一点。

ALTER  TABLE address                                                                                                  
ADD  CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) 
     REFERENCES  customer (id) ON  DELETE CASCADE,ADD  CONSTRAINT fk_city_id     FOREIGN KEY (city_id) 
     REFERENCES  city (id) ON  DELETE  CASCADE;

知道怎么做吗?

解决方法

这并不完全正确(@Thorsten 所说的)。您可以一次添加两个约束。

SQL> create table test (empno number,deptno number);

Table created.

SQL>
SQL> alter table test add
  2    ( constraint fk_test_emp  foreign key (empno)  references emp (empno),3      constraint fk_test_dept foreign key (deptno) references dept (deptno)
  4    );

Table altered.

SQL>