是的。只是你不能用那個引擎。
編輯。你可以編寫一個觸發器,一旦你刪除了表中的記錄,刪除所有其他表中的所有子記錄。
好的。我給你寫了一個例子:
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);
任何想法如何使用觸發器做到這一點? – B4NZ41 2011-03-17 17:43:51
哇,非常好,簡單的解釋,我會適應我的代碼,真的很適合完美!謝謝@nick – B4NZ41 2011-03-17 18:39:55
不客氣。再見:) – 2011-03-17 18:55:44