TIMESTAMP 和 DATETIME 列可以自动初始化并更新为当前的日期和时间(即当前时间戳)。TIMESTAMP and
DATETIME columns can be
automatically initialized 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:
自动初始化列会将插入行中未为该列指定值的记录设置为当前时间戳。An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column.
自动更新列在行中任何其他列的值从当前值发生更改时会自动更新为当前时间戳。如果所有其他列都设置为当前值,则自动更新列保持不变。要防止自动更新列在其他列更改时更新,请显式将其设置为当前值。即使其他列未更改,要更新自动更新列,也请显式将其设置为应有的值(例如,将其设置为
CURRENT_TIMESTAMP)。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 toCURRENT_TIMESTAMP).
此外,如果explicit_defaults_for_timestamp系统变量被禁用,您可以通过赋值为NULL来初始化或更新任何TIMESTAMP(但不能是DATETIME)列为当前日期和时间,除非它已被定义为允许NULL值的NULL属性。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.
要指定自动属性,请在列定义中使用 DEFAULT
CURRENT_TIMESTAMP 和 ON UPDATE
CURRENT_TIMESTAMP 子句。子句的顺序无关紧要。如果在列定义中同时存在这两个子句,则任意一个可以先出现。CURRENT_TIMESTAMP 的任何同义词都与 CURRENT_TIMESTAMP 含义相同。这些同义词包括 CURRENT_TIMESTAMP()、NOW()、LOCALTIME、LOCALTIME()、LOCALTIMESTAMP 和 LOCALTIMESTAMP()。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(),
LOCALTIME,
LOCALTIME(),
LOCALTIMESTAMP, and
LOCALTIMESTAMP().
使用 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 是特定于 TIMESTAMP 和 DATETIME 的。DEFAULT 子句也可用于指定常量(非自动)默认值(例如,DEFAULT 0 或 DEFAULT '2000-01-01
00:00:00')。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').
以下示例使用 DEFAULT 0,这是一个默认值,根据是否启用了严格 SQL 模式或 NO_ZERO_DATE SQL 模式,可能会产生警告或错误。请注意,TRADITIONAL SQL 模式包括严格模式和 NO_ZERO_DATE。参见 第 7.1.11 节,“服务器 SQL 模式”。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. Be aware that the
TRADITIONAL SQL mode
includes strict mode and
NO_ZERO_DATE. See
Section 7.1.11, “Server SQL Modes”.
TIMESTAMP 或 DATETIME 列定义可以指定当前时间戳作为默认值和自动更新值的一种、另一种或两者都不是。不同的列可以具有不同的自动属性组合。以下规则描述了这些可能性: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:
当同时使用
DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP时,该列的默认值为当前时间戳,并会自动更新为当前时间戳。With bothDEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.CREATE TABLE t1 ( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );
如果有
DEFAULT子句但没有ON UPDATE CURRENT_TIMESTAMP子句,该列将使用给定的默认值,并不会自动更新为当前时间戳。With aDEFAULTclause but noON UPDATE CURRENT_TIMESTAMPclause, the column has the given default value and is not automatically updated to the current timestamp.默认值取决于
DEFAULT子句是指定了CURRENT_TIMESTAMP还是一个常量值。如果是CURRENT_TIMESTAMP,则默认值为当前时间戳。The default depends on whether theDEFAULTclause specifiesCURRENT_TIMESTAMPor a constant value. WithCURRENT_TIMESTAMP, the default is the 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 );
如果列有
ON UPDATE CURRENT_TIMESTAMP子句并且有一个固定的DEFAULT子句,则该列会自动更新为当前时间戳,并具有给定的固定默认值。With anON UPDATE CURRENT_TIMESTAMPclause and a constantDEFAULTclause, the column is automatically updated to the current timestamp and has the given constant default value.CREATE TABLE t1 ( ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP, dt DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP );
如果列有
ON UPDATE CURRENT_TIMESTAMP子句但没有DEFAULT子句,则该列会自动更新为当前时间戳,但其默认值不是当前时间戳。With anON UPDATE CURRENT_TIMESTAMPclause but noDEFAULTclause, the column is automatically updated to the current timestamp but does not have the current timestamp for its default value.在这种情况下,默认值依赖于类型。
TIMESTAMP的默认值为 0,除非使用NULL属性定义,在这种情况下默认值为NULL。The default in this case is type dependent.TIMESTAMPhas a default of 0 unless defined with theNULLattribute, in which case the default isNULL.CREATE TABLE t1 ( ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0 ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL );
DATETIME的默认值是NULL,除非定义了NOT NULL属性,在这种情况下默认值为 0。DATETIMEhas a default ofNULLunless defined with theNOT NULLattribute, in which case the default is 0.CREATE TABLE t1 ( dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP, -- default NULL dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0 );
TIMESTAMP 和 DATETIME 列没有自动属性,除非它们被显式指定,唯一的例外是:如果 explicit_defaults_for_timestamp 系统变量被禁用,第一个 TIMESTAMP 列在未显式指定的情况下会具有 DEFAULT CURRENT_TIMESTAMP 和 ON
UPDATE CURRENT_TIMESTAMP。要抑制第一个 TIMESTAMP 列的自动属性,可使用以下策略之一: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. To suppress automatic properties for the first
TIMESTAMP column, use one of
these strategies:
启用
explicit_defaults_for_timestamp系统变量。在这种情况下,指定自动初始化和更新的DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP子句可用,但不会分配给任何TIMESTAMP列,除非在列定义中明确包含。Enable theexplicit_defaults_for_timestampsystem variable. In this case, theDEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPclauses that specify automatic initialization and updating are available, but are not assigned to anyTIMESTAMPcolumn unless explicitly included in the column definition.或者,如果禁用
explicit_defaults_for_timestamp,请执行以下任一操作:Alternatively, ifexplicit_defaults_for_timestampis disabled, do either of the following:使用指定常量默认值的
DEFAULT子句定义该列。Define the column with aDEFAULTclause that specifies a constant default value.指定
NULL属性。这也会导致该列允许NULL值,这意味着不能通过将列设置为NULL来分配当前时间戳。将NULL分配给列会将列设置为NULL,而不是当前时间戳。要分配当前时间戳,请将列设置为CURRENT_TIMESTAMP或类似的同义词,如NOW()。Specify theNULLattribute. This also causes the column to permitNULLvalues, which means that you cannot assign the current timestamp by setting the column toNULL. AssigningNULLsets the column toNULL, not the current timestamp. To assign the current timestamp, set the column toCURRENT_TIMESTAMPor a synonym such asNOW().
考虑以下表定义: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:
在每个表定义中,第一个
TIMESTAMP列没有自动初始化或更新。In each table definition, the firstTIMESTAMPcolumn has no automatic initialization or updating.这些表在
ts1列处理NULL值的方式上有所不同。对于t1,ts1是NOT NULL,并且将其赋值为NULL时会设置为当前时间戳。对于t2和t3,ts1允许NULL,将其赋值为NULL时会设置为NULL。The tables differ in how thets1column handlesNULLvalues. Fort1,ts1isNOT NULLand assigning it a value ofNULLsets it to the current timestamp. Fort2andt3,ts1permitsNULLand assigning it a value ofNULLsets it toNULL.t2和t3在ts1的默认值上有所不同。对于t2,ts1被定义为允许NULL,所以在没有显式DEFAULT子句的情况下,默认值也是NULL。对于t3,ts1允许NULL,但有一个显式的默认值 0。t2andt3differ in the default value forts1. Fort2,ts1is defined to permitNULL, so the default is alsoNULLin the absence of an explicitDEFAULTclause. Fort3,ts1permitsNULLbut has an explicit default of 0.
如果一个 TIMESTAMP 或 DATETIME 列定义在任何地方包含明确的小数秒精度值,则整个列定义中必须使用相同的值。这是允许的: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. 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 初始化与 NULL 属性TIMESTAMP Initialization and the NULL Attribute
如果 explicit_defaults_for_timestamp 系统变量被禁用,TIMESTAMP 列默认是 NOT NULL,不能包含 NULL 值,将 NULL 赋值会赋予当前时间戳。要允许 TIMESTAMP 列包含 NULL,需要显式声明该列具有 NULL 属性。在这种情况下,默认值也会变为 NULL,除非通过 DEFAULT 子句指定其他默认值来覆盖。DEFAULT NULL 可以用来显式指定 NULL 作为默认值。(对于未声明 NULL 属性的 TIMESTAMP 列,DEFAULT NULL 是无效的。)如果 TIMESTAMP 列允许 NULL 值,将 NULL 赋值会设置为 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. To permit
a TIMESTAMP column to contain
NULL, explicitly declare it with the
NULL attribute. In this case, the default
value also becomes NULL unless overridden
with a DEFAULT clause that specifies a
different default value. DEFAULT NULL can be
used to explicitly specify NULL as the
default value. (For a TIMESTAMP
column not declared with the NULL attribute,
DEFAULT NULL is invalid.) If a
TIMESTAMP column permits
NULL values, assigning
NULL sets it to NULL, not
to the current timestamp.
下表包含几个允许 NULL 值的 TIMESTAMP 列:The following table contains several
TIMESTAMP columns that permit
NULL values:
CREATE TABLE t ( ts1 TIMESTAMP NULL DEFAULT NULL, ts2 TIMESTAMP NULL DEFAULT 0, ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP );
允许 NULL 值的 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:
其默认值定义为
CURRENT_TIMESTAMP,并且该列未指定任何值Its default value is defined asCURRENT_TIMESTAMPand no value is specified for the columnCURRENT_TIMESTAMP或其任何同义词,例如NOW()被显式插入到列中CURRENT_TIMESTAMPor any of its synonyms such asNOW()is explicitly inserted into the column
换句话说,定义为允许 NULL 值的 TIMESTAMP 列仅在其定义中包含 DEFAULT CURRENT_TIMESTAMP 时才会自动初始化:In other words, a TIMESTAMP
column defined to permit NULL values
auto-initializes only if its definition includes
DEFAULT CURRENT_TIMESTAMP:
CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
如果 TIMESTAMP 列允许 NULL 值,但其定义中未包含 DEFAULT CURRENT_TIMESTAMP,则必须显式插入与当前日期和时间对应的值。假设表 t1 和 t2 拥有以下定义: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. Suppose that tables t1 and
t2 have these definitions:
CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00'); CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT NULL);
要在插入时将任一表中的 TIMESTAMP 列设置为当前时间戳,需显式地将其赋值为该值。例如:To set the TIMESTAMP column in
either table to the current timestamp at insert time, explicitly
assign it that value. For example:
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP); INSERT INTO t1 VALUES (NOW());
如果启用了 explicit_defaults_for_timestamp 系统变量,TIMESTAMP 列仅在声明为 NULL 属性时才允许 NULL 值。此外,TIMESTAMP 列不允许通过赋值 NULL 来设置当前时间戳,无论其声明为 NULL 还是 NOT NULL 属性。要设置当前时间戳,请将列设置为 CURRENT_TIMESTAMP 或其同义词,例如 NOW()。If the
explicit_defaults_for_timestamp
system variable is enabled,
TIMESTAMP columns permit
NULL values only if declared with the
NULL attribute. Also,
TIMESTAMP columns do not permit
assigning NULL to assign the current
timestamp, whether declared with the NULL or
NOT NULL attribute. To assign the current
timestamp, set the column to
CURRENT_TIMESTAMP or a synonym
such as NOW().