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

sql級聯刪除

發布時間: 2022-08-18 06:39:58

sql server中如何級聯刪除

可以用下邊的方法,僅供參考:

--創建測試主表.ID是主鍵.
CREATETABLEtest_main(
idINTNOTNULL,
valueVARCHAR(10),
PRIMARYKEY(id)
);


--創建測試子表.
CREATETABLEtest_sub(
idINTNOTNULL,
main_idINT,
valueVARCHAR(10),
PRIMARYKEY(id)
);


--插入測試主表數據.
INSERTINTOtest_main(id,value)VALUES(1,'ONE');
INSERTINTOtest_main(id,value)VALUES(2,'TWO');

--插入測試子表數據.
INSERTINTOtest_sub(id,main_id,value)VALUES(1,1,'ONEONE');
INSERTINTOtest_sub(id,main_id,value)VALUES(2,2,'TWOTWO');

然後,創建外鍵,使用ONDELETECASCADE選項,刪除主表的時候,同時刪除子表

ALTERTABLEtest_subADDCONSTRAINTmain_id_consFOREIGNKEY(main_id)REFERENCEStest_mainONDELETECASCADE;

執行刪除:

DELETEFROMTEST_MAINWHEREID=1;

最後:

SELECT*FROMTEST_MAIN;

結果子表中就只有ID=2的記錄,也就說明級聯刪除成功。

② 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

③ SQl語句的級聯刪除問題

刪除應該有順序
1,刪除link表
delete from ref,link where ref.link_code=link.link_code and link_id=?
delete from link where link_id=?
2,刪除plan表
delete from ref,plan where ref.plan_code=plan.plan_code and plan_id=?
delete from plan where plan_id=?

④ 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中級聯刪除,級聯更新是怎麼理解的

級聯刪除你可以把它認為是一個觸發器,也就是你刪除主表中的數據,那麼從表中的相關聯的也就一起刪除了。。。看個例子:======================create table a
(
id varchar(20) primary key,
password varchar(20) not null
)

create table b
(
id int identity(1,1) primary key,
name varchar(50) not null,
userId varchar(20),
foreign key (userId) references a(id) on delete cascade
)
表B創建了外碼userId 對應A的主碼ID,聲明了級聯刪除
測試數據:
insert a values ('11','aaa')
insert a values('23','aaa')
insert b values('da','11')
insert b values('das','11')
insert b values('ww','23')
刪除A表內id為『11』的數據,發現B表內userId 為「11」也被資料庫自動刪除了,這就是級聯刪除
delete a where id='11'=============================================================級聯更新也大同小異。。只是關鍵字為:on update希望回答對你有所幫助........

⑥ SQL 怎麼級聯刪除語句

方法一:
MSSQLSERVER
是支持
級聯更新

級聯刪除
的,你要在建立
外鍵
的時候,要指定
EP:
alter
table
tb_userroles
add
constraint
FK_TB_USERR_RELATIONS_TB_FUNCT
foreign
key
(gnbm)
references
tb_function
(gnbm)
on
update
cascade
on
delete
cascade
方法二:用資料庫觸發器

熱點內容
自動欣悅版有哪些配置 發布:2024-03-29 03:48:26 瀏覽:286
如何用腳本搶 發布:2024-03-29 03:01:59 瀏覽:119
火影忍者手游配置怎麼調 發布:2024-03-29 02:53:53 瀏覽:102
編程畫櫻花 發布:2024-03-29 02:11:24 瀏覽:473
騰訊雲伺服器1mb老掉線 發布:2024-03-29 01:56:11 瀏覽:215
執行sql語句的存儲過程 發布:2024-03-29 01:52:37 瀏覽:697
婚紗攝影腳本 發布:2024-03-29 01:47:40 瀏覽:901
我的世界伺服器咋開外掛 發布:2024-03-29 01:07:45 瀏覽:456
sql寫報表 發布:2024-03-29 01:03:23 瀏覽:306
家用伺服器怎麼選 發布:2024-03-29 00:49:18 瀏覽:402