当前位置:Gxlcms > 数据库问题 > MySQL外键约束On Delete、On Update

MySQL外键约束On Delete、On Update

时间:2021-07-01 10:21:17 帮助过:2人阅读

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

标签:innodb   ble   mode   weight   tab   nbsp   char   trigger   new   

人气教程排行