簡體   English   中英

如何在MySQL MyISAM存儲引擎上使用刪除級聯?

[英]How to use delete cascade on MySQL MyISAM storage engine?

我有一個叫做設備的表,還有8個我稱之為equipment_child1的表,依此類推,直到equipment_child8

所有表之間的commom字段是cod_equip ,使用此字段我可以使用設備父表識別所有子設備表。

我需要在設備移動時從設備中 刪除數據,但我需要刪除所有表equipment_child1到equipment_child8的數據。

然后我記得我在innoDB引擎中使用了DELETE CASCADE ,但現在我正在使用MyISAM engina,這是一個問題嗎?

任何幫助,都會澄清......

是。 只是你不能用那個引擎。

編輯。 您可以編寫一個觸發器,一旦刪除表中的記錄,就刪除所有其他表中的所有子記錄。

好。 我給你寫了一個例子:

 create table tab1 (
 id int )
 engine = myisam;

insert into tab1 values (1),(2),(3),(4); 

 create table tab2(
 id int not null auto_increment primary key,
 id_tab1 int
 ) engine = myisam;

 insert into tab2 (id_tab1) values (1),(2),(2),(3),(4);

 create table tab3(
 id int not null auto_increment primary key,
 id_tab1 int
 ) engine = myisam;

  insert into tab3 (id_tab1) values (1),(2),(2),(3),(2);


delimiter //
create trigger deletecascade after delete on tab1
for each row
begin
delete from tab2 where id_tab1 = old.id;
delete from tab3 where id_tab1 = old.id;
end; //
delimiter ;

delete from tab1 where id = 2;

希望它有所幫助。

編輯。 顯然,即使你同時從table1中刪除了更多的id,它仍然有效:

delete from tab1 where id in (2,3,4);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM