1---2title: 'Triggers in MySQL'3date: '2007-05-08'4published_at: '2007-05-08T12:03:00.000+10:00'5tags: ['databases', 'mysql', 'programming']6author: 'Gavin Jackson'7excerpt: 'Starting from Mysql 5.0.2, mysql now supports triggers - which can be quite handy. I''m using them to automatically create new (derived) table records when a user submits data to my web app. Syntax is...'8updated_at: '2007-05-08T13:50:33.700+10:00'9legacy_url: 'http://www.gavinj.net/2007/05/triggers-in-mysql.html'10---1112Starting from Mysql 5.0.2, mysql now supports triggers - which can be quite handy. I'm using them to automatically create new (derived) table records when a user submits data to my web app. Syntax is very clean (as follows) [http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html](http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html):1314```15CREATE TABLE test1(a1 INT);16CREATE TABLE test2(a2 INT);17CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);18CREATE TABLE test4(19a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,20b4 INT DEFAULT 021);2223DELIMITER |2425CREATE TRIGGER testref BEFORE INSERT ON test126FOR EACH ROW BEGIN27INSERT INTO test2 SET a2 = NEW.a1;28DELETE FROM test3 WHERE a3 = NEW.a1;29UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;30END;31|3233DELIMITER ;34```3536[http://www.advogato.org/article/858.html](http://www.advogato.org/article/858.html)3738On an unrelated note, I just read the other day that Oracle has acquired the company that wrote the InnoDB Mysql engine (which is the real one that enforces foreign keys and transactions etc).3940[ http://developers.slashdot.org/article.pl?sid=07/04/01/1448207&from;=rss](http://developers.slashdot.org/article.pl?sid=07/04/01/1448207&from=rss) On another unrelated note, you can now use Postgresql as a MySQL database engine - how cool is that?414243