How do I edit a table in order to enable CASCADE DELETE?

隐身守侯 提交于 2019-11-27 09:01:35

Read this Microsoft article first. Read Me. I use the GUI during design so here is a picture of how it is selected in SSMS.

The syntax added to the foreign key is " ON DELETE CASCADE "
Mr. TA

Google ALTER TABLE DROP CONSTRAINT, then ALTER TABLE ADD CONSTRAINT:

ALTER TABLE

Here's a quick example:

CREATE TABLE A 
(
 ID INTEGER NOT NULL UNIQUE
);

CREATE TABLE B 
(
 ID INTEGER NOT NULL UNIQUE
    CONSTRAINT fk__B__A 
       REFERENCES A (ID)
);

-- Oops! Forgot the CASCADE referential actions.
-- DROP the constraint then recreate it:

ALTER TABLE B DROP
   CONSTRAINT fk__B__A;

ALTER TABLE B ADD
   CONSTRAINT fk__B__A
      FOREIGN KEY (ID)
      REFERENCES A (ID)
      ON DELETE CASCADE
      ON UPDATE CASCADE;

Here's the way I would add the "cascading delete" feature to an existing foreign key in SQL Server Management Studio.

First, find your foreign key, and open it's "DROP and CREATE To" in a new Query window.

Then, just add "ON DELETE CASCADE" to the "ADD CONSTRAINT" command:

Then just hit hit the "Execute" button to run the query.

Job done !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!