Managing Timestamps in MySQL with a Trigger

MySQL doesn’t support having two columns with time stamping on both initialization and/or on updating at the same time. It would be nice to be able to do *this* where the created_at column gets the current_timestamp on initialization and the updated_at gets changed on updating the row.

# like so doesn't work...
create table entries(
  body blob,
  created_at datetime default current_timestamp,
  updated_at timestamp default current_timestamp on update current_timestamp
);

Seems like a feature a lot of folks would like. There are two work-arounds. The first is baking it into your application code with something like

create table entries(
  body blob,
  created_at datetime default null,
  updated_at timestamp default current_timestamp on update current_timestamp
);
insert into entries (body, created_at) values ('hello world', now());

The second way is to create a trigger and call the trigger on your insert action on a row.

create table entries (
  body  blob,
  created_at datetime default null,
  updated_at timestamp default null on update current_timestamp
);
create trigger init_created_at before insert on entries for each row set new.created_at = now();

Now whenever a new row is created the trigger will be executed and set the time to the current timestamp. You can forget about the created_at column in your code because it’s not meant to be changed.

  • http://www.paperairplaneshq.com/jaguar.html Russell Ater

    This is exactly what I’ve been looking for all day. I should have found your post sooner.

  • http://twitter.com/finestfoods123 Fine Food

    thanks for that

  • Pingback: mysql update timestamp on update multiple | Whoila Blog

  • http://simonsimcity.de.vu Simon Schick

    Hi, all

    It is possible to create a created_at column without using a trigger!

    create table test (str varchar(32), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

    Here are two links if you want to know more about it:
    http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
    http://stackoverflow.com/questions/168736/how-do-you-set-a-default-value-for-a-mysql-datetime-column

    Bye
    Simon

  • bseanvt

    but you can only have ONE column per table when using CURRENT_TIMESTAMP. If you want more than one column (created_at and updated_at columns, for instance) you have to resort to using a trigger.

    several commenters from the stackoverflow thread you posted, use similar approaches to what is listed above in my post.

    this has been a mysql feature request for a while. are you suggesting that it has finally been added?

  • http://simonsimcity.de.vu Simon Schick

    You’re right …

    I tried to use this definition and I got an error:
    CREATE TABLE `fbs`.`testtabelle` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
    `updated_at` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL ,
    `name` VARCHAR( 200 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM ;

    The error-message was: #1293 – Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    Tested using MySQL-Server 5.1.57

    Bye
    Simon