mysql的sql_mode模式

博主写的触发器在本地执行正常,在公司库中运行报1055错误。此问题由MySQL的sql_mode语句检查模式导致,可查询sql_mode,将其置为空字符串或其他模式解决报错。不同模式对应不同功能,修改在MySQL重启后会失效,可修改my.ini文件实现永久有效。

今天写了一个触发器,在本地执行没有问题,在公司库中运行报错:[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and。。。。

这个问题是由于mysql 的sql_mode语句检查模式引起,我们可以查询sql_mode

show variables like "sql_mode";

将sql_mode 置为空字符串或者其他模式可以解决报错

set sql_mode='';
set sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES';

不同的模式对应不同的功能如下:

ONLY_FULL_GROUP_BY

对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么将认为这个SQL是不合法的,因为列不在GROUP BY从句中

STRICT_TRANS_TABLES

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

NO_ZERO_IN_DATE

在严格模式,不接受月或日部分为0的日期。如果使用IGNORE选项,我们为类似的日期插入'0000-00-00'。在非严格模式,可以接受该日期,但会生成警告。

NO_ZERO_DATE

在严格模式,不要将 '0000-00-00'做为合法日期。你仍然可以用IGNORE选项插入零日期。在非严格模式,可以接受该日期,但会生成警告

ERROR_FOR_DIVISION_BY_ZERO

在严格模式,在INSERT或UPDATE过程中,如果被零除(或MOD(X,0)),则产生错误(否则为警告)。如果未给出该模式,被零除时MySQL返回NULL。如果用到INSERT IGNORE或UPDATE IGNORE中,MySQL生成被零除警告,但操作结果为NULL。

NO_AUTO_CREATE_USER

防止GRANT自动创建新用户,除非还指定了密码。

NO_ENGINE_SUBSTITUTION

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

mysql5.0以上版本支持三种sql_mode模式:

ANSI模式

宽松模式,对插入数据进行校验,如果不符合定义类型或长度,对数据类型调整或截断保存,报warning警告。

TRADITIONAL模式

严格模式,当向mysql数据库插入数据时,进行数据的严格校验,保证错误数据不能插入,报error错误。用于事物时,会进行事物的回滚。

STRICT_TRANS_TABLES模式

严格模式,进行数据的严格校验,错误数据不能插入,报error错误。

 

以上修改,mysql 重启后会失效,想要永久有效可以直接修改my.ini文件

### MySQL 8 SQL Mode Configuration and Settings In MySQL 8, the `sql_mode` system variable plays a critical role in defining how the server should handle data validation checks and syntax interpretation[^1]. The default value of this setting has been tightened to enforce stricter standards compared to previous versions. The typical default `sql_mode` for MySQL 8 includes several modes that ensure higher compliance with standard SQL behavior: - **STRICT_TRANS_TABLES**: Ensures strict checking on transactions involving transactional tables. - **NO_ZERO_IN_DATE**, **NO_ZERO_DATE**: Prevents invalid dates from being stored. - **ERROR_FOR_DIVISION_BY_ZERO**: Throws an error when division by zero occurs during insert or update operations. - **NO_AUTO_CREATE_USER**: Disallows automatic creation of users without explicit privilege grants. - **NO_ENGINE_SUBSTITUTION**: Issues warnings if storage engine substitution happens due to unavailable engines specified at table creation time. To view current SQL mode settings within MySQL 8, one can execute the following command: ```sql SELECT @@GLOBAL.sql_mode GLOBAL, @@SESSION.sql_mode SESSION; ``` For modifying these configurations either globally or session-wise, commands like below are used respectively: ```sql SET GLOBAL sql_mode='NEW_MODE'; SET SESSION sql_mode='NEW_MODE'; ``` When adjusting `sql_mode`, caution is advised as changes might affect application compatibility. For instance, removing certain restrictive flags could lead to less stringent enforcement which may introduce potential issues such as storing improper date values or allowing divisions by zero silently failing instead of raising errors[^2]. Moreover, altering `sql_mode` directly through queries affects only runtime behaviors; permanent modifications require editing configuration files (`my.cnf` or `my.ini`) under `[mysqld]` section followed by restarting the service[^3]: ```ini [mysqld] sql-mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值