Cascade deletion using foreign keys in MySQL

Source: Internet
Author: User

First, only the InnoDB Engine allows the use of foreign keys in MySQL versions available in the product environment (4.0.x and 4.1.x). Therefore, our data tables must use the InnoDB engine.

Next, we will first create the following database tables for testing:

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:
1. the InnoDB engine must be used;
2. The foreign key must be indexed );
3. The foreign key binding relationship uses "on delete cascade", which means that if the data corresponding to the foreign key is deleted, the associated data is completely deleted, for more information, see the InnoDB documentation in the MySQL manual;

OK. Then we can insert the 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 ');

Let's take a look at the status of the current data table:

Mysql>; show tables;
+ ---------------- +
| Tables_in_test |
+ ---------------- +
| Roottb |
| Subtb |
+ ---------------- +
2 rows in set (0.00 sec)

Mysql>; select * from 'roottb ';
+ ---- + ------------------ +
| Id | data |
+ ---- + ------------------ +
| 1 | test root line 1 |
| 2 | test root line 2 |
| 3 | test root line 3 |
+ ---- + ------------------ +
3 rows in set (0.05 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 |
| 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 |
+ ---- + -------- + ---------------------------- +
9 rows in set (0.01 sec)

Well, everything is normal. Well, next we will test our cascade deletion function.

We will 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)

Well, check that the data in the subtb table is automatically deleted and the test is successful.

Conclusion: The cascading deletion is successful using the foreign key in MySQL!

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.