带有默认CURDATE()的MySQL DATE字段.不是DATETIME [英] MySQL DATE field with default CURDATE(). NOT DATETIME
问题描述
是否可以在MySQL 5.7的DATE( NOT DATETIME )列上设置默认值到当前日期?
我尝试了这一点(由Workbench生成):
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):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
但不适用于我. (表中没有数据)
but not works for me. (no data in table)
推荐答案
否,您不能. 文档在以下内容上很清楚:
No, you cannot. The documentation is pretty clear on this:
例如,这意味着您不能为日期设置默认值 column为函数的值,例如NOW()或CURRENT_DATE. 唯一的例外是您可以将CURRENT_TIMESTAMP指定为默认值 TIMESTAMP和DATETIME列.请参见第12.3.5节自动 TIMESTAMP和DATETIME的初始化和更新".
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:
- 使用
DATETIME
的默认值设置一列.创建将日期提取为单独列的视图. - 创建一个
insert
触发器以设置date
列.
- 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 thedate
column.
这篇关于带有默认CURDATE()的MySQL DATE字段.不是DATETIME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!