Drop table with foreign key

时光总嘲笑我的痴心妄想 提交于 2019-12-25 08:57:19

问题


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.


回答1:


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



回答2:


use this

DROP TABLE Inventory CASCADE CONSTRAINTS;



回答3:


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



回答4:


Just try this

ALTER TABLE Inventory NOCHECK CONSTRAINT all
DROP TABLE Inventory



回答5:


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



来源:https://stackoverflow.com/questions/38824702/drop-table-with-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!