Jon Crawford Jon Crawford - 11 days ago 9
MySQL Question

What is wrong with my mySQL Trigger?

I am trying to make a simple trigger that logs update events. However I get Error near " BEGIN ...". if I use single quotes, i.e. 'update_log' and 'customer', then the error is near ' 'update_log....'

use tal;
delimiter //
CREATE TRIGGER onUpdate
BEFORE UPDATE ON customer
BEGIN
INSERT INTO update_log VALUES(user(), 'An Udpdate operation against the customer table.', now());
END//
delimiter ;


Why does this not work?

Answer

Unlike Oracle mysql needs FOR EACH ROW

delimiter //
CREATE TRIGGER onUpdate
BEFORE UPDATE ON customer
FOR EACH ROW
BEGIN
INSERT INTO update_log VALUES(user(), 'An Udpdate operation against the customer table.', now());
END//
delimiter ;