默认使用CURDATE()时在mysql中创建表失败 [英] Create table fail in mysql when using CURDATE() as default

查看:209
本文介绍了默认使用CURDATE()时在mysql中创建表失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用phpmyadmin sql控制台创建下表:

I'm trying to create the following table using phpmyadmin sql console:

CREATE TABLE dates
(
id int NOT NULL,
id_date datetime NOT NULL DEFAULT CURDATE(),
PRIMARY KEY (id)
)

但是我得到以下错误:

However I get the following error:

它以红色显示"CURDATE()",所以我想这就是问题所在.

It shows "CURDATE()" in red, so I guess that's the problem.

有人可以帮我吗?

推荐答案

您不能使用CURDATE()作为默认值.

You can't use CURDATE() as a default value.

相反,您可以将TIMESTAMP列与DEFAULT CURRENT_TIMESTAMP一起使用.然后,您将不得不忽略其时间部分.

Instead you can use a TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP. Then you will have to ignore the time part of it.

示例SQL代码:

CREATE TABLE dates
(
    id int NOT NULL,
    id_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
);
INSERT INTO dates (id) VALUES (1);
SELECT id, DATE(id_date) AS id_date FROM dates;

结果:


id  id_date
1   2010-09-12

这篇关于默认使用CURDATE()时在mysql中创建表失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆