Hacking, Coding and Gaming | @[email protected]

I used to be dead against things like triggers... mainly because I hated coding them (and SQL) back when I learnt Oracle PL/SQL at college, and I've always been a "bare-minimum, must-be-backward-compatible, code-it-yourself" programmer... but now I've decided it's just much easier to use them, and it's not my fault if people refuse to upgrade to MySQL 5.

So here's my niffty trigger code for the day, which automatically updates the "last_updated" (timestamp) field in a table, when a record is inserted or updated:

_CREATE TRIGGER `database_name`.`last_updated_insert_trigger`
BEFORE INSERT ON `database_name`.`table_name` FOR EACH ROW SET new.last_updated := UNIX_TIMESTAMP();_

_CREATE TRIGGER `database_name`.`last_updated_update_trigger`
BEFORE UPDATE ON `database_name`.`table_name` FOR EACH ROW SET new.last_updated := UNIX_TIMESTAMP();_

There's probably a better way to do this (and I think you can combine both triggers, using "OR BEFORE UPDATE"?) but this is the first I've worked with triggers in MySQL and I'm too lazy to fiddle. Hope it helps. As I'm sure you can guess, database_name is your database name, last_updated_update_trigger is the name of your trigger, table_name is your table name, and last_updated is your field name that will contain the timestamp.