TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp).TIMESTAMP和DATETIME列可以自动初始化并更新为当前日期和时间(即当前时间戳)。
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:TIMESTAMP或DATETIME列,可以将当前时间戳指定为默认值、自动更新值或两者:
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).CURRENT_TIMESTAMP)。
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_TIMESTAMP和ON 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()、LOCALTIME、LOCALTIME()、LOCALTIMESTAMP和LOCALTIMESTAMP()。
Use of 使用DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP and DATETIME. DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP是专用于TIMESTAMP和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').DEFAULT子句还可用于指定常量(非自动)默认值(例如,DEFAULT 0或DEFAULT '2000-01-01 00:00:00')。
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_DATE。See 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. TIMESTAMP或DATETIME列定义可以指定默认值和自动更新值的当前时间戳,其中一个值不能指定,或者两者都不能指定。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.DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP,列的默认值为当前时间戳,并自动更新为当前时间戳。
CREATE TABLE t1 ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
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.DEFAULT子句但没有ON UPDATE CURRENT_TIMESTAMP子句,则列具有给定的默认值,并且不会自动更新为当前时间戳。
The default depends on whether the 默认值取决于DEFAULT clause specifies CURRENT_TIMESTAMP or a constant value. default子句是指定CURRENT_TIMESTAMP还是常量值。With 对于CURRENT_TIMESTAMP, the default is the current timestamp.CURRENT_TIMESTAMP,默认值为当前时间戳。
CREATE TABLE t1 ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, dt DATETIME DEFAULT CURRENT_TIMESTAMP );
With a constant, the default is the given value. 对于常量,默认值为给定值。In this case, the column has no automatic properties at all.在这种情况下,列根本没有自动属性。
CREATE TABLE t1 ( ts TIMESTAMP DEFAULT 0, dt DATETIME DEFAULT 0 );
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.ON UPDATE CURRENT_TIMESTAMP子句和常量DEFAULT子句,列将自动更新为当前时间戳,并具有给定的常量缺省值。
CREATE TABLE t1 ( ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP, dt DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP );
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.ON UPDATE CURRENT_TIMESTAMP子句但没有DEFAULT子句,列将自动更新为当前时间戳,但其默认值没有当前时间戳。
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.TIMESTAMP的默认值为0,除非使用NULL属性定义,在这种情况下,默认值为NULL。
CREATE TABLE t1 ( ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0 ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL );
DATETIME has a default of NULL unless defined with the NOT NULL attribute, in which case the default is 0.DATETIME的默认值为NULL,除非使用NOT NULL属性定义,在这种情况下,默认值为0。
CREATE TABLE t1 ( dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP, -- default NULL dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0 );
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. TIMESTAMP和DATETIME列没有自动属性,除非显式指定它们,但有以下例外:如果explicit_defaults_for_timestamp系统变量被禁用,则第一个TIMESTAMP列同时具有DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP(如果两者都没有显式指定)。To suppress automatic properties for the first 要抑制第一个TIMESTAMP column, use one of these strategies:TIMESTAMP列的自动属性,请使用以下策略之一:
Enable the 启用explicit_defaults_for_timestamp system variable. explicit_defaults_for_timestamp系统变量。In this case, the 在这种情况下,指定自动初始化和更新的DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses that specify automatic initialization and updating are available, but are not assigned to any TIMESTAMP column unless explicitly included in the column definition.DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP子句是可用的,但除非列定义中明确包含,否则不会分配给任何TIMESTAMP列。
Alternatively, if 或者,如果禁用了explicit_defaults_for_timestamp is disabled, do either of the following:explicit_defaults_for_timestamp,请执行以下操作之一:
Define the column with a 使用指定常量默认值的DEFAULT clause that specifies a constant default value.DEFAULT子句定义列。
Specify the 指定NULL attribute. NULL属性。This also causes the column to permit 这还导致列允许NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. NULL值,这意味着您不能通过将列设置为NULL来分配当前时间戳。Assigning 赋值NULL sets the column to NULL, not the current timestamp. NULL将列设置为NULL,而不是当前时间戳。To assign the current timestamp, set the column to 要分配当前时间戳,请将列设置为CURRENT_TIMESTAMP or a synonym such as NOW().CURRENT_TIMESTAMP或同义词,如NOW()。
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:这些表具有以下属性:
In each table definition, the first 在每个表定义中,第一个TIMESTAMP column has no automatic initialization or updating.TIMESTAMP列没有自动初始化或更新。
The tables differ in how the 这些表在ts1 column handles NULL values. ts1列处理空值的方式上有所不同。For 对于t1, ts1 is NOT NULL and assigning it a value of NULL sets it to the current timestamp. t1,ts1被定义为NOT NULL,为其赋值NULL将其设置为当前时间戳。For 对于t2 and t3, ts1 permits NULL and assigning it a value of NULL sets it to NULL.t2和t3,ts1允许NULL,并将其赋值为NULL将其设置为NULL。
t2 and t3 differ in the default value for ts1. t2和t3在ts1的默认值中不同。For 对于t2, ts1 is defined to permit NULL, so the default is also NULL in the absence of an explicit DEFAULT clause. t2,ts1被定义为允许NULL,因此在没有显式DEFAULT子句的情况下,缺省值也是NULL。For 对于t3, ts1 permits NULL but has an explicit default of 0.t3,ts1允许NULL,但显式默认值为0。
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. TIMESTAMP或DATETIME列定义在任何位置都包含显式的小数秒精度值,则必须在整个列定义中使用相同的值。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) );
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子句重写,否则默认值也将变为NULL。DEFAULT 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列在插入时不采用当前时间戳,除非满足以下条件之一:
Its default value is defined as 它的默认值被定义为CURRENT_TIMESTAMP and no value is specified for the columnCURRENT_TIMESTAMP,并且没有为列指定任何值
CURRENT_TIMESTAMP or any of its synonyms such as NOW() is explicitly inserted into the columnCURRENT_TIMESTAMP或它的任何同义词(如NOW())被显式插入到列中
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:t1和t2有以下定义:
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()。