In SQL Server 2005, can I do a cascade delete without setting the property on my tables?

前端 未结 13 1488
心在旅途
心在旅途 2020-12-12 17:02

I have a database full of customer data. It\'s so big that it\'s really cumbersome to operate on, and I\'d rather just slim it down to 10% of the customers, which is plenty

13条回答
  •  我在风中等你
    2020-12-12 17:27

    Unless you want to maintain all related queries as proposed by Chris, the ON DELETE CASCADE is by far the quickest and the most direct solution. And if you don't want it to be permanent, why don't you have some T-SQL code that will switch this option on and off like here

    1. remove the original Tbl_A_MyFK constraint (without the ON DELETE CASCADE)

      ALTER TABLE Tbl_A DROP CONSTRAINT Tbl_A_MyFK

    2. set the constraint Tbl_A_MyFK with the ON DELETE CASCADE

      ALTER TABLE Tbl_A ADD CONSTRAINT Tbl_A_MyFK FOREIGN KEY (MyFK) REFERENCES Tbl_B(Column) ON DELETE CASCADE

    3. Here you can do your delete

      DELETE FROM Tbl_A WHERE ...

    4. drop your constraint Tbl_A_MyFK

      ALTER TABLE Tbl_A DROP CONSTRAINT Tbl_A_MyFK

    5. set the constraint Tbl_A_MyFK without the ON DELETE CASCADE

      ALTER TABLE Tbl_A ADD CONSTRAINT Tbl_A_MyFK FOREIGN KEY (MyFK) REFERENCES (Tbl_B)

提交回复
热议问题