【问题标题】:Add multiple foreign keys to existing table in Oracle向Oracle中的现有表添加多个外键
【发布时间】:2021-07-06 04:45:39
【问题描述】:

我想在 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;

知道怎么做吗?

【问题讨论】:

    标签: sql database oracle foreign-keys


    【解决方案1】:

    这并不完全正确(@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>
    

    【讨论】:

    • 好吧,显然我所说的完全错误。我误读了语法图。我正在删除我的答案。
    猜你喜欢
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多