MySQL运维实战(4.6) SQL_MODE之NO_BACKSLASH_ESCAPES
设置NO_BACKSLASH_ESCAPES后,反斜杠(\)不作为转义符。
不设置NO_BACKSLASH_ESCAPES时,反斜杠(\)用来转义后一个字符
mysql> create table t_escape(id int, a varchar(100)); Query OK, 0 rows affected (0.01 sec) -- 默认情况,反斜杠是转义符,会改变后一个字符的含义。 mysql> set sql_mode=''; Query OK, 0 rows affected (0.00 sec) mysql> insert into t_escape values(1, 'a\'b'); Query OK, 1 row affected (0.00 sec) mysql> select * from t_escape; +------+------+ | id | a | +------+------+ | 1 | a'b | | 2 | a\b | +------+------+ 2 rows in set (0.00 sec)
设置NO_BACKSLASH_ESCAPES:
-- 这个SQL中单引号不匹配,报语法错误 mysql> insert into t_escape values(3, 'a\'b'); '> '; 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 'b'); '' at line 1 -- 2个反斜杠都写入到表中 mysql> insert into t_escape values(4, 'a\\b'); Query OK, 1 row affected (0.01 sec) -- 单引号不能用反斜杠转义,使用单引号转义单引号(使用2个单引号代表一个单引号)。 mysql> insert into t_escape values(5, 'a''b'); Query OK, 1 row affected (0.01 sec) mysql> select * from t_escape; +------+------+ | id | a | +------+------+ | 1 | a'b | | 2 | a\b | | 4 | a\\b | | 5 | a'b | +------+------+ 4 rows in set (0.00 sec)