3

Possible Duplicate:
MySQL CURRENT_TIMESTAMP as DEFAULT

I am trying to create table as

CREATE TABLE myTable1 
(
 id INT,
 date_validated TIMESTAMP,
 date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

however it is not working. I get error as

Incorrect table definition; there can be only one TIMESTAMP column 
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

When I switch the two timestamp statements (as shown below) then it is working.

CREATE TABLE myTable1 
(
 id INT,
 date_registered TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 
 date_validated TIMESTAMP
);

Any idea why this is happening?

This is something strange for me and never experienced such problem.

sqlfiddle demo to check queries

Community
  • 1
  • 1
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • this is repeated http://stackoverflow.com/questions/11400147/mysql-current-timestamp-as-default/11400449#11400449 – jcho360 Jul 09 '12 at 18:11
  • @jcho360 : **I know you downvoted me... NO NEED TO DOWNVOTE FOR PERSONAL REASONS** This is not repeated. That was different question. Here I am asking why this behavior is happening... – Fahim Parkar Jul 09 '12 at 18:15

1 Answers1

4

The TIMESTAMP is actually similar to a DATETIME, but the first TIMESTAMP you declare gets automatic initialization:

http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

So when you write TIMESTAMP without attributes the first time, MySQL converts it internally adding "DEFAULT CURRENT_TIMESTAMP". When MySQL encounters the second TIMESTAMP where you explicitly set CURRENT_TIMESTAMP, goes in conflict.

If you define CURRENT_TIMESTAMP in the first row, though, then it's redundant - and since you specify nothing in the second one, the second one gets assigned no default and does not go into conflict.

From the link above, "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."

LSerni
  • 55,617
  • 10
  • 65
  • 107