8

Possible Duplicate:
How do you set a default value for a MySQL Datetime column?

I have a table with column CreatedDate of data type datetime and i want to be able to set its default value to current DateTime how do i do it?

I tried Now() and CurrentTimestamp but no luck so far!!!

Community
  • 1
  • 1

2 Answers2

5

You can only set a static default in the table definition.
So unless you want to call ALTER TABLE every minute....

Use a trigger:

DELIMITER $$

CREATE TRIGGER bu_table1_each BEFORE UPDATE ON table1 FOR EACH ROW
BEGIN
  SET new.datefield = NOW();
END $$

DELIMITER ;

See: http://dev.mysql.com/doc/refman/5.5/en/triggers.html

Johan
  • 74,508
  • 24
  • 191
  • 319
1

I do not think you can do it with DateTime.

See: How do you set a default value for a MySQL Datetime column?

Community
  • 1
  • 1
djdy
  • 6,779
  • 6
  • 38
  • 62