开发者

Why won't MySQL let me remove attribute "on update CURRENT_TIMESTAMP"?

开发者 https://www.devze.com 2023-02-13 19:21 出处:网络
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 up

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


You should specify DEFAULT CURRENT_TIMESTAMP (or DEFAULT 0)

ALTER TABLE pages CHANGE date_created date_created TIMESTAMP NOT NULL DEFAULT 0,
CHANGE  `date_updated`  `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL


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

0

精彩评论

暂无评论...
验证码 换一张
取 消