Question

I have a table (TableB) that has a foreign key relationship with a parent table (TableA).

When I delete a record in Table A, I want to preserve referential integrity by deleting all records in TableB that reference the deleted record in TableA.

Normally I would ON DELETE CASCADE. However due to the table structure and the overprotective safeguards against multiple cascade paths in SQL Server, this is not possible for this specific relationship.

I also cannot use an INSTEAD OF trigger as TableA itself has a CASCADE foreign key relationship on it.

What I'm thinking of doing is changing the relationship between TableA and TableB to ON DELETE SET NULL, then creating an AFTER trigger to cleanup the NULL records in TableB.

Are there better ways of dealing with this scenario?

Was it helpful?

Solution

Can you change the other constraint that is preventing you from adding this one as ON DELETE CASCADE?

Can you add a DeleteMe column, then issue an UPDATE A SET DeleteMe = 1, then use an after trigger to delete Table B rows first, then the requested table A rows?

Can you split or merge tables (vertically, that is) in some way that separates their mutually exclusive dependencies?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top