Disabling foreign key constraint, still can't truncate table? (SQL Server 2005)

前端 未结 4 1109
逝去的感伤
逝去的感伤 2020-12-09 08:08

I have a table called PX_Child that has a foreign key on PX_Parent. I\'d like to temporarily disable this FK constraint so that I can truncate PX_Parent. I\'m not sure how

4条回答
  •  春和景丽
    2020-12-09 08:49

    There is no such option to truncate table while foreign key constraint but we can use some trick like

     ALTER TABLE [dbo].[table2] DROP CONSTRAINT [FK_table2_table1]
        GO
        truncate table [table1]
    GO
        ALTER TABLE [dbo].[table2]  WITH CHECK ADD  CONSTRAINT [FK_table2_table1] FOREIGN KEY([FKId])
        REFERENCES [dbo].[table1] ([ID])
        GO
    
        ALTER TABLE [dbo].[table2] CHECK CONSTRAINT [FK_table2_table1]
        GO
    

提交回复
热议问题