MySQL timespan设置 sql_mode设置
Timespan设置:
在MySQL5.7版本中创建表
CREATE TABLE `investor_seat` ( `id` int(11) NOT NULL AUTO_INCREMENT , `investorId` int(11) NOT NULL COMMENT '投资人id', `seatId` int(11) NOT NULL COMMENT '席位id', `maybe` float NOT NULL COMMENT '席位归属某帮派或者某人的概率', `investorName` varchar(28) NOT NULL COMMENT '投资人名含机构', `seatName` varchar(108) NOT NULL COMMENT '席位名', `status` TINYINT NOT NULL DEFAULT 0 COMMENT '投资人席位状态', `addTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `updateTime` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARSET = utf8;
注意updateTime字段,会在创建的时候报错:
[Err] 1067 - Invalid default value for 'updateTime'
修改:
`updateTime` timestamp NOT NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Sql_mode设置:
从当前环境变量查看MySQL的sql_mode配置:
mysql> show variables like 'sql_mode'; +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ | sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------+-------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.00 sec)
从当前连接会话查看sql_mode:
select @@session.sql_mode;
查看全局的sql_mode:
select @@global.sql_mode;
临时修改sql_mode:
set session sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
永久修改:
将mysql安装文件里面的my.ini(windows)//etc/my.cnf(Linux)里面添加设置:
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
或者
sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
在这里自己遇到一个大坑:
安装Mysql的时候配置文件命名为my-default.ini,结果设置sql_mode,重启服务,甚至重启电脑都无效,将my-default.ini改成my.ini再重启服务就OK了!
==============================================
具体出错提示:
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
下载安装的是最新版的mysql5.7.x版本,默认是开启了 only_full_group_by 模式的,但开启这个模式后,原先的 group by 语句就报错,然后又把它移除了。
一旦开启 only_full_group_by ,感觉,group by 将变成和 distinct 一样,只能获取受到其影响的字段信息,无法和其他未受其影响的字段共存,这样,group by 的功能将变得十分狭窄了
only_full_group_by 模式开启比较好。
因为在 mysql 中有一个函数: any_value(field) 允许,非分组字段的出现(和关闭 only_full_group_by 模式有相同效果)。
select @@global.sql_mode -- 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
去掉ONLY_FULL_GROUP_BY,重新设置值。
改变全局sql_model:
set @@global.sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
对于已存在的数据库,则需要在对应的数据下执行:
set sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
http://www.nowck.com/archives/70.html
http://blog.csdn.net/wyzxg/article/details/8787878
http://blog.sina.com.cn/s/blog_49a665e10100cb52.html
https://blog.csdn.net/jiang1245764446/article/details/72898137