刪除SQL Server外鍵約束的實(shí)例
作者:佚名
下面將以實(shí)例的形式為您介紹刪除SQL Server外鍵約束的方法,供您參考,如果您對此有興趣的話,不妨一看,相信對您能有所幫助。
如果需要?jiǎng)h除SQL Server外鍵約束,應(yīng)該如何操作呢?下面將為您舉例說明如何刪除SQL Server外鍵約束,希望對您有所啟迪。
下面給個(gè)例子
- --測試環(huán)境
- --主表
- create table test1(id int primary key not null,value int)
- insert test1 select 1,2
- go
- --從表
- create table test2(id int references test1(id),value int)
- go
- --第一步:找出test2表上的外鍵約束名字
- --2000
- exec sp_helpconstraint 'test2'
- --可以在constraint_name 屬性中找到外鍵約束名字
- --2005
- select name
- from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id where f.parent_object_id=object_id('test2')
- /*
- name
- ---------------------------------
- FK__test2__id__08EA5793*/
- --第二步:刪除外鍵約束
- alter table test2 drop constraint FK__test2__id__08EA5793
- --第三步:檢查表上是否還有外鍵約束
- --只要使用第一步里面的查找語句即可
【編輯推薦】
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)