首页 > 数据库技术 > 详细

MySQL外键约束On Delete、On Update

时间:2017-08-02 14:16:57      阅读:267      评论:0      收藏:0      [点我收藏+]
使用外键实现
Create
Table: CREATE TABLE `child` ( `par_id` int(11) NOT NULL, `child_id` int(11) NOT NULL, PRIMARY KEY (`par_id`,`child_id`), FOREIGN KEY (`par_id`) REFERENCES `parent` (`par_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Create Table: CREATE TABLE `parent` ( `par_id` int(11) NOT NULL, PRIMARY KEY (`par_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
使用mysql trigger实现
mysql> show triggers\G$ *************************** 1. row *************************** Trigger: t12 Event: UPDATE Table: t3 Statement: begin update t4 set tid = new.id where tid = old.id; end Timing: AFTER Created: NULL sql_mode: NO_ENGINE_SUBSTITUTION Definer: root@localhost character_set_client: gbk collation_connection: gbk_chinese_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Trigger: t11 Event: DELETE Table: t3 Statement: begin update t4 set tid = 0 where tid = old.id; end Timing: AFTER Created: NULL sql_mode: NO_ENGINE_SUBSTITUTION Definer: root@localhost character_set_client: gbk collation_connection: gbk_chinese_ci Database Collation: utf8_general_ci 2 rows in set (0.02 sec)

 

MySQL外键约束On Delete、On Update

原文:http://www.cnblogs.com/abc31/p/7273613.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!