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

2021-08-22 08:05:33 字數 3102 閱讀 3459

url:

createtable`roottb`(

`id`int(11)unsignedauto_incrementnotnull,

`data`varchar(100)notnulldefault'',

primarykey(`id`)

)type=innodb;

createtable`subtb`(

`id`int(11)unsignedauto_incrementnotnull,

`rootid`int(11)unsignednotnulldefault'0',

`data`varchar(100)notnulldefault'',

primarykey(`id`),

index(`rootid`),

foreignkey(`rootid`)referencesroottb(`id`)ondeletecascade

)type=innodb;

注意:

1、必須使用innodb引擎;

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

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

insertinto`roottb`(`id`,`data`)

values('1','testrootline1'),

('2','testrootline2'),

('3','testrootline3');

insertinto`subtb`(`id`,`rootid`,`data`)

values('1','1','testsubline1forroot1'),

('2','1','testsubline2forroot1'),

('3','1','testsubline3forroot1'),

('4','2','testsubline1forroot2'),

('5','2','testsubline2forroot2'),

('6','2','testsubline3forroot2'),

('7','3','testsubline1forroot3'),

('8','3','testsubline2forroot3'),

('9','3','testsubline3forroot3');

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

mysql>;showtables;

+----------------+

|tables_in_test|

+----------------+

|roottb|

|subtb|

+----------------+

2rowsinset(0.00sec)

mysql>;select*from`roottb`;

+----+------------------+

|id|data|

+----+------------------+

|1|testrootline1|

|2|testrootline2|

|3|testrootline3|

+----+------------------+

3rowsinset(0.05sec)

mysql>;select*from`subtb`;

+----+--------+----------------------------+

|id|rootid|data|

+----+--------+----------------------------+

|1|1|testsubline1forroot1|

|2|1|testsubline2forroot1|

|3|1|testsubline3forroot1|

|4|2|testsubline1forroot2|

|5|2|testsubline2forroot2|

|6|2|testsubline3forroot2|

|7|3|testsubline1forroot3|

|8|3|testsubline2forroot3|

|9|3|testsubline3forroot3|

+----+--------+----------------------------+

9rowsinset(0.01sec)

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

mysql>;deletefrom`roottb`where`id`='2';

queryok,1rowaffected(0.03sec)

mysql>;select*from`roottb`;

+----+------------------+

|id|data|

+----+------------------+

|1|testrootline1|

|3|testrootline3|

+----+------------------+

2rowsinset(0.00sec)

mysql>;select*from`subtb`;

+----+--------+----------------------------+

|id|rootid|data|

+----+--------+----------------------------+

|1|1|testsubline1forroot1|

|2|1|testsubline2forroot1|

|3|1|testsubline3forroot1|

|7|3|testsubline1forroot3|

|8|3|testsubline2forroot3|

|9|3|testsubline3forroot3|

+----+--------+----------------------------+

6rowsinset(0.01sec)

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

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

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

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

在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中利用外來鍵實現級聯刪除!

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