Can a field with DATE type have a DEFAULT value with CURRENT_TIMESTAMP?

那年仲夏 提交于 2019-12-11 18:15:58

问题


I know that for a DATETIME column you can you CURRENT_TIMESTAMP but what about a DATE column? I don't need hours, minutes and seconds and while trying to set CURRENT_TIMSTAMP as default for I get an error. Thanks


回答1:


A date or time type columns can be defined with a default 0 but not a curdate() or curtime().

As per documentation on 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.




回答2:


on update CURDATE();   

Try this if it works

ALTER TABLE `tablename` 
   CHANGE `tablename` `tablename`timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP


来源:https://stackoverflow.com/questions/22197178/can-a-field-with-date-type-have-a-default-value-with-current-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!