sql server怎樣刪除外來鍵約束

2021-06-16 07:10:10 字數 635 閱讀 6587

--測試環境

--主表create table test1(id int primary key not null,value int)insert test1 select 1,2go

--從表create table test2(id int references test1(id),value int)go

--第一步:找出test2表上的外來鍵約束名字

--2000exec sp_helpconstraint 'test2'

--可以在constraint_name 屬性中找到外來鍵約束名字

--2005select 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

--第三步:檢查表上是否還有外來鍵約束--只要使用第一步裡面的查詢語句即可

sqlserver增加刪除外來鍵

下面將對使用transact sql語句,設定表mybbs中的authorid為sql外來鍵的方法及步驟進行了詳細說明,希望對您能夠有所啟迪。設定表mybbs中的authorid為sql server外來鍵,參照author表的id欄位,直接使用transact sql語句,過程如下 增加表mybb...

sqlserver建立和刪除外來鍵約束

原文 x先找出約束名字 然後刪除它 我給個例子 測試環境 主表 create table test1 id int primary key not null,value int insert test1 select 1,2 go 從表 create table test2 id int refer...

刪除外來鍵約束

主表 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 第...