1樓:匿名使用者
刪除外來鍵約束的表,首先要刪除外來鍵關聯,然後才可以刪除表。
如:1、建立表author :
create table [dbo].[author]
([id] [bigint] not null primary key,
[authorname] [char] (10) null,
[address] [char] (480) null,
[introduction] [ntext] null)
2、建立mybbs表:
create table [dbo].[mybbs] (
[id] [bigint] identity (1, 1) not null,
[authorid] [bigint] not null,
[title] [char] (40) null,
[date_of_created] [datetime] null,
[abstract] [char] (480) null,
[content] [ntext] null)
3、建立外來鍵關係:
alter table dbo.mybbs add constraint fk_mybbs_author
foreign key (authorid)
references dbo.author([id]) on update cascade on delete cascade
4、在刪除表前,要刪除外來鍵,語法如下:
alter table dbo.mybbs drop constraint fk_mybbs_author
5、然後再刪除表:
drop table [dbo].[mybbs]
2樓:匿名使用者
先找出你的外來鍵所在用下面的語句:
--查詢乙個表的所有外來鍵:
select 主鍵列id=b.rkey
,主鍵列名=(select name from syscolumns where colid=b.rkey and id=b.rkeyid)
,外來鍵表id=b.fkeyid
,外來鍵表名稱=object_name(b.fkeyid)
,外來鍵列id=b.fkey
,外來鍵列名=(select name from syscolumns where colid=b.fkey and id=b.fkeyid)
,級聯更新=objectproperty(a.id,'cnstisupdatecascade')
,級聯刪除=objectproperty(a.id,'cnstisdeletecascade')
from sysobjects a
join sysforeignkeys b on a.id=b.constid
join sysobjects c on a.parent_obj=c.id
where a.xtype='f' and c.xtype='u'
and object_name(b.rkeyid)='students'
然後刪除對應的外來鍵~
3樓:
必須要先刪除外來鍵表,才可刪除主表
sql server怎樣刪除外來鍵約束?
4樓:大野瘦子
用這個跟:alter table 表名稱 drop constraint 外來鍵的名稱
使用如下sql語句查詢出表中外鍵約束名稱:
1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.
constraint_object_id=o.object_id 3 where f.parent_object_id=object_id('表名')
執行如下sql語句刪除即可。
1 alter table 表名 drop constraint 外來鍵約束名
5樓:匿名使用者
x先找出約束名字
然後刪除它
我給個例子
--測試環境
--主表
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
--第三步:檢查表上是否還有外來鍵約束
--只要使用第一步裡面的查詢語句即可
6樓:
語句:alter table 表名稱 drop constraint 外來鍵的名稱
也可以在sql server的管理工具中,找到表,找到"鍵"中的相應外來鍵,右擊刪除
7樓:love思宜
我個人覺得最好不要設外來鍵,這東西比較麻煩,若表之間有關聯之後做聯合查詢就是了……那樣更靈活一些
刪除外來鍵約束的方法ls說的是對的
sql 刪除帶外來鍵約束的表的語句是什麼 50
8樓:阿冬
擁有主鍵的表稱為主表,擁有外來鍵的表稱為從表,從表可以直接刪除,不受限制,但主表無法在從表還存在的情況下直接刪除。
另外,根據樓主的意思,我猜想你可能想問的是有外碼關聯的資料刪除的問題。以下簡述:
通常情況下,從表中外鍵所在欄位的資料取值只能取主鍵中存在的值或者取空值,從表中的資料可以隨時刪除,而主表中的資料如果被從表所參照,則不能直接刪除,如果想直接刪除主表中被參照的資料,需要在建立外來鍵時指定on delete cascade或者on delete set null,即
foreign key(從表中的某個字段) references 主表(主鍵) on delete cascade
或者foreign key(從表中的某個字段) references 主表(主鍵) on delete set null
其中第一種方法表示刪除主表中的資料時,從表中對應資料一起被強制刪除;
第二種方法表示刪除主表中的資料時,從表中對應資料設定為null
最後要注意的一點,這兩個短語並非所有資料庫管理系統都支援,印象中db2、oracle和sql server都支援,mysql沒試過,不知道你用哪種資料庫,最好在你的系統中親自試一下。
以上僅供參考。
9樓:匿名使用者
刪除語句如下:
alter table 表名 drop constraint 外來鍵約束名
使用如下sql語句查詢出表中外鍵約束名稱:
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('表名')
各個資料庫有細微區別,以上提供的為sqlserver的
10樓:匿名使用者
刪除外來鍵語句:
現刪除關聯的外鍵名,才能再刪除外來鍵字段;
alter table 表名 drop foreign key 外來鍵關聯名;
alter table 表名 drop column 『列名』
sql 刪除存在外來鍵約束欄位的行怎麼刪
11樓:匿名使用者
drop connstraint 外鍵名(刪除外來鍵)刪除此行資訊。然後再把外來鍵新增了alter table 表名1add connstraint 外鍵名 foreign key(欄位名) references 表2(欄位名)
12樓:匿名使用者
看你的資料庫怎麼設計
一般像這種情況 都是 先有使用者再有訂單 ,就是說 沒有使用者就沒有相應的訂單,要刪除訂單 是不會影響使用者資料的,這裡的使用者表就是主表。 刪除 直接用 delete 訂單表 where 訂單id。
如何用sql語句刪除外來鍵約束
13樓:很多很多
用sql語句刪除外來鍵約束的具體步驟如下:
我們需要準備的材料分別是:電腦、sql server 2008。
1、首先我們用sql server 2008開啟需要刪除外來鍵約束的表,滑鼠右鍵點選左側的「dbo」選項選擇「設擊」。
2、然後我們在該頁面中左鍵點選選擇「刪除主鍵」選項。
3、然後我們在彈出來的該頁面中左鍵單擊選擇「是」就即可刪除。
14樓:樹葉風
先將存在外來鍵約束的字段的外來鍵用
alter table 從表
add constraint fk_xx foreign key(從表字段) references 主表(主表字段)
goalter table 從表
drop constraint fk_xxgo
15樓:手機使用者
需要兩步 1,alter table 從表 drop foreign key 約束名 ; 2.alter table 從表 drop key 約束名;
16樓:興瑞
alter table 表名稱 drop constraint 外來鍵的名稱
oracle語句怎麼刪除外來鍵約束,只是刪約束,不是刪表
17樓:匿名使用者
假如有主表 test_main 與 子表 test_subsql> -- 建立外來鍵(預設選項)
sql> alter table test_sub add constraint main_id_cons foreign key (main_id) references test_main;
table altered.
刪除外來鍵約束
alter table test_sub drop constraint main_id_cons;
18樓:匿名使用者
alter table 表名 drop constraint 外來鍵約束名;
19樓:匿名使用者
alter table table_name drop constraint 外來鍵約束名稱;
SQL刪除語句,刪除行,列,表,SQL語句如何刪除列?
刪除行delete from table where condition 例如delete from employeesswhere emp id 40 刪除employees表裡面 emp id為40的行刪除列alter table table drop column column 例如alter...
sql刪除指定字串,請教SQL刪除特定字元
1 建立資料庫表 2 插入測試資料,並提交 3 編寫sql語句,即可實現去掉中括號裡面的內容,語句如下 select val,substr val,1,instr val,1 substr val,instr val,1 val2 from test str t 4 如果確定中括號在第一位的話,sq...
sql 查詢刪除重複行 sql查詢去掉重覆記錄
兩種方法。1 使用distinct關鍵字。2 使用group by關鍵字對出現重複資料的字段進行分組。sql查詢去掉重覆記錄 sql中怎麼刪除兩條重覆記錄並保留一條 10 首先,在表裡加乙個自增長的字段,假設取名為id,這樣每條記錄就有了唯一標識id了,然後,使用 group by 的方式,取出重覆...
sql裡面如何刪除重複的記錄,SQL裡面如何刪除重複的記錄?
可以用這條語句刪除重覆記錄 delete from select rownumber over partition by 欄位名 from 表名 as e rn where rn 1 sql中如何刪除乙個表中重複的記錄?sql中刪除乙個表中的重覆記錄可以採用如下步驟 1 把a dist表的記錄用di...
sql中如何去掉一條記錄,sql怎麼刪除乙個表中的所有資料
幻翼高達 sql中去掉一條記錄的步驟如下 我們需要準備的材料分別是 電腦 sql查詢器。1 首先,開啟sql查詢器,連線上相應的資料庫表,例如test3表。2 點選 查詢 按鈕,輸入 select a1,a2 from test3 group by a1 3 點選 執行 按鈕,此時查詢出的a1列只顯...