Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 910
Hubro Avatar asked Feb 26 '11 10:02

Hubro


People also ask

What is the Current_timestamp on update Current_timestamp?

With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP , the column has the current timestamp for its default value and is automatically updated to the current timestamp.

What does a timestamp do on update Current_timestamp data type?

With the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, a column has the current timestamp for its default value and is automatically updated to the current timestamp.


1 Answers

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
like image 197
meze Avatar answered Oct 17 '22 06:10

meze