开发者

How to set default value of MySQL DateTime ( not TIMESTAMP ) to NOW() or Current_DateTIme? [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

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

I have a table with column CreatedDate of data typ开发者_JS百科e 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!!!


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


I do not think you can do it with DateTime.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜