生活随笔
收集整理的這篇文章主要介紹了
MySql级联删除和更新
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(一)利用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除
1、先建立測(cè)試數(shù)據(jù)庫(kù)
[sql]?view plaincopy
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;??
注意:數(shù)據(jù)表必須使用InnoDB引擎。
?????????? 外鍵必須建立索引
????????? 外鍵綁定關(guān)系使用了“ on delete cascade ”
2、插入測(cè)試數(shù)據(jù)
[sql]?view plaincopy
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');??
3、查看數(shù)據(jù)表的狀態(tài)
4、試驗(yàn)級(jí)聯(lián)刪除功能
只刪除roottb表中id為2的數(shù)據(jù)記錄,看看subtb表中rootid為2的相關(guān)子紀(jì)錄是否會(huì)自動(dòng)刪除!
[sql]?view plaincopy
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)??
以上部分轉(zhuǎn)自于:bbs.chinaunix.net/forum.php?mod=viewthread&tid=462977
(二)利用觸發(fā)器實(shí)現(xiàn)級(jí)聯(lián)刪除
下面給出實(shí)例
1、建立測(cè)試數(shù)據(jù)庫(kù)
[sql]?view plaincopy
CREATE?TABLE?`root_trigger`?(?? ??`id`?INT(11)?UNSIGNED?AUTO_INCREMENT?NOT?NULL,?? ??`data`?VARCHAR(100)?NOT?NULL?DEFAULT?'',?? ??PRIMARY?KEY?(`id`)?? );?? ?? CREATE?TABLE?`sub_trigger`?(?? ??`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`)???? );??
2、插入測(cè)試數(shù)據(jù)
[sql]?view plaincopy
INSERT?INTO?`root_trigger`?(`id`,`data`)?? ??VALUES?('1',?'test?root?line?1'),?? ?????????('2',?'test?root?line?2'),?? ?????????('3',?'test?root?line?3');?? ?? INSERT?INTO?`sub_trigger`?(`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');??
3、建立(級(jí)聯(lián)/同步)刪除的觸發(fā)器
[sql]?view plaincopy
drop?trigger?if?exists?t_afterdelete_on_sub;?? create?trigger?t_afterdelete_on_sub?? after?delete?on?root_trigger?? for?each?row?? begin??? ??????delete?from?sub_trigger?where?rootid=old.id;???????? end;??
4、刪除root_trigger表中的id=2的記錄,再查看sub_trigger中rootid=2的記錄是否刪除
刪除成功即實(shí)現(xiàn)了,觸發(fā)器的級(jí)聯(lián)刪除!
總結(jié)
以上是生活随笔為你收集整理的MySql级联删除和更新的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。