9

我有一张我称之为设备的表,还有另外 8 张我称之为 device_child1 的表,依此类推,直到device_child8

所有这些表之间的公共字段是cod_equip,通过该字段,我可以使用设备父表识别我的所有子设备表。

我需要在设备移动时从设备中删除数据,但我需要删除我所有表中的数据设备_child1到设备_child8。

然后我记得我在innoDB引擎中使用过DELETE CASCADE,但现在我使用的是MyISAM engina,这是个问题吗?

任何帮助,都会真正澄清......

4

1 回答 1

14

是的。只是你不能用那个引擎。

编辑。您可以编写一个触发器,一旦您删除表中的一条记录,就会删除所有其他表中的所有子记录。

行。我给你写了一个例子:

 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);
于 2011-03-17T17:31:38.697 回答