SQL_MODE是MySQL中的一個系統變量(variable),可由多個MODE組成,每一個MODE控制一種行爲,如是否容許除數爲0,日期中是否容許'0000-00-00'值。html
爲何須要關注SQL_MODE呢?mysql
首先,看三個簡單的Demo(MySQL 5.6)。sql
1.數據庫
mysql> create table t1(c1 datetime); Query OK, 0 rows affected (0.16 sec) mysql> insert into t1 values('2019-02-29'); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from t1; +---------------------+ | c1 | +---------------------+ | 0000-00-00 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
實際存儲值與插入值不符。session
2. less
mysql> create table t2(c1 varchar(10)); Query OK, 0 rows affected (0.06 sec) mysql> insert into t2 values('a'),('b'),('c'); Query OK, 3 rows affected (0.01 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from t2; +------+ | c1 | +------+ | a | | b | | c | +------+ 3 rows in set (0.00 sec) mysql> alter table t2 modify column c1 int; Query OK, 3 rows affected, 3 warnings (0.05 sec) Records: 3 Duplicates: 0 Warnings: 3 mysql> show warnings; +---------+------+-------------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------------+ | Warning | 1366 | Incorrect integer value: 'a' for column 'c1' at row 1 | | Warning | 1366 | Incorrect integer value: 'b' for column 'c1' at row 2 | | Warning | 1366 | Incorrect integer value: 'c' for column 'c1' at row 3 | +---------+------+-------------------------------------------------------+ 3 rows in set (0.00 sec) mysql> select * from t2; +------+ | c1 | +------+ | 0 | | 0 | | 0 | +------+ 3 rows in set (0.00 sec)
DDL致使原列內容丟失。ide
3. 函數
mysql> create table t3(id int not null,c1 varchar(10)); Query OK, 0 rows affected (0.05 sec) mysql> insert into t3 values(null,'a'); ERROR 1048 (23000): Column 'id' cannot be null mysql> insert into t3(c1) values('a'); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> show warnings; +---------+------+-----------------------------------------+ | Level | Code | Message | +---------+------+-----------------------------------------+ | Warning | 1364 | Field 'id' doesn't have a default value | +---------+------+-----------------------------------------+ 1 row in set (0.00 sec) mysql> select * from t3; +----+------+ | id | c1 | +----+------+ | 0 | a | +----+------+ 1 row in set (0.00 sec)
顯式指定列和不顯式指定的處理邏輯居然不同。測試
爲何會這樣呢?這個即與SQL_MODE有關。this
在MySQL 5.6中, SQL_MODE的默認值爲"NO_ENGINE_SUBSTITUTION",非嚴格模式。
在這種模式下,在進行數據變動操做時,若是涉及的列中存在無效值(如日期不存在,數據類型不對,數據溢出),只會提示"Warning",並不會報錯。
若是要規避上述問題,需開啓SQL_MODE的嚴格模式。
所謂的嚴格模式,即SQL_MODE中開啓了STRICT_ALL_TABLES或STRICT_TRANS_TAB LES。
仍是上面的Demo,看看嚴格模式下,MySQL的處理邏輯。
mysql> set session sql_mode='STRICT_TRANS_TABLES'; Query OK, 0 rows affected (0.00 sec) mysql> insert into t1 values('2019-02-29'); ERROR 1292 (22007): Incorrect datetime value: '2019-02-29' for column 'c1' at row 1 mysql> alter table t2 modify column c1 int; ERROR 1366 (HY000): Incorrect integer value: 'a' for column 'c1' at row 1 mysql> insert into t3(c1) values('a'); ERROR 1364 (HY000): Field 'id' doesn't have a default value
一樣的SQL,在嚴格模式下,直接提示"ERROR",而不是"Warning"。
同是嚴格模式,下面看看STRICT_ALL_TABLES或STRICT_TRAN S_TABLES的區別。
STRICT_TRANS_TABLES只對事務表開啓嚴格模式,STRICT_ALL_TABLES是對全部表開啓嚴格模式,不只僅是事務表,還包括非事務表。
看下面這個測試。
對myisam表插入3條數據,其中,第3條數據是空字符串,與定義的int類型不匹配。
mysql> create table t (c1 int) engine=myisam; Query OK, 0 rows affected (0.00 sec) mysql> set session sql_mode='STRICT_TRANS_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values (1),(2),(''); Query OK, 3 rows affected, 1 warning (0.00 sec) Records: 3 Duplicates: 0 Warnings: 1 mysql> show warnings; +---------+------+------------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------------+ | Warning | 1366 | Incorrect integer value: '' for column 'c1' at row 3 | +---------+------+------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select * from t; +------+ | c1 | +------+ | 1 | | 2 | | 0 | +------+ 3 rows in set (0.00 sec) mysql> set session sql_mode='STRICT_ALL_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values (1),(2),(''); ERROR 1366 (HY000): Incorrect integer value: '' for column 'c1' at row 3
能夠看到,在表爲myisam存儲引擎的狀況下,只有開啓STRICT_ALL_TABLES纔會報錯。
不一樣版本默認的SQL_MODE
MySQL 5.5:空
MySQL 5.6:NO_ENGINE_SUBSTITUTION
MySQL 5.7: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
MySQL 8.0:ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION
SQL_MODE既可在全局級別修改,又可在會話級別修改。可指定多個MODE,MODE之間用逗號隔開。
全局級別
set global sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES';
會話級別
set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES';
在嚴格模式下,對於日期的檢測較爲嚴格,其必須有效。若開啓該MODE,對於month和day的檢測會相對寬鬆。其中,month只需在1~12之間,day只需在1~31之間,而無論其是否有效,以下面的'2004-02-31'。
mysql> create table t (c1 datetime); Query OK, 0 rows affected (0.21 sec) mysql> set session sql_mode='STRICT_TRANS_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values('2004-02-31'); ERROR 1292 (22007): Incorrect datetime value: '2004-02-31' for column 'c1' at row 1 mysql> set session sql_mode='STRICT_TRANS_TABLES,ALLOW_INVALID_DATES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values('2004-02-31'); Query OK, 1 row affected (0.01 sec) mysql> select * from t; +---------------------+ | c1 | +---------------------+ | 2004-02-31 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
注意,該MODE只適用於DATE和DATETIME,不適用於TIMESTAMP。
在MySQL中,對於關鍵字和保留字,是不容許用作表名和字段名的。若是必定要使用,必須使用反引號("`")進行轉義。
mysql> create table order (id int); 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 'order (id int)' at line 1 mysql> create table `order` (id int); Query OK, 0 rows affected (0.12 sec)
若開啓該MODE,則雙引號,同反引號同樣,可對關鍵字和保留字轉義。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> create table "order" (c1 int); 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 '"order" (c1 int)' at line 1 mysql> set session sql_mode='ANSI_QUOTES'; Query OK, 0 rows affected (0.00 sec) mysql> create table "order" (c1 int); Query OK, 0 rows affected (0.17 sec)
須要注意的是,在開啓該MODE的狀況下,不能再用雙引號來引字符串。
該MODE決定除數爲0的處理邏輯,實際效果還取決因而否開啓嚴格模式。
1. 開啓嚴格模式,且開啓該MODE,插入1/0,會直接報錯。
mysql> create table t (c1 double); Query OK, 0 rows affected (0.04 sec) mysql> set session sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values(1/0); ERROR 1365 (22012): Division by 0
2. 只開啓嚴格模式,不開啓該MODE,容許1/0的插入,且不提示warning,1/0最後會轉化爲NULL。
mysql> set session sql_mode='STRICT_TRANS_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values(1/0); Query OK, 1 row affected (0.07 sec) mysql> select * from t; +------+ | c1 | +------+ | NULL | +------+ 1 row in set (0.00 sec)
3. 不開啓嚴格模式,只開啓該MODE,容許1/0的插入,但提示warning。
4. 不開啓嚴格模式,也不開啓該MODE,容許1/0的插入,且不提示warning,同2同樣。
默認狀況下,NOT的優先級低於比較運算符。但在某些低版本中,NOT的優先級高於比較運算符。
看看二者的區別。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select not 1 < -1; +------------+ | not 1 < -1 | +------------+ | 1 | +------------+ 1 row in set (0.00 sec) mysql> set session sql_mode='HIGH_NOT_PRECEDENCE'; Query OK, 0 rows affected (0.00 sec) mysql> select not 1 < -1; +------------+ | not 1 < -1 | +------------+ | 0 | +------------+ 1 row in set (0.00 sec)
在sql_mode爲空的狀況下, not 1 < -1至關於not (1 < -1),若是設置了'HIGH_ NOT_PRECEDENCE',則至關於(not 1) < -1。
默認狀況下,函數名和左括號(「(」)之間不容許存在空格。若開啓該MODE,則容許。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select count(*) from t; +----------+ | count(*) | +----------+ | 2 | +----------+ 1 row in set (0.00 sec) mysql> select count (*) from t; 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 '*) from t' at line 1 mysql> set session sql_mode='IGNORE_SPACE'; Query OK, 0 rows affected (0.01 sec) mysql> select count (*) from t; +-----------+ | count (*) | +-----------+ | 2 | +-----------+ 1 row in set (0.01 sec)
默認狀況下,在對自增主鍵插入NULL或0時,會自動生成下一個值。若開啓該MODE,當插入0時,並不會自動生成下一個值。
若是表中自增主鍵列存在0值,在進行邏輯備份還原時,可能會致使數據不一致。因此mysqldump在生成備份數據以前,會自動開啓該MODE,以免數據不一致的狀況。
mysql> create table t (id int auto_increment primary key); Query OK, 0 rows affected (0.11 sec) mysql> set session sql_mode=''; Query OK, 0 rows affected (0.01 sec) mysql> insert into t values (0); Query OK, 1 row affected (0.04 sec) mysql> select * from t; +----+ | id | +----+ | 1 | +----+ 1 row in set (0.00 sec) mysql> set session sql_mode='NO_AUTO_VALUE_ON_ZERO'; Query OK, 0 rows affected (0.02 sec) mysql> insert into t values (0); Query OK, 1 row affected (0.09 sec) mysql> select * from t; +----+ | id | +----+ | 0 | | 1 | +----+ 2 rows in set (0.00 sec)
默認狀況下,反斜槓「\」會做爲轉義符,若開啓該MODE,則反斜槓「\」會做爲一個普通字符,而不是轉義符。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.01 sec) mysql> select '\\t'; +----+ | \t | +----+ | \t | +----+ 1 row in set (0.00 sec) mysql> set session sql_mode='NO_BACKSLASH_ESCAPES'; Query OK, 0 rows affected (0.00 sec) mysql> select '\\t'; +-----+ | \\t | +-----+ | \\t | +-----+ 1 row in set (0.00 sec)
默認狀況下,在建立表時,能夠指定數據目錄(DATA DIRECTORY)和索引目錄(INDEX DIRECTORY),若開啓該MODE,則會忽略這兩個選項。在主從複製場景下,可在從庫上開啓該MODE。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.01 sec) mysql> create table t (id int) data directory '/tmp/'; Query OK, 0 rows affected (0.15 sec) mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci DATA DIRECTORY='/tmp/' 1 row in set (0.00 sec) mysql> set session sql_mode='NO_DIR_IN_CREATE'; Query OK, 0 rows affected (0.00 sec) mysql> drop table t; Query OK, 0 rows affected (0.11 sec) mysql> create table t (id int) data directory '/tmp/'; Query OK, 0 rows affected, 1 warning (0.05 sec) mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec)
在開啓該MODE的狀況下,在建立表時,若是指定的存儲引擎不存在或不支持,則會直接提示「ERROR」。
若不開啓,則只會提示「Warning」,且使用默認的存儲引擎。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> create table t (id int) engine=federated; Query OK, 0 rows affected, 2 warnings (0.11 sec) mysql> show warnings; +---------+------+-------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------+ | Warning | 1286 | Unknown storage engine 'federated' | | Warning | 1266 | Using storage engine InnoDB for table 't' | +---------+------+-------------------------------------------+ 2 rows in set (0.00 sec) mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.01 sec) mysql> drop table t; Query OK, 0 rows affected (0.11 sec) mysql> set session sql_mode='NO_ENGINE_SUBSTITUTION'; Query OK, 0 rows affected (0.00 sec) mysql> create table t (id int) engine=federated; ERROR 1286 (42000): Unknown storage engine 'federated'
兩個整數相減,若是其中一個數是無符號位,默認狀況下,會產生一個無符號位的值,若是該值爲負數,則會提示「ERROR」。如,
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select cast(0 as unsigned)-1; ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
若開啓該MODE,則容許結果爲負數。
mysql> set session sql_mode='NO_UNSIGNED_SUBTRACTION'; Query OK, 0 rows affected (0.00 sec) mysql> select cast(0 as unsigned)-1; +-----------------------+ | cast(0 as unsigned)-1 | +-----------------------+ | -1 | +-----------------------+ 1 row in set (0.00 sec)
該MODE會影響'0000-00-00'的插入。實際效果還取決因而否開啓嚴格模式。
1. 在開啓嚴格模式,且同時開啓該MODE,是不容許'0000-00-00'插入的。
mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> show warnings\G *************************** 1. row *************************** Level: Warning Code: 3135 Message: '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.1 row in set (0.00 sec) mysql> insert into t values ('0000-00-00'); ERROR 1292 (22007): Incorrect datetime value: '0000-00-00' for column 'c1' at row 1
2. 只開啓嚴格模式,不開啓該MODE,容許'0000-00-00'值的插入,且不提示warning。
mysql> set session sql_mode='STRICT_TRANS_TABLES'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values ('0000-00-00'); Query OK, 1 row affected (0.04 sec)
3. 不開啓嚴格模式,只開啓該MODE,容許'0000-00-00'值的插入,但提示warning。
mysql> set session sql_mode='NO_ZERO_DATE'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into t values ('0000-00-00'); Query OK, 1 row affected, 1 warning (0.05 sec) mysql> show warnings; +---------+------+---------------------------------------------+ | Level | Code | Message | +---------+------+---------------------------------------------+ | Warning | 1264 | Out of range value for column 'c1' at row 1 | +---------+------+---------------------------------------------+ 1 row in set (0.01 sec)
4. 不開啓嚴格模式,也不開啓該MODE,容許'0000-00-00'值的插入,且不提示warning。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> insert into t values ('0000-00-00'); Query OK, 1 row affected (0.03 sec)
同NO_ZERO_DATE相似,只不過NO_ZERO_DATE針對的是'0000-00-00',而NO_ZERO_IN_DATE針對的是年不爲0,但月或者日爲0的日期,如,'2010-00-01' or '2010-01-00'。
實際效果也是取決因而否開啓嚴格模式,同NO_ZERO_DATE同樣。
開啓該MODE,則SELECT列表中只能出現分組列和聚合函數。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select dept_no,emp_no,min(from_date) from dept_emp group by dept_no; +---------+--------+----------------+ | dept_no | emp_no | min(from_date) | +---------+--------+----------------+ | d001 | 10017 | 1985-01-01 | | d002 | 10042 | 1985-01-01 | | d003 | 10005 | 1985-01-01 | | d004 | 10003 | 1985-01-01 | | d005 | 10001 | 1985-01-01 | | d006 | 10009 | 1985-01-01 | | d007 | 10002 | 1985-01-01 | | d008 | 10007 | 1985-01-01 | | d009 | 10011 | 1985-01-01 | +---------+--------+----------------+ 9 rows in set (0.64 sec) mysql> set session sql_mode='ONLY_FULL_GROUP_BY'; Query OK, 0 rows affected (0.00 sec) mysql> select dept_no,emp_no,min(from_date) from dept_emp group by dept_no; ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'employees.dept_emp.emp_no' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
若是不開啓該MODE,則容許SELECT列表中出現任意列,但這些列的值並非肯定的,官方文檔中也提到了這一點。
If ONLY_FULL_GROUP_BY is disabled, a MySQL extension to the standard SQL use of GROUP BY permits the select list, HAVING condition, or ORDER BY list to refer to nonaggregated columns even if the columns are not functionally dependent on GROUP BY columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which value within each group the server chooses. Disabling ONLY_FULL_GROUP_BY is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the GROUP BY are the same for each group. https://dev.mysql.com/doc/refman/8.0/en/group-by-handling.html
在對CHAR字段進行存儲時,在Compact格式下,會佔用固定長度的字節。
以下面的c1列,定義爲char(10),雖然'ab'只佔用兩個字節,但在Compact格式下,會佔用10個字節,不足部分以空格填充。
在查詢時,默認狀況下,會剔除掉末尾的空格。若開啓該MODE,則不會剔除,每次都會返回固定長度的字符。
mysql> create table t (c1 char(10)); Query OK, 0 rows affected (0.17 sec) mysql> insert into t values('ab'); Query OK, 1 row affected (0.11 sec) mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select c1, hex(c1), char_length(c1) from t; +------+---------+-----------------+ | c1 | hex(c1) | char_length(c1) | +------+---------+-----------------+ | ab | 6162 | 2 | +------+---------+-----------------+ 1 row in set (0.00 sec) mysql> set session sql_mode='PAD_CHAR_TO_FULL_LENGTH'; Query OK, 0 rows affected (0.00 sec) mysql> select c1, hex(c1), char_length(c1) from t; +------------+----------------------+-----------------+ | c1 | hex(c1) | char_length(c1) | +------------+----------------------+-----------------+ | ab | 61622020202020202020 | 10 | +------------+----------------------+-----------------+ 1 row in set (0.00 sec)
在Oracle中,鏈接字符串可用concat和管道符("||"),但concat只能鏈接兩個字符串(MySQL中的concat可鏈接多個字符),侷限性太大,若是要鏈接多個字符串,通常用的是管道符。
開啓該MODE,便可將管道符做爲鏈接符。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> select 'a'||'b'; +----------+ | 'a'||'b' | +----------+ | 0 | +----------+ 1 row in set, 2 warnings (0.00 sec) mysql> select concat('a','b'); +-----------------+ | concat('a','b') | +-----------------+ | ab | +-----------------+ 1 row in set (0.00 sec) mysql> set session sql_mode='PIPES_AS_CONCAT'; Query OK, 0 rows affected (0.00 sec) mysql> select 'a'||'b'; +----------+ | 'a'||'b' | +----------+ | ab | +----------+ 1 row in set (0.00 sec)
在建立表時,數據類型可指定爲real,默認狀況下,其會轉化爲double,若開啓該MODE,則會轉化爲float。
mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> create table t ( c1 real); Query OK, 0 rows affected (0.12 sec) mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `c1` double DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) mysql> drop table t; Query OK, 0 rows affected (0.04 sec) mysql> set session sql_mode='REAL_AS_FLOAT'; Query OK, 0 rows affected (0.00 sec) mysql> create table t ( c1 real); Query OK, 0 rows affected (0.11 sec) mysql> show create table t\G *************************** 1. row *************************** Table: t Create Table: CREATE TABLE `t` ( `c1` float DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec)
對事務表開啓嚴格模式。
對全部表開啓嚴格模式。
在時間類型定義了小數秒的狀況下,若是插入的位數大於指定的位數,默認狀況下,會四捨五入,若開啓了該MODE,則會直接truncate掉。
mysql> create table t (c1 int,c2 datetime(2)); Query OK, 0 rows affected (0.04 sec) mysql> set session sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> insert into t values(1,'2018-08-08 11:12:13.125'); Query OK, 1 row affected (0.06 sec) mysql> select * from t; +------+------------------------+ | c1 | c2 | +------+------------------------+ | 1 | 2018-08-08 11:12:13.13 | +------+------------------------+ 1 row in set (0.00 sec) mysql> set session sql_mode='TIME_TRUNCATE_FRACTIONAL'; Query OK, 0 rows affected (0.00 sec) mysql> insert into t values(2,'2018-08-08 11:12:13.125'); Query OK, 1 row affected (0.06 sec) mysql> select * from t; +------+------------------------+ | c1 | c2 | +------+------------------------+ | 1 | 2018-08-08 11:12:13.13 | | 2 | 2018-08-08 11:12:13.12 | +------+------------------------+ 2 rows in set (0.00 sec)
在MySQL 8.0以前,直接受權會隱式建立用戶。
mysql> select host,user from mysql.user where user='u1'; Empty set (0.00 sec) mysql> grant all on *.* to 'u1'@'%' identified by '123'; Query OK, 0 rows affected, 1 warning (0.12 sec) mysql> show warnings; +---------+------+------------------------------------------------------------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------------------------------------------------------------------------------------------+ | Warning | 1287 | Using GRANT for creating new user is deprecated and will be removed in future release. Create new user with CREATE USER statement. | +---------+------+------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select host,user from mysql.user where user='u1'; +------+------+ | host | user | +------+------+ | % | u1 | +------+------+ 1 row in set (0.00 sec)
一樣的grant語句,在MySQL 8.0中是會報錯的。
mysql> grant all on *.* to 'u1'@'%' identified by '123'; 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 'identified by '123'' at line 1
在MySQL 8.0中,已不容許grant語句隱式建立用戶,因此,該MODE在8.0中也不存在。
從字面上看,該MODE是禁止受權時隱式建立用戶。但在實際測試過程當中,發現其並不能禁止。
mysql> set session sql_mode='NO_AUTO_CREATE_USER'; Query OK, 0 rows affected (0.03 sec) mysql> grant all on *.* to 'u1'@'%' identified by '123'; Query OK, 0 rows affected, 1 warning (0.00 sec)
其實,該MODE禁止的只是不帶「identified by」子句的grant語句,對於帶有「identified by」子句的grant語句,其並不會禁止。
mysql> drop user u1; Query OK, 0 rows affected (0.00 sec) mysql> set session sql_mode='NO_AUTO_CREATE_USER'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'u1'@'%'; ERROR 1133 (42000): Can't find any matching row in the user table mysql> set session sql_mode=''; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'u1'@'%'; Query OK, 0 rows affected, 1 warning (0.00 sec)
在MySQL 5.7中,還可將SQL_MODE設置爲ANSI, DB2, MAXDB, MSSQL, MYSQL323, MYSQL40, ORACLE, POSTGRESQL, TRADITIONAL。
其實,這些MODE只是上述MODE的一種組合,目的是爲了和其它數據庫兼容。
在MySQL 8.0中,只支持ANSI和TRADITIONAL這兩種組合。
等同於REAL_AS_FLOAT, PIPES_AS_CONCAT, ANSI_QUOTES, IGNORE_SPACE, ONLY_FULL_GROUP_BY。
mysql> set session sql_mode='ANSI'; Query OK, 0 rows affected (0.00 sec) mysql> show session variables like 'sql_mode'; +---------------+--------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+--------------------------------------------------------------------------------+ | sql_mode | REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI | +---------------+--------------------------------------------------------------------------------+ 1 row in set (0.03 sec)
等同於STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO, NO_ENGINE_SUBSTITUTION。
mysql> set session sql_mode='TRADITIONAL'; Query OK, 0 rows affected (0.00 sec) mysql> show session variables like 'sql_mode'; +---------------+----------------------------------------------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+----------------------------------------------------------------------------------------------------------------------------------+ | sql_mode | STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_ENGINE_SUBSTITUTION | +---------------+----------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec)
1. SQL_MODE在非嚴格模式下,會出現不少意料不到的結果。建議線上開啓嚴格模式。但對於線上老的環境,若是一開始就運行在非嚴格模式下,切忌直接調整,畢竟二者的差別性仍是至關巨大。
2. 官方默認的SQL_MODE一直在發生變化,MySQL 5.5, 5.6, 5.7就不盡相同,但整體是趨嚴的,在對數據庫進行升級時,其必須考慮默認的SQL_MODE是否須要調整。
3. 在進行數據庫遷移時,可經過調整SQL_MODE來兼容其它數據庫的語法。