tags:

views:

2462

answers:

3

I have the following table schema;

CREATE TABLE `db1`.`sms_queue` (
  `Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `Message` VARCHAR(160) NOT NULL DEFAULT 'Unknown Message Error',
  `CurrentState` VARCHAR(10) NOT NULL DEFAULT 'None',
  `Phone` VARCHAR(14) DEFAULT NULL,
  `Created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `LastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  `TriesLeft` tinyint NOT NULL DEFAULT 3,
  PRIMARY KEY (`Id`)
)
ENGINE = InnoDB;

It fails with the following error:

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.

My question is, can I have both of those fields? or do I have to manually set a LastUpdated field during each transaction?

+3  A: 

You can have them both, just take off the "CURRENT_TIMESTAMP" flag on the created field. Whenever you create a new record in the table, just use "NOW()" for a value.

Or.

On the contrary, remove the 'ON UPDATE CURRENT_TIMESTAMP' flag and send the NOW() for that field. That way actually makes more sense.

Stephen
Is this the only way? I can't have the database look after all that detail?
Xenph Yan
According to the MySql manual, CURRENT_TIMESTAMP is a synonym for NOW() so I don't think this will work.
tvanfosson
I'm sure you can, it's just that CURRENT_TIMESTAMP is a flag reserved for only one field. Either way, if you had that flag in there, regardless what value you have for that field when you add a record, it will always be the current timestamp, hence the name.
Stephen
URL: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
tvanfosson
@tvanfosson, I think he meant as part of the insert statement.
Xenph Yan
@tvanfosson: Using 'NOW()' in a query passes the current time to the database. What that value actually is depends on the language, engine, and probably a hundred other things I don't know. The flag I was referring to sets it so that when a record is created, the time is added to that field.
Stephen
I see. I thought he meant in the definition. Either way, I'd be happy to be wrong.
tvanfosson
I went with the insert NOW() way, mostly as other code might touch these tables and I don't trust people to update them correctly. :)
Xenph Yan
+5  A: 

From the documentation:

For one TIMESTAMP column in a table, you can assign the current timestamp as the default value and the auto-update value. It is possible to have the current timestamp be the default value for initializing the column, for the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

Robert Gamble
+1 Thanks for the official weigh-in from the documentation.
Stephen
A: 

There is a trick how to have both timestamps.
But it has a little limitation.

Create and update timestamps with mysql.

Bogdan Gusiev
You aren't really supposed to post links to blog articles (especially your own) it kind of defeats the purpose. +1 anyway as the information was good.
Xenph Yan
Link is now dead.
Domster