Nov 4, 2017

Automatic Initialization and Updating for TIMESTAMP and DATETIME in MySQL 5.6.5 and up

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table.

To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions.

For example:

You also need to set the column not null to have the default value, or else it may just use null value.

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 asCURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().

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'.
Post a Comment

Featured Post

Blockchain could solve accounting problems and bring an unprecedented level of accuracy, security, and speed to record-keeping.

By adopting it, companies could eliminate supply chain inefficiencies -- and save billions of dollars.