在MySQL中利用外來鍵實現級聯刪除

2021-04-26 16:18:56 字數 4015 閱讀 9797

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;

注意:

1、必須使用innodb引擎;

2、外來鍵必須建立索引(index);

3、外來鍵繫結關係這裡使用了「 on delete cascade」,意思是如果外來鍵對應資料被刪除,將關聯資料完全刪除,更多資訊請參考mysql手冊中關於innodb的文件;

好,接著我們再來插入測試資料:

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');

我們先看一下當前資料表的狀態:

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)

嗯,一切都正常,好,下面我們要試驗我們的級聯刪除功能了。

我們將只刪除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)

嗯,看subtb表中對應資料確實自動刪除了,測試成功。

結論:在mysql中利用外來鍵實現級聯刪除成功!

在MySQL中利用外來鍵實現級聯刪除

url 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 ...

在MySQL中利用外來鍵實現級聯刪除

url createtable roottb id int 11 unsignedauto incrementnotnull,data varchar 100 notnulldefault primarykey id type innodb createtable subtb id int 11 u...

在MySQL中利用外來鍵實現級聯刪除!

下面,我們先建立以下測試用資料庫表 create table roottb id int 11 unsigned auto increment not null,data varchar 100 not null default primary key id type innodb create t...