MySQL运维实战(4.7) SQL_MODE之ANSI_QUOTES
默认情况下,mysql使用反引号(`)作为标识符的引号。使用mysql关键字作为表名、字段名会报语法错误,这时可以加上反引号( `),避免报错。
设置ANSI_QUOTES后,使用双引号(")作为标识符的引号。
不设置ANSI_QUOTES时,mysql可以使用双引号来引用字符串常量。而设置ANSI_QUOTES后,双引号内的内容不再是字符串,而是mysql的标识符(如表名、字段名)
mysql> set sql_mode = ''; Query OK, 0 rows affected (0.00 sec) -- order是关键字,不能作为表名 mysql> create table order(a varchar(100)); 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(a int)' at line 1 mysql> create table "order"(a varchar(100)); 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"(a int)' at line 1 -- 使用反引号(``), mysql> create table `order`(a varchar(100)); Query OK, 0 rows affected (0.02 sec) mysql> insert into `order` values("abc"); Query OK, 1 row affected (0.00 sec) mysql> select * from `order`; +------+ | a | +------+ | abc | 、+------+ 1 rows in set (0.00 sec)
设置ANSI_QUOTES
mysql> drop table `order`; Query OK, 0 rows affected (0.00 sec) -- "设置ANSI_QUOTES mysql> set sql_mode='ANSI_QUOTES'; Query OK, 0 rows affected (0.00 sec) -- 使用双引号 mysql> create table "order"(a varchar(100)); Query OK, 0 rows affected (0.02 sec) -- "abc" 不是合法的字符串表达式, 而是字段abc mysql> insert into "order" values ( "abc"); ERROR 1054 (42S22): Unknown column 'abc' in 'field list' -- 这里插入的"a"不是字符串a,而是字段a mysql> insert into "order" values ( "a"); Query OK, 1 row affected (0.01 sec) mysql> select * from "order"; +------+ | a | +------+ | NULL | +------+