mysql級聯刪除

2021-09-01 01:32:20 字數 3560 閱讀 6919

首先,目前在產品環境可用的mysql版本(指4.0.x和4.1.x)中,只有innodb引擎才允許使用外來鍵,所以,我們的資料表必須使用innodb引擎。

但mysql 5版本以上不需指定innodb引擎。

下面,我們先建立以下測試用資料庫表:

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

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

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)

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

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 多表級聯刪除

備忘一下 例如存在3個表,a,b,c.a,b是一對多關係 a id,name a b id,aid,name b b,c是一對多關係 b id,aid,name b c id,bid,name c 實現效果 刪除乙個a的id,與之關聯的b內aid的所有元組都刪除,b刪除就會把c關聯b的bid的所有元...

phpmyadmin級聯刪除,mysql

資料庫中的好東西 今天在做專案的時候偶然遇到了級聯刪除的問題,想到mssqlserver和access有級聯刪除,然後去找了下資料,mysql也是有的,有些孤陋寡聞了.在仔細的尋找過後,在phpmyadmin中發現了建立級聯刪除的方法.首先需要兩個表 乙個表 使用者 user id username...

mysql 設定級聯刪除

以下方法來自網路 create table person fileinfo person id int not null,fileinfo id int not null primary key person id,fileinfo id foreign key fileinfo id refere...