select * from dict where DictCode != '' and PId=5 truncate table menu SELECT * FROM sys.foreign_keys WHERE referenced_object_id=OBJECT_ID('menu'); --找到引用该表的外键 --ALTER TABLE dbo.XX DROP constraint FK_User_XX ALTER TABLE dbo.RoleMenu DROP constraint FK_Menu_RoleMenu --删除外键 select * from sys.foreign_keys where referenced_object_id=object_id('menu') order by 1 --查询引用的外键 alter table menu drop constraint FK_dbo.RoleMenu_dbo.Menu_MenuId select fk.name,fk.object_id,OBJECT_NAME(fk.parent_object_id) as referenceTableName from sys.foreign_keys as fk join sys.objects as o on fk.referenced_object_id=o.object_id where o.name='menu' -- 得到 外键消息 FK__photo__x_id__6991A7CB -- 得到 FK_dbo.RoleMenu_dbo.Menu_MenuId ALTER TABLE dbo.RoleMenu DROP CONSTRAINT Menu_MenuId select fk.name,fk.object_id,object_name(fk.parent_object_id) as referencetablename from sys.foreign_keys as fk join sys.objects as o on fk.referenced_object_id=o.object_id where o.name = 'Menu' alter table RoleMenu drop constraint FK_RoleMenu_Menu_MenuId ALTER TABLE 表名 DROP CONSTRAINT 约束名 ALTER TABLE Menu DROP CONSTRAINT FK_dbo.RoleMenu_dbo.Menu_MenuId
以上代码测试均失败,
最后, 直接删除表主键