MySQL DATE field with default CURDATE(). NOT DATETIME

前端 未结 2 1399
遥遥无期
遥遥无期 2020-12-06 19:31

It is possible to set default value on DATE (NOT DATETIME) column in MySQL 5.7 to current date?
I try this (generated by Workbench):

ALT         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 20:05

    No, you cannot. The documentation is pretty clear on this:

    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 TIMESTAMP and DATETIME columns. See Section 12.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

    You can do one of the following:

    • Set up a column with a default value for the DATETIME. Create view that extracts the date as a separate column.
    • Create an insert trigger to set the date column.

提交回复
热议问题