mysql 列中的时间戳
Timestamp in mysql column
我正在将数据库上传到 MySQL 并遇到这个问题:
ERROR 1293 (HY000) at line 31: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
问题是 "Timestamp" 数据类型的两列 table,只有一个应该有 "Current Timestamp" 值,而另一个默认值为 "null" .当我上传数据库时,两列的值都更改为 "Current Timestamp",这使得导出和导入的过程非常烦人,因为我必须在再次导入之前手动将其更改回 null。
知道为什么它会自动更改为 "current timestamp" 吗?这是一个 Amazon EC2 linux 实例(见附件 mysql 版本)
在版本 5.6 之前,MySQL 在您声明时间戳列时做出假设...具体来说,它假设 [=47= 上的 first 时间戳列] 将是具有自动更新属性的。
如果您没有明确禁用第一个时间戳的行为,它将隐式启用,这会导致拒绝稍后时间戳的显式自动值。不问是不够的。
It need not be the first TIMESTAMP
column in a table that is automatically initialized or updated to the current timestamp. However, to specify automatic initialization or updating for a different TIMESTAMP
column, you must suppress the automatic properties for the first one. Then, for the other TIMESTAMP
column, the rules for the DEFAULT
and ON UPDATE
clauses are the same as for the first TIMESTAMP
column, except that if you omit both clauses, no automatic initialization or updating occurs.
To suppress automatic properties for the first TIMESTAMP
column, do either of the following:
Define the column with a DEFAULT
clause that specifies a constant default value.
Specify the NULL
attribute. This also causes the column to permit NULL
values, which means that you cannot assign the current timestamp by setting the column to NULL
. Assigning NULL
sets the column to NULL
.
— https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
因此,对于您的第一个时间戳——如果它不是您想要的自动时间戳——使用以下任一列类型声明(它们是相同的):
TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
TIMESTAMP NOT NULL DEFAULT 0 -- automatically expanded to '0000-00-00 00:00:00'
这应该允许您在系统之间毫无问题地复制此 table 定义。
这个愚蠢的问题在 MySQL Server 5.6 中得到了修复,其中系统变量 explicit_defaults_for_timestamp
禁用 table.
中第一个时间戳的隐式自动行为
如果启动服务器 运行 5.6 时未设置此选项,则会将警告写入错误日志。
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please
use --explicit_defaults_for_timestamp server option (see documentation
for more details).
该警告提醒您您仍然有遗留行为,该行为已在 5.6 中弃用。
我正在将数据库上传到 MySQL 并遇到这个问题:
ERROR 1293 (HY000) at line 31: Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
问题是 "Timestamp" 数据类型的两列 table,只有一个应该有 "Current Timestamp" 值,而另一个默认值为 "null" .当我上传数据库时,两列的值都更改为 "Current Timestamp",这使得导出和导入的过程非常烦人,因为我必须在再次导入之前手动将其更改回 null。
知道为什么它会自动更改为 "current timestamp" 吗?这是一个 Amazon EC2 linux 实例(见附件 mysql 版本)
在版本 5.6 之前,MySQL 在您声明时间戳列时做出假设...具体来说,它假设 [=47= 上的 first 时间戳列] 将是具有自动更新属性的。
如果您没有明确禁用第一个时间戳的行为,它将隐式启用,这会导致拒绝稍后时间戳的显式自动值。不问是不够的。
It need not be the first
TIMESTAMP
column in a table that is automatically initialized or updated to the current timestamp. However, to specify automatic initialization or updating for a differentTIMESTAMP
column, you must suppress the automatic properties for the first one. Then, for the otherTIMESTAMP
column, the rules for theDEFAULT
andON UPDATE
clauses are the same as for the firstTIMESTAMP
column, except that if you omit both clauses, no automatic initialization or updating occurs.To suppress automatic properties for the first
TIMESTAMP
column, do either of the following:Define the column with a
DEFAULT
clause that specifies a constant default value.Specify the
NULL
attribute. This also causes the column to permitNULL
values, which means that you cannot assign the current timestamp by setting the column toNULL
. AssigningNULL
sets the column toNULL
.— https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
因此,对于您的第一个时间戳——如果它不是您想要的自动时间戳——使用以下任一列类型声明(它们是相同的):
TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
TIMESTAMP NOT NULL DEFAULT 0 -- automatically expanded to '0000-00-00 00:00:00'
这应该允许您在系统之间毫无问题地复制此 table 定义。
这个愚蠢的问题在 MySQL Server 5.6 中得到了修复,其中系统变量 explicit_defaults_for_timestamp
禁用 table.
如果启动服务器 运行 5.6 时未设置此选项,则会将警告写入错误日志。
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please
use --explicit_defaults_for_timestamp server option (see documentation
for more details).
该警告提醒您您仍然有遗留行为,该行为已在 5.6 中弃用。