如何将MySQL DateTime(不是TIMESTAMP)的默认值设置为NOW()或Current_DateTIme? [英] How to set default value of MySQL DateTime ( not TIMESTAMP ) to NOW() or Current_DateTIme?
问题描述
可能重复:
如何设置MySQL Datetime列的默认值?
我有一个表的数据类型为datetime的列CreatedDate,我想要能够将其默认值设置为当前的DateTime我如何做? p>
我试过Now()和CurrentTimestamp,但没有运气到目前为止!!!
您只能在表定义中设置静态默认值。
因此,除非您要每分钟调用 ALTER TABLE
/ p>
使用触发器:
DELIMITER $$
CREATE TRIGGER bu_table1_each在更新之前table1 FOR EACH ROW
BEGIN
SET new.datefield = NOW();
END $$
DELIMITER;
请参阅: http://dev.mysql.com/doc/refman/5.5/en/triggers.html
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!!!
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
这篇关于如何将MySQL DateTime(不是TIMESTAMP)的默认值设置为NOW()或Current_DateTIme?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!