11.2.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME时间戳和日期时间的自动初始化和更新

TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp).TIMESTAMPDATETIME列可以自动初始化并更新为当前日期和时间(即当前时间戳)。

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:对于表中的任何TIMESTAMPDATETIME列,可以将当前时间戳指定为默认值、自动更新值或两者:

In addition, if the explicit_defaults_for_timestamp system variable is disabled, you can initialize or update any TIMESTAMP (but not DATETIME) column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.此外,如果禁用了explicit_defaults_for_timestamp系统变量,则可以通过为任何TIMESTAMP(而不是DATETIME)列指定一个空值来初始化或更新该列到当前日期和时间,除非已使用NULL属性定义该列以允许空值。

To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions. 要指定自动属性,请在列定义中使用DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP子句。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. CURRENT_TIMESTAMP的任何同义词都与CURRENT_TIMESTAMP具有相同的含义。These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().它们是CURRENT_TIMESTAMP()NOW()LOCALTIMELOCALTIME()LOCALTIMESTAMPLOCALTIMESTAMP()

Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP and DATETIME. 使用DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP是专用于TIMESTAMPDATETIME的。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').DEFAULT子句还可用于指定常量(非自动)默认值(例如,DEFAULT 0DEFAULT '2000-01-01 00:00:00')。

Note注意

The following examples use DEFAULT 0, a default that can produce warnings or errors depending on whether strict SQL mode or the NO_ZERO_DATE SQL mode is enabled. 以下示例使用DEFAULT 0,该默认值可能会产生警告或错误,具体取决于是否启用了严格SQL模式或NO_ZERO_DATESQL模式。Be aware that the TRADITIONAL SQL mode includes strict mode and NO_ZERO_DATE. 请注意,TRADITIONALSQL模式包括严格模式和NO_ZERO_DATESee Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节,“服务器SQL模式”

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. TIMESTAMPDATETIME列定义可以指定默认值和自动更新值的当前时间戳,其中一个值不能指定,或者两者都不能指定。Different columns can have different combinations of automatic properties. 不同的列可以有不同的自动属性组合。The following rules describe the possibilities:以下规则描述了各种可能性:

TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly. TIMESTAMPDATETIME列没有自动属性,除非显式指定它们,但有以下例外:如果explicit_defaults_for_timestamp系统变量被禁用,则第一个TIMESTAMP列同时具有DEFAULT CURRENT_TIMESTAMPON UPDATE CURRENT_TIMESTAMP(如果两者都没有显式指定)。To suppress automatic properties for the first TIMESTAMP column, use one of these strategies:要抑制第一个TIMESTAMP列的自动属性,请使用以下策略之一:

Consider these table definitions:考虑以下表定义:

CREATE TABLE t1 (
  ts1 TIMESTAMP DEFAULT 0,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t2 (
  ts1 TIMESTAMP NULL,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t3 (
  ts1 TIMESTAMP NULL DEFAULT 0,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);

The tables have these properties:这些表具有以下属性:

If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition. 如果TIMESTAMPDATETIME列定义在任何位置都包含显式的小数秒精度值,则必须在整个列定义中使用相同的值。This is permitted:这是允许的:

CREATE TABLE t1 (
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
);

This is not permitted:这是不允许的:

CREATE TABLE t1 (
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(3)
);

TIMESTAMP Initialization and the NULL Attribute时间戳初始化和NULL属性

If the explicit_defaults_for_timestamp system variable is disabled, TIMESTAMP columns by default are NOT NULL, cannot contain NULL values, and assigning NULL assigns the current timestamp. 如果禁用了explicit_defaults_for_timestamp系统变量,则默认情况下,TIMESTAMP列不为NULL,不能包含NULL值,并且赋值NULL将分配当前时间戳。To permit a TIMESTAMP column to contain NULL, explicitly declare it with the NULL attribute. 要让TIMESTAMP列允许包含NULL,请使用NULL属性显式声明它。In this case, the default value also becomes NULL unless overridden with a DEFAULT clause that specifies a different default value. 在这种情况下,除非用指定不同默认值的DEFAULT子句重写,否则默认值也将变为NULLDEFAULT NULL can be used to explicitly specify NULL as the default value. DEFAULT NULL可用于显式指定NULL作为默认值。(For a TIMESTAMP column not declared with the NULL attribute, DEFAULT NULL is invalid.) (对于未使用NULL属性声明的TIMESTAMP列,默认NULL无效。)If a TIMESTAMP column permits NULL values, assigning NULL sets it to NULL, not to the current timestamp.如果TIMESTAMP列允许NULL值,则赋值NULL会将其设置为NULL,而不是当前时间戳。

The following table contains several TIMESTAMP columns that permit NULL values:下表包含几个允许NULL值的TIMESTAMP列:

CREATE TABLE t
(
  ts1 TIMESTAMP NULL DEFAULT NULL,
  ts2 TIMESTAMP NULL DEFAULT 0,
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

A TIMESTAMP column that permits NULL values does not take on the current timestamp at insert time except under one of the following conditions:允许NULL值的TIMESTAMP列在插入时不采用当前时间戳,除非满足以下条件之一:

In other words, a TIMESTAMP column defined to permit NULL values auto-initializes only if its definition includes DEFAULT CURRENT_TIMESTAMP:换句话说,定义为允许NULL值的TIMESTAMP列只有在其定义包含默认的当前时间戳时才会自动初始化:

CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);

If the TIMESTAMP column permits NULL values but its definition does not include DEFAULT CURRENT_TIMESTAMP, you must explicitly insert a value corresponding to the current date and time. 如果TIMESTAMP列允许NULL值,但其定义不包括DEFAULT CURRENT_TIMESTAMP,则必须显式插入与当前日期和时间对应的值。Suppose that tables t1 and t2 have these definitions:假设表t1t2有以下定义:

CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT NULL);

To set the TIMESTAMP column in either table to the current timestamp at insert time, explicitly assign it that value. 要将任一表中的TIMESTAMP列设置为插入时的当前时间戳,请显式地将该值赋给它。For example:例如:

INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
INSERT INTO t1 VALUES (NOW());

If the explicit_defaults_for_timestamp system variable is enabled, TIMESTAMP columns permit NULL values only if declared with the NULL attribute. 如果启用了explicit_defaults_for_timestamp系统变量,则仅当使用NULL属性声明时,TIMESTAMP列才允许NULL值。Also, TIMESTAMP columns do not permit assigning NULL to assign the current timestamp, whether declared with the NULL or NOT NULL attribute. 而且,TIMESTAMP列不允许赋值NULL来赋值当前时间戳,无论是用NULL还是NOT NULL属性声明的。To assign the current timestamp, set the column to CURRENT_TIMESTAMP or a synonym such as NOW().要分配当前时间戳,请将列设置为CURRENT_TIMESTAMP或其同义词,如NOW()