mysql原理和优化使用 > mysql配置和配置优化
sqlmode 对sql执行的影响

发现问题:

今天在导入测试数据时候遇到一个问题, 将原始数据库导入到新的数据库导入失败 timestamp 无效的默认值. 


原始数据库mysql版本是5.6 在创建数据表的时候设置的默认值是 "0000-00-00 00:00:00"  表结构如下 

CREATE TABLE `tablename` (                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
//这里省略几行...																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																													
 `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
 `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
 `online_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
 `deleted_at` timestamp NULL DEFAULT NULL,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
) ENGINE=InnoDB AUTO_INCREMENT=100201 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

查阅资料得知,mysql5.7版本中有了一个STRICT mode(严格模式),而在此模式下默认是不允许设置日期的值为全0值的.


解决问题:

既然 msql 默认是不允许 "0000-00-00 00:00:00" 这样, 拿就修改让他允许呗. 查阅资料 这个是由

NO_ZERO_IN_DATE,NO_ZERO_DATE 控制, 

查看当前 支持的模式: 

SELECT @@global.sql_mode;

@@global.sql_mode                                                                                             
--------------------------------------------------------------------------------------------------------------
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,
ERROR_FOR_DIVISION_BY_ZERO,
NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION  

去掉NO_ZERO_IN_DATE,NO_ZERO_DATE

SET @@global.sql_mode=(SELECT REPLACE(@@global.sql_mode,'NO_ZERO_IN_DATE,NO_ZERO_DATE',''));

再次测试问题解决:


上面引出了sqlmode 概念 ,下面简单介绍一下各种sqlmode 吧:

几种常见的mode介绍

ONLY_FULL_GROUP_BY:

出现在select语句、HAVING条件和ORDER BY语句中的列,必须是GROUP BY的列或者依赖于GROUP BY列的函数列。


NO_AUTO_VALUE_ON_ZERO:

该值影响自增长列的插入。默认设置下,插入0或NULL代表生成下一个自增长值。如果用户希望插入的值为0,而该列又是自增长的,那么这个选项就有用了。


STRICT_TRANS_TABLES:

在该模式下,如果一个值不能插入到一个事务表中,则中断当前的操作,对非事务表不做限制


NO_ZERO_IN_DATE:

这个模式影响了是否允许日期中的月份和日包含0。如果开启此模式,2016-01-00是不允许的,但是0000-02-01是允许的。它实际的行为受到 strict mode是否开启的影响1。


NO_ZERO_DATE:

设置该值,mysql数据库不允许插入零日期。它实际的行为受到 strictmode是否开启的影响2。


ERROR_FOR_DIVISION_BY_ZERO:

在INSERT或UPDATE过程中,如果数据被零除,则产生错误而非警告。如果未给出该模式,那么数据被零除时MySQL返回NULL


NO_AUTO_CREATE_USER:

禁止GRANT创建密码为空的用户


NO_ENGINE_SUBSTITUTION:

如果需要的存储引擎被禁用或未编译,那么抛出错误。不设置此值时,用默认的存储引擎替代,并抛出一个异常


PIPES_AS_CONCAT:

将”||”视为字符串的连接操作符而非或运算符,这和Oracle数据库是一样的,也和字符串的拼接函数Concat相类似


ANSI_QUOTES:

启用ANSI_QUOTES后,不能用双引号来引用字符串,因为它被解释为识别符