Why there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause?
CREATE TABLE `foo` (
`ProductID` INT(10) UNSIGNED NOT NULL,
`AddedDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UpdatedDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=INNODB;
The error that results:
Error Code : 1293
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
I also wondered that long time ago. I searched a bit in my history and I think that this post: http://lists.mysql.com/internals/34919 represents the semi-official position of MySQL (before Oracle's intervention ;))
In short:
So their explanation is "because it is implemented like this". Doesn't sound very scientific. I guess it all comes from some old code. This is suggested in the thread above: "carry-over from when only the first timestamp field was auto-set/update".
Cheers!
We can give a default value for the timestamp to avoid this problem.
This post gives a detailed workaround: http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql/
Indeed an implementation fault.
The native approach in MySQL is to update a creation date yourself ( if you need one ) and have MySQL worry about the timestamp
update date ? update date : creation date
like so:On creation Insert NULL:
NULL values for timestamp are interperted as CURRENT_TIMESTAMP by default.
In MySQL the first TIMESTAMP column of a table gets both
DEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
attribute, if no attributes are given for it. this is why TIMESTAMP column with attributes must come first or you get the error described in this thread.