是否将DATE类型列的默认值设置为当前日期而没有时间部分? [英] Setting default value for DATE type column to current date without time part?

查看:327
本文介绍了是否将DATE类型列的默认值设置为当前日期而没有时间部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:问题是关于DATE类型,而不是日期时间或时间戳

如何更改日期数据类型的列以默认使用当前日期? 我看到了很多datetime的示例(带时间部分),但没有日期示例.我已经尝试过:

How to alter column of date data type to use current date by default? I saw a lot of examples for datetime (with time part), but not for date. I have tried:

ALTER TABLE `accounting` ALTER `accounting_date` 
  SET DEFAULT CURRENT_DATE;
ALTER TABLE `accounting` CHANGE `accounting_date` 
  `accounting_date` DATE NOT NULL DEFAULT CURRENT_DATE;

我还尝试了CURDATE()NOW()CURRENT_DATE() ...

I also tried with CURDATE(), NOW(), CURRENT_DATE() ...

推荐答案

可能您无法在mysql中为'date'数据类型设置默认值.您需要将类型更改为时间戳或日期时间.

Probably you cannot set default value for 'date' data type in mysql. You need to change the type to timestamp or datetime.

您可以看看这个类似的问题.

You may have a look at this similar question.

日期"的默认默认值

在5.6.5版中,可以在datetime列上设置默认值,甚至可以创建一个在更新行时将更新的列.类型定义:

In version 5.6.5, it is possible to set a default value on a datetime column, and even make a column that will update when the row is updated. The type definition:

CREATE TABLE foo (
    `creation_time`     DATETIME DEFAULT CURRENT_TIMESTAMP,
    `modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP
)

参考: http://optimize -this.blogspot.com/2012/04/datetime-default-now-finally-available.html

这篇关于是否将DATE类型列的默认值设置为当前日期而没有时间部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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