我可以在 mysql 的一个触发器声明中定义多个事件吗?

Can I define multiple events in one Trigger declaration in mysql?(我可以在 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.触发器必须是 BEFOREAFTERINSERTUPDATEDELETE.

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 的一个触发器声明中定义多个事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)