SQL Server › Constraints (Primary Key, Foreign Key, Check & Unique)

How do you temporarily disable a constraint to perform a bulk data load, and how do you safely re-enable it afterward?

Intermediate

Answer

You use ALTER TABLE with the NOCHECK CONSTRAINT option to temporarily disable a specific constraint, perform your bulk data operation, and then re-enable it using CHECK CONSTRAINT, ideally with the WITH CHECK option to also validate that all the newly loaded data actually satisfies the constraint.
ALTER TABLE Orders NOCHECK CONSTRAINT FK_Orders_Customers;
-- Perform bulk insert here
ALTER TABLE Orders WITH CHECK CHECK CONSTRAINT FK_Orders_Customers;
Real-world example A data migration team temporarily disables a foreign key constraint to quickly bulk load historical order data, then re-enables and validates the constraint afterward to confirm all the loaded data is actually consistent.

Common follow-ups: What is the performance benefit of disabling constraints during a bulk load?;How do you find out if a constraint is currently trusted or untrusted after being re-enabled?