MySQL外键约束On DeleteOn Update

Posted 随笔记录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL外键约束On DeleteOn Update相关的知识,希望对你有一定的参考价值。

使用外键实现
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 DeleteOn Update的主要内容,如果未能解决你的问题,请参考以下文章

MySQL外键约束On Delete和On Update的使用

SQL表与表之间建立外键约束之后,怎么建立连级更新和删除?

Mysql2外键约束,多表/子查询,事务

mysql 约束

为啥我的模型的“on_delete=models.CASCADE”不生成级联外键约束?

Mysql的外键约束内外连接查询以及锁