MySql级联删除和更新

(一)利用外键实现级联删除

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)

以上部分转自于:bbs.chinaunix.net/forum.php?mod=viewthread&tid=462977


(二)利用触发器实现级联删除

下面给出实例

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_sub
after delete on root_trigger
for each row
begin 
      delete from sub_trigger where rootid=old.id;      
end;

4、删除root_trigger表中的id=2的记录,再查看sub_trigger中rootid=2的记录是否删除

删除成功即实现了,触发器的级联删除!


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页