为什么 MySQL 不允许我删除“更新 CURRENT_TIMESTAMP 时”属性?
我有一个包含两个时间戳字段的表。我只是用名称和类型TIMESTAMP
定义了它们,但出于某种原因,MySQL 会自动使用默认值和属性更新 CURRENT_TIMESTAMP
来设置其中之一。我计划在这两个字段中都没有默认值,但其中一个字段称为“date_updated”,所以我想我可以将提到的属性设置为该字段。
不幸的是,“date_created”字段是通过 on update CURRENT_TIMESTAMP
属性设置的,无论我做什么,MySQL 都不会让我删除它。
我尝试编辑“date_created”字段并删除该属性。单击“保存”后,该属性又回来了。我还尝试选择这两个字段,从其中一个字段中删除属性并将其设置在另一个字段上。它给了我错误#1293 - 不正确的表定义; DEFAULT 或 ON UPDATE 子句中只能有一个带有 CURRENT_TIMESTAMP 的 TIMESTAMP 列,突然,值上的两个属性列都设置为
on update CURRENT_TIMESTAMP
结果:
Error
SQL query:
ALTER TABLE `pages` CHANGE `date_created` `date_created` TIMESTAMP NOT NULL ,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
MySQL said:
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
我必须真正重新创建这两个列吗以正确的顺序解决这个问题?
我想知道如何正确解决这个问题,以供将来参考。
谢谢
现在我也尝试运行
ALTER TABLE pages
CHANGE date_created
date_created TIMESTAMP NOT NULL
I have a table with two timestamp fields. I simply defined them with a name and the type TIMESTAMP
, yet for some reason MySQL automatically set one of them with a default value and the attribute on update CURRENT_TIMESTAMP
. I was planning on having NO default value in either of the fields, but one of the fields is called "date_updated" so I suppose I could set the mentioned attribute to that field.
Unfortunately, it's the field "date_created" that was set with the on update CURRENT_TIMESTAMP
attribute, and no matter what I do, MySQL won't let me remove it.
I've tried editing the "date_created" field and removing the attribute. When clicking save, the attribute is back. I have also tried selecting both fields, removing the attribute from one of them and setting it on the other. It gives me the error #1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
and suddenly both attribute columns on the values are set to on update CURRENT_TIMESTAMP
the result:
Error
SQL query:
ALTER TABLE `pages` CHANGE `date_created` `date_created` TIMESTAMP NOT NULL ,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
MySQL said:
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Must I really recreate both those columns in the correct order to fix this?
I would like to know how I could solve this problem correctly, for future reference.
Thanks
Now I've also tried to run
ALTER TABLE pages
CHANGE date_created
date_created TIMESTAMP NOT NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该指定
DEFAULT CURRENT_TIMESTAMP
(或DEFAULT 0
)You should specify
DEFAULT CURRENT_TIMESTAMP
(orDEFAULT 0
)从 MySQL 版本 5.6.6 开始,您可以在配置文件中使用 explicit_defaults_for_timestamp 选项,因此时间戳列将不会具有“DEFAULT CURRENT_TIMESTAMP”或“ON UPDATE CURRENT_TIMESTAMP”属性默认情况下。如果这些列未声明为 NOT NULL,也可以将它们设置为 NULL。
请参阅: http://dev.mysql.com /doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
As of MySQL version 5.6.6, you can use the explicit_defaults_for_timestamp option in the configuration file, therefore timestamp columns will not have 'DEFAULT CURRENT_TIMESTAMP' or 'ON UPDATE CURRENT_TIMESTAMP' attributes by default. It will also be possible so set these columns to NULL if they are not declared as NOT NULL.
See: http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp