sql_mode :(STRICT_TRANS_TABLES与STRICT_ALL_TABLES 区别)
http://blog.csdn.net/wulantian/article/details/8905573
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_auto_create_user
http://blog.csdn.net/dslztx/article/details/47176549
http://www.cnblogs.com/javaleon/p/4024761.html
mysql> create table test( id int not null auto_increment primary key,browsertype enum('ie','firefox','360browser'),version int );
mysql> insert into test(browsertype) values('ie');
mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| | ie | NULL |
+----+-------------+---------+
row in set (0.00 sec) mysql> show create table test; CREATE TABLE `test` (
`id` int() NOT NULL AUTO_INCREMENT,
`browsertype` enum('ie','firefox','360browser') DEFAULT NULL,
`version` int() DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8 | mysql> insert into test(browsertype) values('maxthon');
Query OK, row affected, warning (0.19 sec) //warings mysql> show warnings;
+---------+------+--------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------+
| Warning | | Data truncated for column 'browsertype' at row |
+---------+------+--------------------------------------------------+
row in set (0.15 sec) mysql> insert into test(browsertype,version) values('firefox',);
Query OK, row affected (0.17 sec) mysql> insert into test(browsertype,version) values('360browser','a');
Query OK, row affected, warning (0.16 sec) //warnings 第5条记录,类型值不同,插入了 version 字段值为0 mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| | ie | NULL |
| | | NULL |
| | | NULL |
| | firefox | |
| 5 | 360browser | 0 |
+----+-------------+---------+
rows in set (0.00 sec) mysql> set sql_mode ="STRICT_TRANS_TABLES"; //设置 严格模式
Query OK, rows affected (0.00 sec) mysql> insert into test(browsertype,version) values('360browser','a'); //报错了并没有插入进出
ERROR (HY000): Incorrect integer value: 'a' for column 'version' at row 1
mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| 1 | ie | NULL |
| 2 | | NULL |
| 3 | | NULL |
| 4 | firefox | 2 |
| 5 | 360browser | 0 |
+----+-------------+---------+
5 rows in set (0.01 sec)
//并没有插入进出
mysql> set sql_mode=ansi; //设置非严格模式
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(browsertype,version) values('360browser','a');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| 1 | ie | NULL |
| 2 | | NULL |
| 3 | | NULL |
| 4 | firefox | 2 |
| 5 | 360browser | 0 |
| 6 | 360browser | 0 |
+----+-------------+---------+
6 rows in set (0.00 sec) //错误的数据已经插入进入表了
myisam引擎:
STRICT_TRANS_TABLES与STRICT_ALL_TABLES 区别:
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`browsertype` enum('ie','firefox','360browser') DEFAULT NULL,
`version` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
mysql> set sql_mode="STRICT_TRANS_TABLES";
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
Empty set (0.01 sec)
mysql> insert into test(id,browsertype,version) values (null,"ie",1),(null,"firefox",2),(null,"tt",3);
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 1
mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| 1 | ie | 1 |
| 2 | firefox | 2 |
| 3 | | 3 |
+----+-------------+---------+
3 rows in set (0.00 sec)
mysql> set sql_mode="STRICT_ALL_TABLES ";
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(id,browsertype,version) values (null,"ie",1),(null,"firefox",2),(null,"tt",3);
ERROR 1265 (01000): Data truncated for column 'browsertype' at row 3
mysql> select * from test;
+----+-------------+---------+
| id | browsertype | version |
+----+-------------+---------+
| 1 | ie | 1 |
| 2 | firefox | 2 |
| 3 | | 3 |
| 4 | ie | 1 |
| 5 | firefox | 2 |
+----+-------------+---------+
5 rows in set (0.00 sec)
停止语句执行,存在部分更新的问题
MySQL 处理非法数据 默认情况下,MySQL按照以下规则处理数据越界和其他非正常数据 、对于数值数据或time数据列,超出合法范围的值将被阶段到最近的取值范围边界;
、对于字符串数据列(不包括enum、默认情况下,MySQL按照以下规则处理“数据越界”和其他非正常数据
、对于数值数据或time数据列,超出合法范围的值将被阶段到最近的取值范围边界;
、对于字符串数据列(不包括enum、set)太长的字符串将被截断到数据列的最大长度,对于ENUM类型数据,MySQL将不合法成员用空字符串代替。如果赋值给某个set数据列包含非法子字符串,那mysql会删除那些子字符串并把剩下的赋值给该数据列
、对于日期和时间数据列,非法数值会被转换成‘’值替代
当发生上述情况的时候,MySQL会生成警告, 可使用 show warnings 查看。
、对于字符串序列,太长的字符串将被截短到数据列的最大长度
如果需要在插入或更新数据时进行更严格的检查,可以启用特殊的SQL MODE;
sql_mode='strict_all_tables,strict_trans_tables'
对于支持事务的表,这两种模式是一样的:如果发现某个值缺失或非法,MySQL将抛出错误,语句会停止运行并回滚。
对于不支持事务的表,这两种模式的效果:
、如果在插入或修第一个数据行时就发现某个值非法或缺失,那该语句直接抛错,语句停止执行。这个和支持事务的数据表行为时一样的。
、如果在插入或修改第n个(n>)数据行时才发现错误,那就会出现下面的情况:
2.1 在strict_all_tables模式下,停止语句执行,存在部分更新的问题
2.2 在strict_trans_tables模式下,MySQL将继续执行该语句避免“部分更新问题”,对每个非法值将其转换为最接近的合法值。
配合严格模式的其他几个SQL模式:
ERROR_FOR_DIVISION_BY_ZERO: 在严格模式下,遇到以0为除数的情况,拒绝插入数据库,(如果不在严格模式下,MYSQL将生成一条告警,并插入NULL值)
NO_ZERO_DATE:在严格模式下,拒绝0日期值进入数据库。
建议采用:strict_trans_tables ERROR_FOR_DIVISION_BY_ZERO
制止错误的另一个办法是在insert或update语句里使用IGNORE关键字,使非法值而导致的错误弱化为一个警告。
innodb引擎下: 严格模式:STRICT_TRANS_TABLES与strict_all_tables 是一样的
mysql> CREATE TABLE `test` ( `id` int() NOT NULL AUTO_INCREMENT, `browsertype` enum('ie','firefox','360browser') DEFAULT NULL, `version` int() DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=innodb AUTO_INCREMENT= DEFAULT CHARSET=utf8;
Query OK, rows affected (0.21 sec) mysql> set sql_mode="STRICT_TRANS_TABLES";
Query OK, rows affected (0.00 sec) mysql> insert into test(id,browsertype,version) values (null,"tt",),(null,"firefox",),(null,"ie",);
ERROR (): Data truncated for column 'browsertype' at row
mysql> select * from test;
Empty set (0.00 sec) mysql> insert into test(id,browsertype,version) values (null,"ie",),(null,"firefox",),(null,"tt",),(null,"ie",);
ERROR (): Data truncated for column 'browsertype' at row
mysql> select * from test;
Empty set (0.00 sec)
mysql> set sql_mode="strict_all_tables";
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(id,browsertype,version) values (null,"tt",1),(null,"firefox",2),(null,"ie",3);
ERROR 1265 (01000): Data truncated for column 'browsertype' at row 1
mysql> insert into test(id,browsertype,version) values (null,"ie",1),(null,"firefox",2),(null,"tt",3),(null,"ie",4);
ERROR 1265 (01000): Data truncated for column 'browsertype' at row 3
mysiam引擎下,当第一行出错有出错数据,将不执行当前,以及后面的语句
CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `browsertype` enum('ie','firefox','360browser') DEFAULT NULL, `version` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; mysql> set sql_mode="STRICT_TRANS_TABLES";
Query OK, rows affected (0.00 sec) mysql> insert into test(id,browsertype,version) values (null,"tt",1),(null,"firefox",),(null,"ie",);
ERROR (): Data truncated for column 'browsertype' at row
mysql> select * from test;
Empty set (0.00 sec) mysql> set sql_mode="STRICT_ALL_TABLES ";
Query OK, rows affected (0.00 sec) mysql> insert into test(id,browsertype,version) values (null,"tt",1),(null,"firefox",),(null,"ie",);
ERROR (): Data truncated for column 'browsertype' at row
mysql> select * from test;
Empty set (0.00 sec)
mysql> set session sql_mode="TRADITIONAL"
mysql> select @@sql_mode;
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+------------------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.00 sec)
mysql> set session sql_mode="ANSI";
Query OK, rows affected (0.00 sec) mysql> select @@sql_mode;
+-------------------------------------------------------------+
| @@sql_mode |
+-------------------------------------------------------------+
| REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ANSI |
+-------------------------------------------------------------+
row in set (0.00 sec)
mysql> SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
mysql> create table tt (a int auto_increment primary key);
Query OK, rows affected (0.22 sec) mysql> insert into tt values();
Query OK, row affected (0.17 sec) mysql> select * from tt;
+---+
| a |
+---+
| |
+---+
row in set (0.16 sec) mysql> insert into tt values(null);
Query OK, row affected (0.17 sec) mysql> select * from tt;
+---+
| a |
+---+
| |
| |
+---+
rows in set (0.00 sec) mysql> SET SQL_MODE="";
Query OK, rows affected (0.00 sec) mysql> insert into tt values();
Query OK, row affected (0.16 sec) mysql> select * from tt;
+---+
| a |
+---+
| |
| |
| |
+---+
rows in set (0.00 sec)
TRADITIONAL Before MySQL 5.7., and in MySQL 5.7. and later:
TRADITIONAL 等价于 STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION. From MySQL 5.7. though 5.7.7:
TRADITIONAL 等价于 STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_AUTO_CREATE_USER, and NO_ENGINE_SUBSTITUTION. The NO_ZERO_IN_DATE, NO_ZERO_DATE, and ERROR_FOR_DIVISION_BY_ZERO modes are not named because in those versions their effects are included in the effects of strict SQL mode (STRICT_ALL_TABLES or STRICT_TRANS_TABLES). Thus, the effects of TRADITIONAL are the same in all MySQL 5.7 versions (and the same as in MySQL 5.6). For additional discussion, see SQL Mode Changes in MySQL 5.7.
'5.7.10-log' version:
'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode.
They will be merged with strict mode in a future release.
The Most Important SQL Modes: ansi
STRICT_TRANS_TABLES
TRADITIONAL
Combination SQL Modes:
ANSI DB2 MAXDB MSSQL MYSQL323 MYSQL40
ORACLE POSTGRESQL TRADITIONAL StrictSQL Mode:
STRICT_TRANS_TABLES
STRICT_ALL_TABLES
-- 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 to and the day is in the range from to
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, rows affected (0.01 sec)
mysql> select "id",id from "t" limit ;
+----+----+
| id | id |
+----+----+
| | |
| | |
+----+----+
rows in set (0.00 sec
--关闭 ANSI_QUOTES 表名加双引报错
mysql> SET sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
Query OK, rows affected, warning (0.00 sec)
mysql> select "id",id from "t" limit ;
ERROR (): 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 -- 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 BETWEEN - AND ;
->
mysql> SET sql_mode = 'HIGH_NOT_PRECEDENCE';
mysql> SELECT NOT BETWEEN - AND ;
-> -- 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 into it.
NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 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 -- 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
sql_mode :(STRICT_TRANS_TABLES与STRICT_ALL_TABLES 区别)的更多相关文章
- CDH 6.0.1 集群搭建 「Process」
这次搭建我使用的机器 os 是 Centos7.4 RH 系的下面以流的方式纪录搭建过程以及注意事项 Step1: 配置域名相关,因为只有三台机器组集群,所以直接使用了 hosts 的方法: 修改主机 ...
- MySQL 5.6 以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。
Field 'id' doesn't have a default value问题解决方法 运维的名义关注0人评论3323人阅读2018-01-23 17:37:42 MySQL 5.0 以上版本 ...
- 使用MySQL,SQL_MODE有哪些坑,你知道么?
SQL_MODE是MySQL中的一个系统变量(variable),可由多个MODE组成,每个MODE控制一种行为,如是否允许除数为0,日期中是否允许'0000-00-00'值. 为什么需要关注SQL_ ...
- mysql SQL_MODE设置
1.1. SQL_MODE设置 在生产环境中强烈建议将这个值设置为严格模式,这样有些问题可以在数据库的设计和开发阶段就能实现,而如果在生产环境下运行数据库后发现这类问题,那么修改的代价将变得十分巨 ...
- MySQL::SQL_MODE
SQL_MODE可能是比较容易让开发人员和DBA忽略的一个变量,默认为空.SQL_MODE的设置其实是比较冒险的一种设置,因为在这种设置下可以允许一些非法操作,比如可以将NULL插入NOT NULL的 ...
- MySQL数据类型:SQL_MODE设置不容忽视
[IT168 技术]SQL_MODE可能是比较容易让开发人员和DBA忽略的一个变量,默认为空.SQL_MODE的设置其实是比较冒险的一种设置,因为在这种设置下可以允许一些非法操作,比如可以将NULL插 ...
- 为什么在有的服务器上禅道、蝉知安装会报错? 之理解MySQL的SQL_MODE
最近用蝉知的CMS 建站比较多,感觉蛮顺手的,但在给客户安装的时候却会出现安装报错,其原因也很简单 查看了一下他们的install.sql文件中,有些时间字段的默认值是0000-00-00 00:00 ...
- [Mysql]由Data truncated for column联想到的sql_mode配置
系统日志中出现了 ata truncated for column 'agent' at row 1 mysql出现这个问题的原因,无非就是字符集设置 或者是 字段过长导致的. mysql在初始化的时 ...
- MySQL的SQL_Mode修改小计
问题复现 今天突然发现MySQL服务器升级之后sql_mode变成宽松摸索了,危害如下: 临时解决 set global sql_mode='strict_trans_tables'(阿里服务器默认是 ...
随机推荐
- 关于全局变量和函数,在其他类中调用问题,extern关键字
1个工程下有几个类文件,和1个全局的文件comm.h, comm.h中存放了这几个类同时需要的变量和同时调用的函数. 当时,我只在comm.h中定义: int commData1: vector&l ...
- ruby 中文支持设置
学习Ruby的过程中,对于于涉及中文的的代码的时候,需要添加如下代码在首行 # encoding: utf-8 或者EMAC写法 # -*- coding : utf-8 -*- 因为Ruby编译器会 ...
- ”ENV_IS_EMBEDDED“解惑以及相关的移植实验
一.概述( ENV_IS_EMBEDDED的目的) 经典资料 认识 ENV_IS_EMBEDDED只有在CFG_ENV_IS_IN_FLASH或者CFG_ENV_IS_IN_NAND定义了才有 ...
- ASP.NET MVC轻教程 Step By Step 13——页面布局
一般在一个网站中页面会使用相同的结构和元素,如果每个页面都要重复添加这些元素,不仅繁琐更会给我们后期维护带来大麻烦.所以我们采用网页模板之类的技术,将固定不变的元素放入模板,同时留下一些占位符供页面各 ...
- js防止表单重复提交的两种方法
第一种:用flag标识,下面的代码设置checkSubmitFlg标志: 复制代码代码如下: <script language=""javascript"" ...
- [BZOJ 3052] [wc2013] 糖果公园 【树上莫队】
题目链接:BZOJ - 3052 题目分析 这道题就是非常经典的树上莫队了,并且是带修改的莫队. 带修改的莫队:将询问按照 左端点所在的块编号为第一关键字,右端点所在的块为第二关键字,位于第几次修改之 ...
- 安卓天天练练(五)CompoundButton
ToggleButton 让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的.. 注意: 如果LinearLayout,与RelativeLayout不同,必须有orientation ...
- Ultraedit中使用Astyle格式化代码
方法: 使用UE的自定义工具栏并借助开源工具astyle.exe来完成. 1. 首先下载最新的astyle,因为ue自带的astyle版本太老,不支持空格.中文名等. http://astyle.so ...
- Qt4.8 移植(超详细Configure的参数)
Qt4.8.6 configure 参数 不只是适用于Qt4.8.6,原则上适用于Qt4所有版本 Usage: configure [-h] [-prefix <dir>] [-prefi ...
- Android SwitchCompat 自定义颜色及使用
在Android 5.0 中 Switch 更新了样式 变得 比较好用了 但是在5.0 以下的版本 还是老样子 不实用 因此 就有了 SwitchCompat 来兼容 它是v7 包中的 因此可兼容到 ...