Why won't MySQL let me remove attribute “on up

2019-04-19 23:08发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-04-20 00:06

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
查看更多
三岁会撩人
3楼-- · 2019-04-20 00:11

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

查看更多
登录 后发表回答