Tuesday, July 20, 2010

SQL Server - Enforce foreign key constraints and cascading deletes

Apparently if Enforce foreign key constraint is set to No in the designer cascading deletes will not take place. The programmatic variant of setting that to No is:

 ALTER TABLE MyTable NOCHECK CONSTRAINT MyFKConstraint


Swapping out NOCHECK with CHECK will enforce the fk constraint.

Although I think this behavior is reasonable, and even desirable it has put me on my heels more than once. Hopefully writing it down will keep me from forgetting again.

No comments: