SQL Server - cascade delete?

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.

Use something like ALTER TABLE T2 ADD CONSTRAINT fk_employee FOREIGN KEY (employeeID) REFERENCES T1 (employeeID) ON DELETE CASCADE; Fill in the correct column names and you should be set. As mark_s correctly stated, if you have already a foreign key constraint in place, you maybe need to delete the old one first and then create the new one.

You cannot just add a new FK constraint if there's one in place already (as the OP says there is); you will need to delete the existing one first – marc_s Jun 7 at 5:10 @marc_s Thank you. I edited my answer. – Hyperboreus Jun 7 at 5:15 1 @marc_s - actually, you can add a second foreign key against exactly the same columns on both sides, and it will work correctly.

If working in a production environment with no downtime, it may be preferable to introduce the new FK with cascade, and then drop the older FK, rather than leave a window on the table when no FK is in place. (Just tested on SQL 2008) – Damien_The_Unbeliever Jun 7 at 7:50 This is correct. I tried this, and it works.

There is no need to drop the first foreign key constraints. Thanks for the reply. – Bichvan Nguyen Jun 7 at 3:39.

How do I alter the table definitions to perform cascading delete in SQL Server when a record from T1 is deleted, all associated records in T2 also deleted. The foreign constraint is in place between them. I don't want to drop the tables or create a trigger to do the deletion for T2.

For example, when I delete an employee, all the review record should be gone, too.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions