當前位置:首頁 » 編程語言 » sql刪除兩張表

sql刪除兩張表

發布時間: 2024-10-17 21:10:38

sql sever中要刪除兩個相關聯的表該怎麼進行級聯刪除

------解決方案-------------------------------------------------------- --1、建立一個觸發器(推薦)
create trigger on p for deleteas �0�2delete from spj where pno = (select pno from deleted)go--執行刪除delete from p where pname='螺絲'
--2、級聯刪除
alter table p add constraint pk_p_id primary key (pno)go--為tb創建外健,並指定級聯刪除
alter table spj add constraint fk_spj_aid foreign key (pno) references p(pno) on delete cascadego------解決方案----------------------------------------------------------推薦觸發器控制,可控性比較強
SQL code --1、建立一個觸發器(推薦) create trigger on p for delete as delete from spj where pno = (select pno from deleted) go --執行刪除 delete from p where pname='螺絲' --2、級聯刪除 alter table p add constraint pk_p_id primary key (pno) go --為tb創建外健,並指定級聯刪除 alter table spj add constraint fk_spj_aid foreign key (pno) references p(pno) on delete cascade go
------解決方案--------------------------------------------------------建立測試數據
SQL code if object_id('dbo.SPJ') is not null drop table dbo.SPJ; go if object_id('dbo.P') is not null drop table dbo.P; go create table dbo.P ( pno int not null primary key, pname nvarchar(20) not null ); go create table dbo.SPJ ( sno int not null primary key, pno int not null ); insert into dbo.P select 1, 'type-a' union all select 2, 'type-b' union all select 3, 'type-c'; go insert into dbo.SPJ select 1, 1 union all select 2, 1 union all select 3, 1 union all select 4, 2 union all select 5, 3 union all select 6, 3; go
------解決方案--------------------------------------------------------建議用外鍵約束
先刪除子表在刪除父表
------解決方案-------------------------------------------------------- �0�2個人建議用事務處理。

⑵ sql中能一次刪除兩張表的數據嗎

如果在一條語句中,delete命令是不能刪除兩張表的.
但是以下情況例外:
1.在當前被刪表中建立了觸發器,去刪除另外一張表;
2.在SQL Server中,外鍵約束存在cascade delete.

⑶ 如何用SQL語句刪除兩個表中相同的記錄

1,首先創建一個表,並在表中插入重復的記錄,如下圖所示。

⑷ 如何在sql中同時刪除兩個表的內容

那就用事務:

delete from ta where ...
delete from tb where ...

commit;

⑸ SQL 語句刪除問題同時刪除兩個表內關聯的數據

一個sql語句是沒辦法執行兩個刪除操作,如果你要實現上面的功能,有以下幾個選擇:
1.用外鍵關聯刪除,把B表的uid設成外鍵關聯A表的ID,並關聯刪除操作
2.用存儲過程,用事務來處理實現;

望採納!

熱點內容
ftp怎麼上傳文件到伺服器 發布:2024-10-17 23:22:05 瀏覽:164
堆棧演算法 發布:2024-10-17 22:40:47 瀏覽:140
復印機ftp掃描設置不同網段 發布:2024-10-17 22:40:36 瀏覽:739
解壓球和汽車的魔術 發布:2024-10-17 22:34:17 瀏覽:124
貼吧上傳介面 發布:2024-10-17 22:32:32 瀏覽:261
北京雲主機搭建伺服器 發布:2024-10-17 22:31:40 瀏覽:108
php延時 發布:2024-10-17 22:26:25 瀏覽:407
安卓手機插頭是什麼類型 發布:2024-10-17 22:25:22 瀏覽:183
ie緩存圖片在哪 發布:2024-10-17 22:05:23 瀏覽:573
qq群腳本 發布:2024-10-17 22:04:45 瀏覽:34