gpt4 book ai didi

mysql - MySQL 告诉我 "Invalid default value for ' postdate'"的原因是什么?

转载 作者:行者123 更新时间:2023-11-29 12:05:00 25 4
gpt4 key购买 nike

我有 2 台运行 MySQL 的 Windows 服务器,其中一台运行版本 5.1,另一台运行版本 5.7。我正在尝试将数据库从 MySQL 5.7 复制到另一台服务器上的 5.1,并相信版本差异(5.7 中的新语法?)导致了此错误,但我可能是错的。

通过 phpMyAdmin 导出 5.7 版本中的数据库并尝试在 5.1 版本中导入后,出现错误

MySQL said: Documentation #1067 - Invalid default value for 'postdate'

关于命令

CREATE TABLE IF NOT EXISTS `jobs` (

`id` mediumint( 9 ) NOT NULL ,
`title` varchar( 200 ) DEFAULT NULL ,
`descr` varchar( 5000 ) DEFAULT NULL ,
`postdate` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB AUTO_INCREMENT =5 DEFAULT CHARSET = utf8mb4;

知道为什么吗?

最佳答案

在 5.1 中,默认值必须是常量值(例如,可接受 NULL),但 timestamp 类型除外,其中 current_timestamp 为允许。即,对于日期或日期时间,您不能使用 current_datenowcurrent_timestamp

因此,您要么坚持 postdate 列的 datetime 类型,并且必须放弃 current_timestamp 作为默认值(您可以也许为此目的设置一个触发器,请参阅示例 here ),或者 - 根据您的要求 - 考虑使用 timestamp (它具有不同的值范围)。

manual对应部分说:

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.

关于mysql - MySQL 告诉我 "Invalid default value for ' postdate'"的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31658836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com