MySQL cascade deletion and update

Source: Internet
Author: User

(1) cascading deletion using foreign keys

1. Create a test database first

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;

Note: Data Tables must use the InnoDB engine.

The foreign key must be indexed.

The foreign key binding relationship uses "on Delete cascade"

2. Insert Test Data

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. view the status of the data table

4. Test the cascade deletion function

Delete only the data records with the ID of 2 in the roottb table to see if the related sub-records with the ID of 2 in the subtb table are automatically deleted!

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)

The above part is transferred from: bbs.chinaunix.net/forum.php? MoD = viewthread & tid = 462977.

(2) cascade deletion using triggers

The following example is provided:

1. Create a Test Database

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 Test Data

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. Create a trigger for (cascade/synchronize) Deletion

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;

4. Delete the record with ID = 2 in the root_trigger table, and check whether the record with rootid = 2 in sub_trigger is deleted.

The deletion is successful, and the trigger is deleted cascade!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.