生活随笔
收集整理的這篇文章主要介紹了
MySql级联删除和更新
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
(一)利用外鍵實現級聯刪除
1、先建立測試數據庫
[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;??
注意:數據表必須使用InnoDB引擎。
?????????? 外鍵必須建立索引
????????? 外鍵綁定關系使用了“ on delete cascade ”
2、插入測試數據
[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、查看數據表的狀態
4、試驗級聯刪除功能
只刪除roottb表中id為2的數據記錄,看看subtb表中rootid為2的相關子紀錄是否會自動刪除!
[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)??
以上部分轉自于:bbs.chinaunix.net/forum.php?mod=viewthread&tid=462977
(二)利用觸發器實現級聯刪除
下面給出實例
1、建立測試數據庫
[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、插入測試數據
[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、建立(級聯/同步)刪除的觸發器
[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的記錄是否刪除
刪除成功即實現了,觸發器的級聯刪除!
總結
以上是生活随笔為你收集整理的MySql级联删除和更新的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。