MSSQL에서 DELETE CASCADE 설정방법

2018. 1. 4. 15:37IT개발/Database

반응형

출처 : https://stackoverflow.com/questions/6260688/how-do-i-use-cascade-delete-with-sql-server

방법1. 

You will need to,

  • Drop the existing foreign key constraint,
  • Add a new one with the ON DELETE CASCADE setting enabled.

Something like:

ALTER TABLE dbo.T2
   DROP CONSTRAINT FK_T1_T2   -- or whatever it's called

ALTER TABLE dbo.T2
   ADD CONSTRAINT FK_T1_T2_Cascade
   FOREIGN KEY (EmployeeID) REFERENCES dbo.T1(EmployeeID) ON DELETE CASCADE


방법2.

To add "Cascade delete" to an existing foreign key in SQL Server Management Studio:

First, select your Foreign Key, and open it's "DROP and Create To.." in a new Query window.

enter image description here

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

n

And hit the "Execute" button to run this query.

방법3.

You can do this with SQL Server Management Studio.

→ Right click the table design and go to Relationships and choose the foreign key on the left-side pane and in the right-side pane, expand the menu "INSERT and UPDATE specification" and select "Cascade" as Delete Rule.

SQL Server Management Studio



반응형