溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何在MySQL中利用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除

發(fā)布時(shí)間:2021-07-08 17:52:12 來(lái)源:億速云 閱讀:255 作者:chen 欄目:MySQL數(shù)據(jù)庫(kù)

本篇內(nèi)容介紹了“如何在MySQL中利用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!



下面,我們先創(chuàng)建以下測(cè)試用數(shù)據(jù)庫(kù)表:


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、外鍵綁定關(guān)系這里使用了“ ON DELETE CASCADE”,意思是如果外鍵對(duì)應(yīng)數(shù)據(jù)被刪除,將關(guān)聯(lián)數(shù)據(jù)完全刪除,更多信息請(qǐng)參考MySQL手冊(cè)中關(guān)于InnoDB的文檔;

好,接著我們?cè)賮?lái)插入測(cè)試數(shù)據(jù):


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



我們先看一下當(dāng)前數(shù)據(jù)表的狀態(tài):


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)



嗯,一切都正常,好,下面我們要試驗(yàn)我們的級(jí)聯(lián)刪除功能了。

我們將只刪除roottb表中id為2的數(shù)據(jù)記錄,看看subtb表中rootid為2的相關(guān)子紀(jì)錄是否會(huì)自動(dòng)刪除:


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表中對(duì)應(yīng)數(shù)據(jù)確實(shí)自動(dòng)刪除了,測(cè)試成功。

結(jié)論:在MySQL中利用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除成功!

“如何在MySQL中利用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI