Skip to content

MySQL DATETIME Auto Update

Paul Gerarts edited this page Jun 12, 2017 · 4 revisions

As of MySQL 5.6.5, you can use the DATETIME type with a dynamic default value:

CREATE TABLE foo (
    creation_time      DATETIME DEFAULT   CURRENT_TIMESTAMP,
    modification_time  DATETIME ON UPDATE CURRENT_TIMESTAMP
)

Or even combine both rules:

modification_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Reference: http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html http://optimize-this.blogspot.com/2012/04/datetime-default-now-finally-available.html

From: https://stackoverflow.com/questions/5818423/set-now-as-default-value-for-datetime-datatype

ALTER TABLE `rb_pc_commit`
	CHANGE `col_created` `col_created` DATETIME  NOT NULL  DEFAULT CURRENT_TIMESTAMP,
	CHANGE `col_modified` `col_modified` DATETIME  NOT NULL  DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Clone this wiki locally