一个MySQL触发器中的多个事件

时间:2014-06-27 08:16:27

标签: mysql triggers

我想使用MYSQL触发器监视一个表上的多个事件。当这些事件发生时,我可以在这个数据库上做点什么。以下代码可以完成此任务:

DROP TRIGGER if exists trigger_sample1;;  
CREATE TRIGGER trigger_sample1  
after UPDATE on table_1 for each row  
begin  
    /* do something */  
end ;; 

DROP TRIGGER if exists trigger_sample2;;  
CREATE TRIGGER trigger_sample2  
after INSERT on table_1 for each row  
begin  
    /* do something */  
end ;; 

DROP TRIGGER if exists trigger_sample3;;  
CREATE TRIGGER trigger_sample3  
after DELETE on table_1 for each row  
begin  
    /* do something */  
end ;; 

但我希望将这些触发器组合成一个触发器。我尝试了一些代码:

DROP TRIGGER if exists trigger_sample;;  
CREATE TRIGGER trigger_sample  
after UPDATE, INSERT, DELETE on table_1 for each row  
begin  
    /* do something */  
end ;;  

这不行。我也尝试过:

DROP TRIGGER if exists trigger_sample;;  
CREATE TRIGGER trigger_sample  
after UPDATE on table_1  
after INSERT on table_1  
after DELETE on talble_1  
for each row  
begin  
    /* do something */  
end ;;  

这两点都不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:-1)

你可以这样做。

CREATE OR REPLACE TRIGGER trigger_sample  
AFTER UPDATE OR INSERT OR DELETE ON table_name 
FOR EACH ROW  
BEGIN  
    /* do something */  
END;