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

BSDLite發表於2007-08-12
在MySQL中利用外來鍵實現級聯刪除(轉)[@more@]首先,目前在產品環境可用的MySQL版本(指4.0.x和4.1.x)中,只有InnoDB引擎才允許使用外來鍵,所以,我們的資料表必須使用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);
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中利用外來鍵實現級聯刪除成功!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617542/viewspace-949422/,如需轉載,請註明出處,否則將追究法律責任。

相關文章