Automatic Initialization and Updating for TIMESTAMP and DATETIME

TIMESTAMP and DATETIME columns can be automatically initialized and updated to the current date and time (that is, the current timestamp).

For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or both:

  • An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column.

  • An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).

To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW().

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP and DATETIME. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value (for example, DEFAULT 0 or DEFAULT ‘2000-01-01 00:00:00’).

TIMESTAMP or DATETIME column definitions can specify the current timestamp for both the default and auto-update values, for one but not the other, or for neither. Different columns can have different combinations of automatic properties. The following rules describe the possibilities:

  • With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.
  1. CREATE TABLE t1 (
  2. ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  3. dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  4. );
  • With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp.

The default depends on whether the DEFAULT clause specifies CURRENT_TIMESTAMP or a constant value. With CURRENT_TIMESTAMP, the default is the current timestamp.

  1. CREATE TABLE t1 (
  2. ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  3. dt DATETIME DEFAULT CURRENT_TIMESTAMP
  4. );

With a constant, the default is the given value. In this case, the column has no automatic properties at all.

  1. CREATE TABLE t1 (
  2. ts TIMESTAMP DEFAULT 0,
  3. dt DATETIME DEFAULT 0
  4. );
  • With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the given constant default value.
  1. CREATE TABLE t1 (
  2. ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,
  3. dt DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
  4. );
  • With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, the column is automatically updated to the current timestamp but does not have the current timestamp for its default value.

The default in this case is type dependent. TIMESTAMP has a default of 0 unless defined with the NULL attribute, in which case the default is NULL.

  1. CREATE TABLE t1 (
  2. ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0
  3. ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL
  4. );

DATETIME has a default of NULL unless defined with the NOT NULL attribute, in which case the default is 0.

  1. CREATE TABLE t1 (
  2. dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP, -- default NULL
  3. dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0
  4. );