How to drop a list of SQL Server tables, ignoring constraints?

后端 未结 6 1203
醉梦人生
醉梦人生 2021-02-12 21:47

I have a list of half a dozen MSSQL 2008 tables that I would like to remove at once from my database. The data has been entirely migrated to new tables. There is no reference in

6条回答
  •  心在旅途
    2021-02-12 22:11

    I suspect that you would have to do an 'alter' command on the offending tables before the drop to remove the forigen key contraints.

    ALTER TABLE Orders DROP FOREIGN KEY fk_PerOrders;
    DROP TABLE Orders;
    

    Of course if you drop the child tables first, then you wont have this problem. (unless you have table A contraint to table B and table B constraint to A, then you will need to Alter one of the tables, e.g. A to remove the constraint)

    e.g. this WONT work, since Orders has a contraint from Order_Lines

    DROP TABLE Orders;
    DROP TABLE Order_lines;
    

    e.g. this will work

    DROP TABLE Order_lines;
    DROP TABLE Orders;
    

提交回复
热议问题