使用 Curdate() 时出现语法错误

Syntax error when using Curdate()

我正在尝试创建一个 table,其中包含一个跟踪插入行时间的列。

下面的查询工作正常。 table 创建完成,添加数据后可以在 track_date 列中查看日期和时间。

CREATE TABLE tracker(id int, track_date DATETIME DEFAULT NOW());

假设我现在只需要跟踪日期,所以我现在 运行 下面的查询但是我得到语法错误。

CREATE TABLE tracker_2(id int, track_date DATE DEFAULT CURDATE());

能否请教一下。

MySQLdocumentation在这一点上说的很清楚了。您可以为 DATETIMETIMESTAMP 列设置默认值,但不能为日期设置默认值:

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.

您的选择是使用触发器或在查询列时删除时间部分。

(我应该注意到该声明有点误导,因为 NOW() 可以 用作 DATETIME 的默认值作为两个问题以及要澄清的文档。该段特别指的是 DATE 列。)