MySQL Fire Trigger for both Insert and Update

Adam S-Price picture Adam S-Price · Aug 23, 2009 · Viewed 100.3k times · Source

Is it possible to fire a mysql trigger for both the insert and update events of a table?

I know I can do the following

CREATE TRIGGER my_trigger
    AFTER INSERT ON `table`
    FOR EACH ROW
BEGIN
.....
END //

CREATE TRIGGER my_trigger
    AFTER UPDATE ON `table`
    FOR EACH ROW
BEGIN
.....
END //

But how can I do

CREATE TRIGGER my_trigger
    AFTER INSERT ON `table` AND
    AFTER UPDATE ON `table`
    FOR EACH ROW
BEGIN
.....

Is it possible, or do I have to use 2 triggers? The code is the same for both and I don't want to repeat it.

Answer

derobert picture derobert · Aug 23, 2009

You have to create two triggers, but you can move the common code into a procedure and have them both call the procedure.