tags:

views:

122

answers:

1

I'd like to accomplish something like the following in a MySQL Create table action

date_created datetime NOT NULL DEFAULT CURRENT_DATETIME,  
date_updated datetime NOT NULL ON UPDATE CURRENT_DATETIME,

Once again, this is a MySQL create table action and these are two of the fields in my table.

+2  A: 

Have a look at this

TIMESTAMP Properties

From the link you can use

CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                             ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                             DEFAULT CURRENT_TIMESTAMP);
astander
Is there not one for datetime?
Jonathan Kushner
There is also an explicitly documented restriction that you can only have the default and auto-update on a single column - not split across two columns as requested. And only one column can be treated thusly. Seems like an odd restriction; the scenario in the question is very common. If you could do it on two columns, then the 'date_created' column would be given a default without the ON UPDATE clause and the date updated would have both the default and ON UPDATE clauses.
Jonathan Leffler