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

sql刪除關聯表

發布時間: 2023-08-24 22:40:52

1. sql 如何把兩個表相關聯的數據一同刪除

其實你這個問題最好用資料庫本身的外鍵解決。就是在子表建立指向父表的外鍵。當刪除主表數據時,只要加上delete語句加上 on cascade,所有子表引用的數據就刪除了。

2. 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個人建議用事務處理。

3. sql server資料庫怎樣同時刪除兩張表的數據

1、打開SQL Server 2008 並連接一個資料庫。

4. SQL中怎麼刪除2張表中有關聯的數據信息

可以使用兩種方式
1:刪除子表數據,再刪除主表數據
2:如果主細表中建立了級聯刪除操作,直接刪除主表數據即可

5. 如何用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('表名')

刪除的一般語法為:
alter table 表名 drop constraint 外鍵約束名;

6. sql多表關聯刪除

刪除多表關聯數據的三種方法

1、級聯刪除

createtablea
(
idvarchar(20)primarykey,
passwordvarchar(20)notnull
)
createtableb
(
idintidentity(1,1)primarykey,
namevarchar(50)notnull,
userIdvarchar(20),
foreignkey(userId)referencesa(id)ondeletecascade
)



表B創建了外碼userId 對應A的主碼ID,聲明了級聯刪除

測試數據:

insertavalues('11','aaa')
insertavalues('23','aaa')
insertbvalues('da','11')
insertbvalues('das','11')
insertbvalues('ww','23')


刪除A表內id為『11』的數據,發現B表內userId 為「11」也被資料庫自動刪除了

deleteawhereid='11'


2、採用存儲過程

A表:AID Aname 主健:AID

B表:BID BelongAID Bname 主健:BID,外健:BelongAID

C表:CID BelongBID Cname 主健:CID,外健:BelongBID

D表:DID BelongCID Dname 主健:DID,外健:BelongCID

其中:

A表和B表通過A.AID和B.BelongAID 創建了外健關系

B表和C表通過B.BID和C.BelongBID 創建了外健關系

C表和D表通過C.CID和D.BelongCID 創建了外健關系


3、採用觸發器

刪除Class表中的一條記錄的同時刪除該記錄Class_No欄位值在Student表中對應的記錄。

CreateTriggerClass_delete
onClass
fordelete
as
begin
deletefromStudent
whereClass_No=(selectClass_Nofromdeleted)
end
熱點內容
vps自動腳本 發布:2025-03-15 20:50:29 瀏覽:59
php刷新重復提交 發布:2025-03-15 20:50:26 瀏覽:306
艾莫迅plc編程電纜 發布:2025-03-15 20:44:05 瀏覽:302
妖妖靈腳本 發布:2025-03-15 20:36:56 瀏覽:256
公司自己搭建ftp 發布:2025-03-15 20:36:07 瀏覽:62
如何增加配置使半袖變得不單調 發布:2025-03-15 20:33:37 瀏覽:350
linux顯示目錄 發布:2025-03-15 20:30:42 瀏覽:661
素數演算法表示 發布:2025-03-15 20:24:02 瀏覽:843
大話西遊手游怎麼看伺服器等級 發布:2025-03-15 20:21:53 瀏覽:222
rsa加密c源代碼 發布:2025-03-15 19:53:55 瀏覽:694