MySQL triggers for inserted and updated unix timestamps

Here’s the sql I use when creating a database for an app where it’s assumed each table will have columns that store unix time stamps for their created date and time, and updated date and time. ┬áIt assumes that there is a column named created and one named updated on the table.

create trigger {table}_set_timestamp_on_insert before insert on {table} for each row set new.created = unix_timestamp(now()), new.updated = unix_timestamp(now());
create trigger {table}_set_timestamp_on_update before update on {table} for each row set new.updated = unix_timestamp(now());

Triggers are awesome like that.

Leave a Reply

Your email address will not be published. Required fields are marked *