MySQL Variables explicit_defaults_for_timestamp 数据库 参数变量解释及正确配置使用
本站中文解释
explicit_defaults_for_timestamp是MySQL中的一个参数变量,当该变量设置为ON时,服务器将在创建表时指定TIMESTAMP字段的默认值;当该变量设置为OFF时,服务器将不再为 TIMESTAMP 字段指定任何默认值,转而根据 SQL 标准做出相应的行为:在插入批量数据前,没有提供该字段的值,则设其字段值为零值(0),而在插入批量数据中某条数据的某字段值没有提供,则设其字段值为该条数据前面一条数据的值。
要设置该变量,需在MySQL服务启动前,可以通过命令行里的–explict_defaults_for_timestamp参数,或者在MySQL的配置文件中添加:explict_defaults_for_timestamp=0/1 来设置,0是OFF,1是ON。
官方英文解释
explicit_defaults_for_timestamp
| Command-Line Format | --explicit-defaults-for-timestamp[={OFF|ON}] |
|---|---|
| Deprecated | Yes |
| System Variable | explicit_defaults_for_timestamp |
| Scope | Global, Session |
| Dynamic | Yes |
| Type | Boolean |
| Default Value | OFF |
This system variable determines whether the server enables
certain nonstandard behaviors for default values and
NULL-value handling in
TIMESTAMP columns. By default,
explicit_defaults_for_timestamp
is disabled, which enables the nonstandard behaviors.
If
explicit_defaults_for_timestamp
is disabled, the server enables the nonstandard behaviors and
handles TIMESTAMP columns as
follows:
-
TIMESTAMPcolumns not
explicitly declared with theNULL
attribute are automatically declared with theNOTattribute. Assigning such a column a value
NULL
ofNULLis permitted and sets the
column to the current timestamp. -
The first
TIMESTAMPcolumn
in a table, if not explicitly declared with the
NULLattribute or an explicit
DEFAULTorON UPDATE
attribute, is automatically declared with the
DEFAULT CURRENT_TIMESTAMPand
ON UPDATE CURRENT_TIMESTAMPattributes. -
TIMESTAMPcolumns following
the first one, if not explicitly declared with the
NULLattribute or an explicit
DEFAULTattribute, are automatically
declared asDEFAULT '0000-00-00(the “zero” timestamp).
00:00:00'
For inserted rows that specify no explicit value for such
a column, the column is assigned'0000-00-00and no warning occurs.
00:00:00'Depending on whether strict SQL mode or the
NO_ZERO_DATESQL mode is
enabled, a default value of'0000-00-00may be invalid. Be aware that the
00:00:00'
TRADITIONALSQL mode
includes strict mode and
NO_ZERO_DATE. See
Section 5.1.10, “Server SQL Modes”.
The nonstandard behaviors just described are deprecated;
expect them to be removed in a future release of MySQL.
If
explicit_defaults_for_timestamp
is enabled, the server disables the nonstandard behaviors and
handles TIMESTAMP columns as
follows:
-
It is not possible to assign a
TIMESTAMPcolumn a value of
NULLto set it to the current
timestamp. To assign the current timestamp, set the column
toCURRENT_TIMESTAMPor a
synonym such asNOW(). -
TIMESTAMPcolumns not
explicitly declared with theNOT NULL
attribute are automatically declared with the
NULLattribute and permit
NULLvalues. Assigning such a column a
value ofNULLsets it to
NULL, not the current timestamp. -
TIMESTAMPcolumns declared
with theNOT NULLattribute do not
permitNULLvalues. For inserts that
specifyNULLfor such a column, the
result is either an error for a single-row insert if
strict SQL mode is enabled, or'0000-00-00is inserted for multiple-row inserts
00:00:00'
with strict SQL mode disabled. In no case does assigning
the column a value ofNULLset it to
the current timestamp. -
TIMESTAMPcolumns
explicitly declared with theNOT NULL
attribute and without an explicit
DEFAULTattribute are treated as having
no default value. For inserted rows that specify no
explicit value for such a column, the result depends on
the SQL mode. If strict SQL mode is enabled, an error
occurs. If strict SQL mode is not enabled, the column is
declared with the implicit default of'0000-00-00and a warning occurs. This is similar
00:00:00'
to how MySQL treats other temporal types such as
DATETIME. -
No
TIMESTAMPcolumn is
automatically declared with theDEFAULTor
CURRENT_TIMESTAMPON UPDATEattributes. Those attributes
CURRENT_TIMESTAMP
must be explicitly specified. -
The first
TIMESTAMPcolumn
in a table is not handled differently from
TIMESTAMPcolumns following
the first one.
If
explicit_defaults_for_timestamp
is disabled at server startup, this warning appears in the
error log:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
As indicated by the warning, to disable the deprecated
nonstandard behaviors, enable the
explicit_defaults_for_timestamp
system variable at server startup.
explicit_defaults_for_timestamp
is itself deprecated because its only purpose is to permit
control over deprecated
TIMESTAMP behaviors that are
to be removed in a future release of MySQL. When removal of
those behaviors occurs,
explicit_defaults_for_timestamp
no longer has any purpose, and you can expect it to be
removed as well.
For additional information, see
Section 11.2.6, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.