MySQL 具有默认 CURDATE() 的日期字段。不是日期时间
MySQL DATE field with default CURDATE(). NOT DATETIME
可以将 MySQL 5.7 中的 DATE(NOT DATETIME)列的默认值设置为当前日期?
我试试这个(由 Workbench 生成):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
但不适合我。
(table 中没有数据)
不,你不能。 documentation 对此非常清楚:
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”.
您可以执行以下操作之一:
- 为
DATETIME
设置一个具有默认值的列。创建将日期提取为单独列的视图。
- 创建一个
insert
触发器来设置 date
列。
如果您有另一列具有例如默认为 NOW() 的日期时间字段,则有一种方法可以执行此操作。 See this post:
可以将 MySQL 5.7 中的 DATE(NOT DATETIME)列的默认值设置为当前日期?
我试试这个(由 Workbench 生成):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
但不适合我。 (table 中没有数据)
不,你不能。 documentation 对此非常清楚:
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”.
您可以执行以下操作之一:
- 为
DATETIME
设置一个具有默认值的列。创建将日期提取为单独列的视图。 - 创建一个
insert
触发器来设置date
列。
如果您有另一列具有例如默认为 NOW() 的日期时间字段,则有一种方法可以执行此操作。 See this post: