How can foreign key constraints be temporarily disabled using T-SQL?

后端 未结 16 2275
Happy的楠姐
Happy的楠姐 2020-11-22 04:57

Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create

16条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 05:40

    You should actually be able to disable foreign key constraints the same way you temporarily disable other constraints:

    Alter table MyTable nocheck constraint FK_ForeignKeyConstraintName
    

    Just make sure you're disabling the constraint on the first table listed in the constraint name. For example, if my foreign key constraint was FK_LocationsEmployeesLocationIdEmployeeId, I would want to use the following:

    Alter table Locations nocheck constraint FK_LocationsEmployeesLocationIdEmployeeId
    

    even though violating this constraint will produce an error that doesn't necessarily state that table as the source of the conflict.

提交回复
热议问题