Drop table with foreign key

2019-09-08 14:29发布

I'm trying this code

Drop Table Inventory

I get error:

Could not drop object 'Inventory' because it is referenced by a FOREIGN KEY constraint.

5条回答
太酷不给撩
2楼-- · 2019-09-08 15:07

You need to Drop the Constraint first..

ALTER TABLE [dbo].[t2] DROP CONSTRAINT [foreign key constraint]

then You can Drop the table

Drop table t1
查看更多
Summer. ? 凉城
3楼-- · 2019-09-08 15:10

Just try this

ALTER TABLE Inventory NOCHECK CONSTRAINT all
DROP TABLE Inventory
查看更多
闹够了就滚
4楼-- · 2019-09-08 15:13

use this

DROP TABLE Inventory CASCADE CONSTRAINTS;
查看更多
Viruses.
5楼-- · 2019-09-08 15:30

First you have to Drop the table's constraints and then table

SELECT 
    'ALTER TABLE ' +  OBJECT_SCHEMA_NAME(parent_object_id) +
    '.[' + OBJECT_NAME(parent_object_id) + 
    '] DROP CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Inventory')
Drop Table Inventory
查看更多
够拽才男人
6楼-- · 2019-09-08 15:33

drop the foreign key constraint from subtable and then drop the main table

查看更多
登录 后发表回答