Skip to content Skip to sidebar Skip to footer

Deleting And Restoring Foreign Key Values In Sql Server Database Table

I am working with loading XML files contents into a table in a SQL Server database, for which I wanted to delete the contents of a table dbo.Accounting in the SQL Server database,

Solution 1:

You don't need to remove (DROP) the foreign key constraint, you can disabled it instead, using NOCHECK CONSTRAINT:

ALTERTABLE dbo.Inventories NOCHECK CONSTRAINT FK__Inventori__Accou__29CC2871

And after the changes, to enable again:

ALTERTABLE dbo.Inventories WITHCHECKCHECKCONSTRAINT FK__Inventori__Accou__29CC2871

Off course, if there's any inconsistent data, you won't be able to enable the constraint

Solution 2:

This is quite common topic: How can foreign key constraints be temporarily disabled using T-SQL?

Enabling/Disabling constraints will not result to data loss. However, you cannot enable them back if you have data inconsistancy. For instance, child table record refers to a missing record of parent table

Post a Comment for "Deleting And Restoring Foreign Key Values In Sql Server Database Table"