views:

154

answers:

3

How do I go about deleting a row that is referenced by many other tables, either as a primary key or as a foreign key?

Do I need to delete each reference in the appropriate order, or is there an 'auto' way to perform this in, for example, linq to sql?

thanks.

+1  A: 

if you have the foreign keys set with ON DELETE CASCADE, it'll take care of pruning your database with just DELETE master WHERE id = :x

just somebody
That's 2005+ only.
OMG Ponies
using older versions of MS SQL Server goes contrary to "best practices" ;)
just somebody
@OMG, Sql 2000 includes referential key cascades (i.e. on delete/update cascade) - maybe the answer was edited to remove something that was originally there (like on delete/update set x) which is 2005+ only, but standard delete cascading is available in Sql 2000+
chadhoc
That is not only 2005+ SQL Server 2000 allows for cascades as well..
JonH
+4  A: 

If you're performing all of your data access through stored procedures then your delete stored procedure for the master should take care of this. You need to maintain it when you add a new related table, but IMO that requires you to think about what you're doing, which is a good thing.

Personally, I stay away from cascading deletes. It's too easy to accidentally delete a slew of records when the user should have been warned about existing children instead.

Tom H.
+1 - I have never seen a case in systems I have worked on where I ever wanted cascading deletes. We either weren't deleting things (because they were too important - e.g. Patient, Patient Detail), or the items which we usually used foreign keys to never really became deletable, because so many things linked to them (e.g. lookups, code tables, etc)
Cade Roux
+2  A: 

Many times the best way to delete something in a database is to just "virtually" delete it by setting an IsDeleted column, and then ignoring the row in all other queries.

Deletes can be very expensive for heavily linked tables, and the locks can cause other queries to fail while the delete is happening.

You can just leave the "IsDeleted" rows in the system forever (which might be helpful for auditing), or go back and delete them for real when the system is idle.

Eric Z Beard