Can I define multiple events in one Trigger declaration in mysql?(我可以在 mysql 的一个触发器声明中定义多个事件吗?)
问题描述
这是我的要求,我有一个 mysql 表,任何更改(插入/删除/更新)都应该以完全相同的方式处理.根据mysql文档创建触发器语法如下:
Here is my requirement, I have a mysql table on which any change (insert/delete/update) should be handled in exactly same way. According to mysql documentation create trigger syntax is as follows:
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time **trigger_event**
ON tbl_name FOR EACH ROW
trigger_body
当我尝试放置多个事件时,会抛出语法错误.一种解决方案是我可以编写一个过程和 3 个触发器(每个事件一个)并从所有触发器调用相同的过程.
When I'm trying to put more than one event, its throwing syntax error. One solution is I can write one procedure and 3 triggers (one for each event) and call the same procedure from all the triggers.
对此有什么复杂的解决方案吗??
Is there any sophisticated solution for this ??
推荐答案
没有.在 MySQL 中,触发器用于单个 trigger_event.触发器必须是 BEFORE
或 AFTER
和 INSERT
、UPDATE
、DELETE之一代码>.
No. In MySQL, a trigger is for a single trigger_event. A trigger has to be either BEFORE
or AFTER
and one of INSERT
, UPDATE
, DELETE
.
如果我们有很多跨触发器事件共享的逻辑(需要在多个触发器中复制的逻辑),我们可以编写/创建一个 PROCEDURE 来封装该逻辑,并从多个触发器的主体中调用该过程触发器.
If we have lots of logic that is shared across the trigger events (logic that would need to be duplicated in multiple triggers), we can write/create a PROCEDURE to encapsulate that logic, and call that procedure from the body of multiple triggers.
这篇关于我可以在 mysql 的一个触发器声明中定义多个事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在 mysql 的一个触发器声明中定义多个事件吗?


基础教程推荐
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01