mysql sql_mode 汇总整理

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介:

-- ANSI 使sql符合标准sql
This mode changes syntax and behavior to conform more closely to standard SQL


-- STRICT_TRANS_TABLES 如果事务语句有错,则使事务失败
If a value could not be inserted as given into a transactional table, abort the statement


-- TRADITIONAL 使用error替代warning
this mode is “give an error instead of a warning” when inserting an incorrect value into a column


-- ALLOW_INVALID_DATES 允许非法日期
Do not perform full checking of dates. Check only that the month is in the range from 1 to 12 and the day is in the range from 1 to 31
This mode applies to DATE and DATETIME columns. It does not apply TIMESTAMP columns, which always require a valid date
With strict mode disabled, invalid dates such as '2004-04-31' are converted to '0000-00-00' and a warning is generated. With strict mode enabled, invalid dates generate an error.


-- ANSI_QUOTES 使双引号当在一个标识符,而不 是一个字符串标识
Treat "" as an identifier quote character (like the "`" quote character) and not as a string quote character
--启用 ANSI_QUOTES 表名加双引号正常执行
mysql>  SET sql_mode='ANSI_QUOTES';
Query OK, 0 rows affected (0.01 sec)
mysql> select "id",id from "t" limit 3;
+----+----+
| id | id |
+----+----+
|  1 |  1 |
|  2 |  2 |
+----+----+
3 rows in set (0.00 sec
--关闭 ANSI_QUOTES 表名加双引报错
mysql>  SET sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select "id",id from "t" limit 10;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"t" limit 10' at line 1


-- ERROR_FOR_DIVISION_BY_ZERO 决定被0整除是后的行为? 
If this mode is not enabled, division by zero inserts NULL and produces no warning.
If this mode is enabled, division by zero inserts NULL and produces a warning.
If this mode and strict mode are enabled, division by zero produces an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, division by zero inserts NULL and produces a warning.


-- HIGH_NOT_PRECEDENCE 控制 not 的优化级
The precedence of the NOT operator is such that expressions such as NOT a BETWEEN b AND c are parsed as NOT (a BETWEEN b AND c)
mysql> SET sql_mode = '';
mysql> SELECT NOT 1 BETWEEN -5 AND 5;
-> 0
mysql> SET sql_mode = 'HIGH_NOT_PRECEDENCE';
mysql> SELECT NOT 1 BETWEEN -5 AND 5;
-> 1



-- NO_AUTO_CREATE_USER 阻止grant自动创建用户,如果没有提供密码或空密码是不认的
Prevent the GRANT statement from automatically creating new user accounts if it would otherwise do so, unless authentication information is specified
The statement must specify a nonempty password using IDENTIFIED BY or an authentication plugin using IDENTIFIED WITH


-- NO_AUTO_VALUE_ON_ZERO 当插入null或0者是否对 AUTO_INCREMENT 产生下一个值
NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. 
NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.


-- NO_DIR_IN_CREATE 在创建表或索引时是否忽略目录参数,注意如果主从的目录结构不同,则可使用此参数
When creating a table, ignore all INDEX DIRECTORY and DATA DIRECTORY directives. This option is useful on slave replication servers.


-- NO_ENGINE_SUBSTITUTION 当创建表或修改表时使用了一个非法或disable的存储引擎时,是否使用其它替代的引擎
Control automatic substitution of the default storage engine when a statement such as CREATE TABLE or ALTER TABLE specifies a storage engine that is disabled or not compiled in


-- NO_ZERO_DATE 控制00是否可做为一个日期类型
The NO_ZERO_DATE mode affects whether the server permits '0000-00-00' as a valid date. Its effect also depends on whether strict SQL mode is enabled.
If this mode is not enabled, '0000-00-00' is permitted and inserts produce no warning.
If this mode is enabled, '0000-00-00' is permitted and inserts produce a warning.
If this mode and strict mode are 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.

-- NO_ZERO_IN_DATE 控制在年份不为0,但月或天是否可为0,影响如上
The NO_ZERO_IN_DATE mode affects whether the server permits dates in which the year part is nonzero but the month or day part is 0


-- ONLY_FULL_GROUP_BY 使group操作符合标准聚合操作
Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns


-- PIPES_AS_CONCAT 使双竖线成为连接符,类似于postgresql和oracle中的||
Treat || as a string concatenation operator (same as CONCAT()) rather than as a synonym for OR.


-- STRICT_ALL_TABLES 使严格模式使用于所有的表,非法数据被拒绝,推荐启用
Enable strict SQL mode for all storage engines. Invalid data values are rejected

-- STRICT_TRANS_TABLES 使严格事务模式使用于所有的存储引擎,推荐启用
Enable strict SQL mode for transactional storage engines, and when possible for nontransactional storage engines


-- mysql还针对不同的数据库提供了组合模式
ANSI,DB2,ORACLE,POSTGRESQL,MSSQL

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
SQL 存储 关系型数据库
MySQL下使用SQL命令进行表结构与数据复制实践
MySQL下使用SQL命令进行表结构与数据复制实践
157 0
|
SQL 关系型数据库 MySQL
MySQl数据库第八课-------SQL命令查询-------主要命脉2
MySQl数据库第八课-------SQL命令查询-------主要命脉
|
SQL 关系型数据库 MySQL
MySQl数据库第八课-------SQL命令查询-------主要命脉 1
MySQl数据库第八课-------SQL命令查询-------主要命脉
120 0
|
SQL 存储 开发框架
MySQl数据库第六课-------SQl命令的延续------快来看看
MySQl数据库第六课-------SQl命令的延续------快来看看
|
SQL 存储 NoSQL
MySQl数据库第五课 --------在SQl的简单命令--------学习学习
MySQl数据库第五课 --------在SQl的简单命令--------学习学习
|
SQL 缓存 监控
MySQL性能监控(sql命令语句 & MONyog工具)全掌握,这一篇足以!
MySQL性能监控(sql命令语句 & MONyog工具)全掌握,这一篇足以!
938 0
MySQL性能监控(sql命令语句 & MONyog工具)全掌握,这一篇足以!
|
SQL 关系型数据库 MySQL
软件测试mysql面试题:SQL命令有哪些不同类型?
软件测试mysql面试题:SQL命令有哪些不同类型?
90 0
|
SQL 关系型数据库 MySQL
软件测试mysql面试题:SQL中有哪些不同的DDL命令?
软件测试mysql面试题:SQL中有哪些不同的DDL命令?
78 0
|
SQL 关系型数据库 MySQL
软件测试mysql面试题:SQL中有哪些不同的DCL命令?
软件测试mysql面试题:SQL中有哪些不同的DCL命令?
94 0
|
SQL 关系型数据库 MySQL
软件测试mysql面试题:SQL中有哪些不同的DML命令?
软件测试mysql面试题:SQL中有哪些不同的DML命令?
85 0