“dateAdded"的默认值无效

2021-11-20 00:00:00 sql mysql

我遇到了一个无法修复的愚蠢的 SQL 问题.

I got a stupid problem with SQL that I can't fix.

ALTER TABLE  `news` 
 ADD  `dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AUTO_INCREMENT ,
 ADD PRIMARY KEY (  `dateAdded` )

错误:

(#1067)Invalid default value for 'dateAdded'

有人可以帮我吗?

推荐答案

CURRENT_TIMESTAMP 仅适用于 TIMESTAMP 字段.DATETIME 字段必须保留空默认值,或者根本没有默认值 - 默认值必须是常量值,而不是表达式的结果.

CURRENT_TIMESTAMP is only acceptable on TIMESTAMP fields. DATETIME fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.

相关文档:http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

您可以通过在表上设置插入后触发器以在任何新记录上填充现在"值来解决此问题.

You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.

相关文章