sql - 如何在ALTER TABLE语句中添加“ON DELETE CASCADE”

我的表中有一个外键约束,我想向它添加ON DELETE CASCADE。

我试过这个:

alter table child_table_name
  modify constraint fk_name
  foreign key (child_column_name)
  references parent_table_name (parent_column_name) on delete cascade;

不行。

编辑:
外键已经存在,外键列中有数据。

执行语句后得到的错误消息:

ORA-02275: such a referential constraint already exists in the table
Ula Krukar asked 2019-08-12T23:23:18Z
8个解决方案
124 votes

您不能将ON DELETE CASCADE添加到现有约束。 您将不得不以drop和re-create为约束。 文档显示MODIFY CONSTRAINT子句只能修改约束的状态(i-e:ENABLED/DISABLED ...)。

Vincent Malgrat answered 2019-08-12T23:23:32Z
58 votes

首先drop你的外键并尝试上面的命令,把add constraint代替modify constraint。现在这是命令:

ALTER TABLE child_table_name 
  ADD CONSTRAINT fk_name 
  FOREIGN KEY (child_column_name) 
  REFERENCES parent_table_name(parent_column_name) 
  ON DELETE CASCADE;
pradeep answered 2019-08-12T23:23:59Z
7 votes

此PL * SQL将向DBMS_OUTPUT写入一个脚本,该脚本将删除没有删除级联的每个约束,并使用删除级联重新创建它。

注意:运行此脚本的输出是您自己承担的风险。 最好阅读生成的脚本并在执行前编辑它。

DECLARE
      CURSOR consCols (theCons VARCHAR2, theOwner VARCHAR2) IS
        select * from user_cons_columns
            where constraint_name = theCons and owner = theOwner
            order by position;
      firstCol BOOLEAN := TRUE;
    begin
        -- For each constraint
        FOR cons IN (select * from user_constraints
            where delete_rule = 'NO ACTION'
            and constraint_name not like '%MODIFIED_BY_FK'  -- these constraints we do not want delete cascade
            and constraint_name not like '%CREATED_BY_FK'
            order by table_name)
        LOOP
            -- Drop the constraint
            DBMS_OUTPUT.PUT_LINE('ALTER TABLE ' || cons.OWNER || '.' || cons.TABLE_NAME || ' DROP CONSTRAINT ' || cons.CONSTRAINT_NAME || ';');
            -- Re-create the constraint
            DBMS_OUTPUT.PUT('ALTER TABLE ' || cons.OWNER || '.' || cons.TABLE_NAME || ' ADD CONSTRAINT ' || cons.CONSTRAINT_NAME 
                                        || ' FOREIGN KEY (');
            firstCol := TRUE;
            -- For each referencing column
            FOR consCol IN consCols(cons.CONSTRAINT_NAME, cons.OWNER)
            LOOP
                IF(firstCol) THEN
                    firstCol := FALSE;
                ELSE
                    DBMS_OUTPUT.PUT(',');
                END IF;
                DBMS_OUTPUT.PUT(consCol.COLUMN_NAME);
            END LOOP;                                    

            DBMS_OUTPUT.PUT(') REFERENCES ');

            firstCol := TRUE;
            -- For each referenced column
            FOR consCol IN consCols(cons.R_CONSTRAINT_NAME, cons.R_OWNER)
            LOOP
                IF(firstCol) THEN
                    DBMS_OUTPUT.PUT(consCol.OWNER);
                    DBMS_OUTPUT.PUT('.');
                    DBMS_OUTPUT.PUT(consCol.TABLE_NAME);        -- This seems a bit of a kluge.
                    DBMS_OUTPUT.PUT(' (');
                    firstCol := FALSE;
                ELSE
                    DBMS_OUTPUT.PUT(',');
                END IF;
                DBMS_OUTPUT.PUT(consCol.COLUMN_NAME);
            END LOOP;                                    

            DBMS_OUTPUT.PUT_LINE(')  ON DELETE CASCADE  ENABLE VALIDATE;');
        END LOOP;
    end;
shindigo answered 2019-08-12T23:24:32Z
6 votes

如前所述:

ALTER TABLE TABLEName
drop CONSTRAINT FK_CONSTRAINTNAME;

ALTER TABLE TABLENAME
ADD CONSTRAINT FK_CONSTRAINTNAME
    FOREIGN KEY (FId)
    REFERENCES OTHERTABLE
        (Id)
    ON DELETE CASCADE ON UPDATE NO ACTION;

正如你所看到的那些必须是分开的命令,首先删除然后添加。

David Silva-Barrera answered 2019-08-12T23:25:05Z
2 votes

对于任何使用MySQL的人:

如果您进入PHPMYADMIN网页并导航到包含您要更新的外键的表格,您只需单击位于Structure选项卡中的Relational view,然后将On delete选择菜单选项更改为Cascade

图片如下:

enter image description here

James111 answered 2019-08-12T23:25:46Z
2 votes

这是一个方便的解决方案!我使用的是SQL Server 2008 R2。

如果要通过添加ON DELETE / UPDATE CASCADE来修改FK约束,请按照下列步骤操作:

1号:

右键单击约束,然后单击“修改”

enter image description here

2号:

在左侧选择约束(如果有多个)。 然后在右侧,崩溃" INSERT AND UPDATE规范" 指向并指定删除规则或更新规则行上的操作以满足您的需要。 之后,关闭对话框。

enter image description here

第3号:

最后一步是保存这些修改(当然!)

enter image description here

PS:由于我想修改另一个表中引用的主键,因此它从一堆工作中救了我。

Serge Kishiko answered 2019-08-12T23:27:10Z
1 votes

如果要更改外键而不删除它,可以执行以下操作:

ALTER TABLE child_table_name  WITH CHECK ADD FOREIGN KEY(child_column_name)
REFERENCES parent_table_name (parent_column_name) ON DELETE CASCADE
RedPelle answered 2019-08-12T23:27:36Z
1 votes

MYSQL用户的答案:

ALTER TABLE ChildTableName 
DROP FOREIGN KEY `fk_table`;
ALTER TABLE ChildTableName 
ADD CONSTRAINT `fk_t1_t2_tt`
  FOREIGN KEY (`parentTable`)
  REFERENCES parentTable (`columnName`)
  ON DELETE CASCADE
  ON UPDATE CASCADE;
bhavani answered 2019-08-12T23:28:03Z
translate from https://stackoverflow.com:/questions/1571581/how-to-add-on-delete-cascade-in-alter-table-statement