文章来源:http://blog.csdn.net/doupei2006/article/details/7949077
(一)利用外键实现级联删除
1、先建立测试数据库
CREATE TABLE `roottb` ( `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL, `data` VARCHAR(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`)) TYPE=InnoDB;CREATE TABLE `subtb` ( `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL, `rootid` INT(11) UNSIGNED NOT NULL DEFAULT '0', `data` VARCHAR(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`), INDEX (`rootid`), FOREIGN KEY (`rootid`) REFERENCES roottb(`id`) ON DELETE CASCADE) TYPE=InnoDB;
注意:数据表必须使用InnoDB引擎。
外键必须建立索引
外键绑定关系使用了“ on delete cascade ”
2、插入测试数据
INSERT INTO `roottb` (`id`,`data`) VALUES ('1', 'test root line 1'), ('2', 'test root line 2'), ('3', 'test root line 3');INSERT INTO `subtb` (`id`,`rootid`,`data`) VALUES ('1', '1', 'test sub line 1 for root 1'), ('2', '1', 'test sub line 2 for root 1'), ('3', '1', 'test sub line 3 for root 1'), ('4', '2', 'test sub line 1 for root 2'), ('5', '2', 'test sub line 2 for root 2'), ('6', '2', 'test sub line 3 for root 2'), ('7', '3', 'test sub line 1 for root 3'), ('8', '3', 'test sub line 2 for root 3'), ('9', '3', 'test sub line 3 for root 3');
3、查看数据表的状态
4、试验级联删除功能
只删除roottb表中id为2的数据记录,看看subtb表中rootid为2的相关子纪录是否会自动删除!
mysql>; delete from `roottb` where `id`='2';Query OK, 1 row affected (0.03 sec)mysql>; select * from `roottb`;+----+------------------+| id | data |+----+------------------+| 1 | test root line 1 || 3 | test root line 3 |+----+------------------+2 rows in set (0.00 sec)mysql>; select * from `subtb`;+----+--------+----------------------------+| id | rootid | data |+----+--------+----------------------------+| 1 | 1 | test sub line 1 for root 1 || 2 | 1 | test sub line 2 for root 1 || 3 | 1 | test sub line 3 for root 1 || 7 | 3 | test sub line 1 for root 3 || 8 | 3 | test sub line 2 for root 3 || 9 | 3 | test sub line 3 for root 3 |+----+--------+----------------------------+6 rows in set (0.01 sec)
(二)利用触发器实现级联删除
下面给出实例
1、建立测试数据库
CREATE TABLE `root_trigger` ( `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL, `data` VARCHAR(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`));CREATE TABLE `sub_trigger` ( `id` INT(11) UNSIGNED AUTO_INCREMENT NOT NULL, `rootid` INT(11) UNSIGNED NOT NULL DEFAULT '0', `data` VARCHAR(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`) );
2、插入测试数据
INSERT INTO `root_trigger` (`id`,`data`) VALUES ('1', 'test root line 1'), ('2', 'test root line 2'), ('3', 'test root line 3');INSERT INTO `sub_trigger` (`id`,`rootid`,`data`) VALUES ('1', '1', 'test sub line 1 for root 1'), ('2', '1', 'test sub line 2 for root 1'), ('3', '1', 'test sub line 3 for root 1'), ('4', '2', 'test sub line 1 for root 2'), ('5', '2', 'test sub line 2 for root 2'), ('6', '2', 'test sub line 3 for root 2'), ('7', '3', 'test sub line 1 for root 3'), ('8', '3', 'test sub line 2 for root 3'), ('9', '3', 'test sub line 3 for root 3');
3、建立(级联/同步)删除的触发器
drop trigger if exists t_afterdelete_on_sub;create trigger t_afterdelete_on_subafter delete on root_triggerfor each rowbegin delete from sub_trigger where rootid=old.id; end;