Posted by: prajapatinilesh | October 11, 2007

Is it possible to set current date/time as default value in date column in mysql?

Data Type Default Values

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column.

Reference pages:

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

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


Responses

  1. ALTER TABLE paintings CHANGE creation_date creation_date DATETIME DEFAULT ‘now()’ not null’;

    Join the hfvs.net Community for more info/help and ask in the I.T. Section.

    — Neddy.

  2. Thanks alot, i found an answer here, but how can i use CURDATE() as a default except in my queries?

  3. use DataType timestamp and whenever u add a record it will automatically add current date.


Leave a comment

Categories