MySQL “日期”的默认值无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11573586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid default value for 'Date'
提问by Shahid Ghafoor
I want to set date as default value for date in mysql (not timestamp), but the following error appear
我想将日期设置为mysql中日期的默认值(不是时间戳),但出现以下错误
ALTER TABLE `RMS`.`transactionentry`
CHANGE `Date` `Date` DATE DEFAULT NOW() NOT NULL
Error
错误
Invalid default value for 'Date'
Same Case
同案
alter table `RMS`.`transactionentry`
change `Date` `Date` date default 'CURRENT_DATE' NOT NULL
回答by manurajhada
alter table `RMS`.`transactionentry`
change `Date` `Date` date default current_timestamp NOT NULL
Updated:
更新:
I don't think you can achieve that with mysql date
. You have to use timestamp
or try this approach..
我认为您无法通过 mysql 实现这一目标date
。您必须使用timestamp
或尝试这种方法..
CREATE TRIGGER transactionentry_OnInsert BEFORE INSERT ON `RMS`.`transactionentry`
FOR EACH ROW SET NEW.dateColumn = IFNULL(NEW.dateColumn, NOW());
回答by Both FM
The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. 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 a TIMESTAMP column.
数据类型规范中的 DEFAULT value 子句指示列的默认值。除了一个例外,默认值必须是一个常量;它不能是函数或表达式。这意味着,例如,您不能将日期列的默认值设置为 NOW() 或 CURRENT_DATE 等函数的值。例外是您可以将 CURRENT_TIMESTAMP 指定为 TIMESTAMP 列的默认值。
回答by Pravin
remember it could be an issue with MYSQL version itself 5.6 allows timestamp with out a default value and doesn't requires to set them as null however 5.7 requires you to explicitly set the default if not then nul
请记住,这可能是 MYSQL 版本本身的问题 5.6 允许没有默认值的时间戳,并且不需要将它们设置为 null 但是 5.7 要求您显式设置默认值,如果不是,则为 nul
回答by Xesued
According to the MySQL site:
根据 MySQL 站点:
The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. 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 a TIMESTAMP column.
数据类型规范中的 DEFAULT value 子句指示列的默认值。除了一个例外,默认值必须是一个常量;它不能是函数或表达式。这意味着,例如,您不能将日期列的默认值设置为 NOW() 或 CURRENT_DATE 等函数的值。例外是您可以将 CURRENT_TIMESTAMP 指定为 TIMESTAMP 列的默认值。
So:
所以:
alter table `RMS`.`transactionentry`
change `Date` `Date` date default CURRENT_TIMESTAMP NOT NULL
should work. Your issue was you're making it a string by placing quotes around it.
应该管用。你的问题是你通过在它周围加上引号来使它成为一个字符串。
回答by Pablo Siciliano
You can change the column definition with:
您可以使用以下方法更改列定义:
ALTER TABLE transactionentry MODIFY COLUMN `Date` date null;
Allow null works for me.
允许 null 对我有用。