sqlserver 2014 删除主键约束

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

以上代码测试均失败,

最后, 直接删除表主键

 

转载于:https://www.cnblogs.com/enych/p/10049649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值