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

时间:2022-03-12 00:03:35

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:

这是我的要求,我有一个mysql表,任何更改(插入/删除/更新)都应该以完全相同的方式处理。根据mysql文档创建触发器语法如下:

    CREATE
     [DEFINER = { user | CURRENT_USER }]
     TRIGGER trigger_name
     trigger_time **trigger_event**
     ON tbl_name FOR EACH ROW
     trigger_body

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 ??

有什么复杂的解决办法吗?

1 个解决方案

#1


7  

No. Only one "event" is supported. The trigger has to be either BEFORE or AFTER one of INSERT, UPDATE, DELETE.

不。只支持一个“事件”。触发器必须在插入、更新、删除之前或之后。

If you have lots of logic that is "shared", you could write a procedure, and call the procedure from the body of the trigger.

如果您有许多“共享”的逻辑,您可以编写一个过程,并从触发器的主体调用该过程。

The sophisticated solution to this is to use a different DBMS. Otherwise, you have to work within the confines of what MySQL supports.

复杂的解决方案是使用不同的DBMS。否则,您必须在MySQL支持的范围内工作。

#1


7  

No. Only one "event" is supported. The trigger has to be either BEFORE or AFTER one of INSERT, UPDATE, DELETE.

不。只支持一个“事件”。触发器必须在插入、更新、删除之前或之后。

If you have lots of logic that is "shared", you could write a procedure, and call the procedure from the body of the trigger.

如果您有许多“共享”的逻辑,您可以编写一个过程,并从触发器的主体调用该过程。

The sophisticated solution to this is to use a different DBMS. Otherwise, you have to work within the confines of what MySQL supports.

复杂的解决方案是使用不同的DBMS。否则,您必须在MySQL支持的范围内工作。