mysql - 设置 DATE 或 DATETIME 默认值时 MySQL 出错

标签 mysql sql date datetime console

我正在运行 MySql Server 5.7.11 和这句话:

updated datetime NOT NULL DEFAULT '0000-00-00 00:00:00'

工作。给出错误:

ERROR 1067 (42000): Invalid default value for 'updated'

但以下内容:

updated datetime NOT NULL DEFAULT '1000-01-01 00:00:00'

正常工作

DATE 的情况相同。

作为旁注MySQL docs 中提到了它。 :

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

即使他们也说:

Invalid DATE, DATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00').

还考虑到 MySQL 文档中的第二个引用,有人可以让我知道为什么会出现该错误吗?

最佳答案

该错误是由于 sql 模式造成的,根据最新的 MYSQL 5.7 文档,该模式可能是严格模式

MySQL Documentation 5.7 says :

Strict mode affects whether the server permits '0000-00-00' as a valid date: If strict mode is not enabled, '0000-00-00' is permitted and inserts produce no warning. If strict mode is enabled, '0000-00-00' is not permitted and inserts produce an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, '0000-00-00' is permitted and inserts produce a warning.

检查MYSQL模式

SELECT @@GLOBAL.sql_mode 全局、@@SESSION.sql_mode session

禁用 STRICT_TRANS_TABLES 模式

但是,要允许格式 0000-00-00 00:00:00,您必须在 mysql 配置文件中或通过命令禁用 STRICT_TRANS_TABLES 模式

通过命令

SET sql_mode = '';

设置全局 sql_mode = '';

使用关键字GLOBAL需要 super 特权,它会影响从那时起所有客户端连接的操作

如果上面不起作用,请转到/etc/mysql/my.cnf(按照ubuntu)并注释掉STRICT_TRANS_TABLES

此外,如果您想在服务器启动时永久设置 sql 模式,请在 Linux 或 MacOS 上的 my.cnf 中包含 SET sql_mode=''。对于 Windows,这必须在 my.ini 文件中完成。

注意

但是 MYSQL 5.6 中默认不启用严格模式。因此,它不会产生 MYSQL 6 documentation 所示的错误。其中说

MySQL permits you to store a “zero” value of '0000-00-00' as a “dummy date.” This is in some cases more convenient than using NULL values, and uses less data and index space. To disallow '0000-00-00', enable the NO_ZERO_DATE SQL mode.

更新

关于@Dylan-Su 所说的错误问题:

我不认为这是 MYSQL 随着时间的推移而演变的错误,由于产品的进一步改进,一些事情发生了变化。

但是我还有另一个关于 NOW() 函数的相关错误报告

Datetime field does not accept default NOW()

另一个有用的注释 [参见 Automatic Initialization and Updating for TIMESTAMP and DATETIME ]

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table. The following notes first describe automatic initialization and updating for MySQL 5.6.5 and up, then the differences for versions preceding 5.6.5.

有关 NO_ZERO_DATE 的更新

从 MySQL 5.7.4 开始,此模式已被弃用。对于以前的版本,您必须注释掉配置文件中的相应行。请参阅MySQL 5.7 documentation on NO_ZERO_DATE

关于mysql - 设置 DATE 或 DATETIME 默认值时 MySQL 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51221597/

相关文章:

php - MySQL 返回类型差异 - NULL、EMPTY、FALSE

java - 使用 Mysql 的 Java Web 应用程序中的增补字符支持

MySQL 查询需要将现有日期更新 8

bash - 在 MAC OSX 中比较 Bash 中的两个日期

php - 如何从 MySQL 表中正确获取结果

sql - 在 SQL 中,如果可用,如何获取具有特定列值的行,否则获取任何其他行

mysql - 理解加盟

mysql - SQL 插入(从中选择)

javascript - 如何使用javascript从字符串中提取日期

javascript - javascript 中的过去日期永远不会显示正确的结果