views:

125

answers:

1

When am trying to set default date and default sysdate am getting following errors:

MySQL Query:

create table product_offer_type(object_id INT(19), snapshot_id INT(19), PRIMARY KEY(object_id,snapshot_id), enum_value VARCHAR(64) NOT NULL, external_name VARCHAR(64) NOT NULL, description VARCHAR(255), business_validation INT(1), valid_for_start_date_time DATE  DEFAULT '1900-01-10', valid_for_end_date_time DATE  DEFAULT '4712-01-01', mutation_date_time DATE SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER');

Error Message:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SYSDATE, mutation_user VARCHAR(32) DEFAULT 'USER')' at line 1

Any pointer's would be highly appreciated.

+1  A: 

If you're trying to track the last modification time of the row, I usually use something like

mutation_date_time timestamp default current_timestamp, on update current_timestamp

You might need slight modification if you really want Date and not timestamp.

Keith Randall
I am looking for date instead of timestamp, I will try modification of provided approach and update comment in couple of mins.
Rachel
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'current_date, mutation_user VARCHAR(32) DEFAULT 'USER')' at line 1
Rachel
current_timestamp only works with the timestamp type of column , so you will have to convert your column type to timestamp to be able to automatically put in those times.
Sabeen Malik
I dont't think current_date is defined - just use current_timestamp - it should get rounded correctly when assigned to a date field (I think - be sure to test it).
Keith Randall
@Rachel .. current_date wont work .. there is no way to be able to set default for date columns dynamically , so ur stuck with timestamp columns
Sabeen Malik
@Keith .. i believe timestamp is the only column type which allows automatic timestamp storage on insert and update .. mixing current_timestamp with date column errors out.
Sabeen Malik
OK, then I guess Rachel has to use a timestamp field and then use DATE(FROM_UNIXTIME(mutation_date_time)) to get just the date part.
Keith Randall
Is this the only option left ?
Rachel
You can always delcare mutation_date_time as a Date and then populate it explicitly on every update. You have to set mutation_user explicitly anyway, so it shouldn't be hard - just set it to DATE(NOW()).
Keith Randall
My main motive is to get Sysdate and so I guess Timestamp would work for me as it gives date as well as time.
Rachel