红联Linux门户
Linux帮助

在MySQL中利用外键实现级联删除

发布时间:2006-04-02 00:39:04来源:红联作者:reing
首先,目前在产品环境可用的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中利用外键实现级联删除成功!
文章评论

共有 81 条评论

  1. 207.67.117.* 于 2007-09-05 13:48:51发表:

    http://emeraldring.fora.pl/
    http://emeraldring.fora.pl/
    http://emeraldring.fora.pl/ - emerald cut wedding ring

  2. 69.147.76.* 于 2007-09-05 13:48:50发表:

    http://emeraldring.fora.pl/
    http://emeraldring.fora.pl/
    http://emeraldring.fora.pl/ - emerald cut wedding ring

  3. 211.140.138.* 于 2007-09-02 07:05:25发表:

    Hi, nice very nice page..!
    fund
    fund
    abb fund
    fund raising counselor organization
    fund raising counselor organization

    Good luck !
    PS: do you listen Linkin Park ?

  4. 61.241.146.* 于 2007-08-31 22:14:37发表:

    Hi, nice very nice page..!
    mutual fund company - http://fund.freewebpages.org/cookie-dough-fund-raiser/index.html
    state insurance fund
    memorial scholarship fund - http://fund.freewebpages.org/memorial-scholarship-fund/index.html
    memorial scholarship fund
    alaska permanent fund dividend - http://fund.freewebpages.org/fund/index.html

    Good luck !
    PS: do you listen Linkin Park ?

  5. 61.241.146.* 于 2007-08-30 14:40:48发表:

    Hi, nice very nice page..!
    sample fund raising letter
    child defense fund
    mutual fund research
    mutual fund research
    fund raising event - http://fund.freewebpages.org/child-defense-fund/index.html

    Good luck !
    PS: do you listen Linkin Park ?

  6. 193.183.81.* 于 2007-08-29 10:00:02发表:

    Hi, nice very nice page..!
    best money market rate - http://www.volny.cz/moneymarket1/money-market-savings-account/
    money market rate - http://www.volny.cz/moneyrate/money-market-rate/
    money market fund
    capital money market
    money market mutual funds

    Good luck !
    PS: do you listen Linkin Park ?

  7. 218.66.36.* 于 2007-08-29 09:59:49发表:

    Hi, nice very nice page..!
    best money market rate - http://www.volny.cz/moneymarket1/money-market-savings-account/
    money market rate - http://www.volny.cz/moneyrate/money-market-rate/
    money market fund
    capital money market
    money market mutual funds

    Good luck !
    PS: do you listen Linkin Park ?

  8. 59.151.29.* 于 2007-08-25 23:09:32发表:

    Hi, nice very nice page..!
    ford money market account - http://volny.cz/capitalmoney/capital-money-market/
    ford money market
    money market fund
    money market savings account
    money banking and financial market

    Good luck !
    PS: do you listen Linkin Park ?

  9. 61.90.248.* 于 2007-08-24 08:12:58发表:

    Hi, nice very nice page..!
    capital money market
    money market account rate - http://www.volny.cz/businessmoney/best-rate-on-money-market-account
    money market account rate - http://www.volny.cz/moneyrate/money-market-funds/
    money market rate
    money market rate

    Good luck !
    PS: do you listen Linkin Park ?

  10. 0.0.0.* 于 2007-08-14 13:39:38发表:

    good work !
    chandelier lamp shades
    chandelier lamp shades
    chandelier lamp shades - www.desklamp1.fora.pl


    buy viagra online
    buy viagra online
    buy viagra online - www.buyviagraonlinex.fora.pl

    .
    norwegian pearl
    norwegian pearl
    norwegian pearl - http://volny.cz/norwegianpearl
    blackberry pearl white
    blackberry pearl white
    blackberry pearl white - http://volny.cz/blackberrypearlwhite
    pink pearls
    pink pearls
    pink pearls - http://volny.cz/pinkpearls
    blackberry pearl review
    blackberry pearl review
    blackberry pearl review - http://volny.cz/blackberrypearlrev
    pearl white
    pearl white
    pearl white - http://volny.cz/pearlwhite
    good luck

  11. 219.159.67.* 于 2007-08-07 18:59:24发表:

    desk lamp
    desk lamp - www.halogendesklamp.freehostia.com/desk-lamp/

    lamp shades
    lamp shades - www.halogendesklamp.freehostia.com/lamp-shades/

    office desk lamp
    office desk lamp - www.halogendesklamp.freehostia.com/office-desk-lamp/

  12. 217.141.105.* 于 2007-08-05 17:25:06发表:

    nice site... look on my site

    http://www.desklamp.fora.pl - desk lamp
    http://www.lampshades.fora.pl - lamp shades

  13. 213.132.242.* 于 2007-08-04 01:37:13发表:

    thnx... nice site
    and i wan't see my:
    http://www.tablelamp.fora.pl/
    http://www.tablelamp.fora.pl/
    http://www.tablelamp.fora.pl/ - table lamp

  14. 216.244.231.* 于 2007-08-02 09:17:24发表:

    bondage cartoon drawings http://cartoonfox.150m.com/bondage_cartoon_drawings.html bondage cartoon drawings bondage cartoon drawings

    extreme adult comic art drawings http://cartoonfox.150m.com/extreme_adult_comic_art_drawings.html extreme adult comic art drawings extreme adult comic art drawings

    super extreme cartoon http://cartoonfox.150m.com/super_extreme_cartoon.html super extreme cartoon super extreme cartoon

    cartoon extreme http://cartoonfox.150m.com/cartoon_extreme.html cartoon extreme cartoon extreme

    adult comics bondage toon http://cartoonfox.150m.com/adult_comics_bondage_toon.html adult comics bondage toon adult comics bondage toon

  15. 218.94.80.* 于 2007-07-31 19:42:58发表:

    gmc trucks here www.gmctruck.fora.pl
    gmc from america www.gmctruck.fora.pl
    real gmc www.gmctruck.fora.pl


    and www.emeraldring.fora.pl rings

  16. 59.93.203.* 于 2007-07-19 22:09:26发表:

    naruto cartoon porn http://adultcomics19jul.tripod.com/naruto_cartoon_porn.html naruto cartoon porn naruto cartoon porn
    wonder woman cartoon porn http://adultcomics19jul.tripod.com/wonder_woman_cartoon_porn.html wonder woman cartoon porn wonder woman cartoon porn
    free adult porn cartoon http://adultcomics19jul.tripod.com/free_adult_porn_cartoon.html free adult porn cartoon free adult porn cartoon
    free cartoon porn game http://adultcomics19jul.tripod.com/free_cartoon_porn_game.html free cartoon porn game free cartoon porn game
    cartoon porn futurama http://adultcomics19jul.tripod.com/cartoon_porn_futurama.html cartoon porn futurama cartoon porn futurama

  17. 85.28.74.* 于 2007-07-17 14:32:14发表:

    http://8bff877c53e4512d53d4c004ad5d9a6d-t.xkktxb.org 8bff877c53e4512d53d4c004ad5d9a6d http://8bff877c53e4512d53d4c004ad5d9a6d-b1.xkktxb.org 8bff877c53e4512d53d4c004ad5d9a6d http://8bff877c53e4512d53d4c004ad5d9a6d-b3.xkktxb.org 8d1f2bfe3cbc5359328d95464cab8b7c