sql_mode 之 ignore_space

用于忽略mysql系统函数名与以后的括号之间的空格、mysql

仍是给个形像的说明吧如:count   (*) 经过设置ignore_space 这个sql_mode 就能够把空格给忽略变成count(*)sql

 

一、先从一个普通的例子开始讲起编程

create table t(id int not null primary key auto_increment, x int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t(x) values(1),(2),(3),(4),(5),(6);
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select count(*) from t; -- 查看t表中有多少行数据
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

  上面这个例子仍是比较“中规中矩”、可是生活中又老是有一些人“放荡不羁有自由”;好比他们要建一张表、表名就叫count!session

 

二、创建一张名叫count的表(1)函数

create table count(x 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 'count(x int)' at line 1

  看到这个语法错误你可能会想到count是关键字、是否是要明确的标记出来呢? 因而spa

 

三、创建一张名叫count的表(2)code

create table `count`(x int);
Query OK, 0 rows affected (0.02 sec)

  惊不惊喜? 我想答案必定的否认的、由于咱们今天主讲的是ignore_space这个sql_mode 然而它到这里了尚未登场! 就已经有了一种快完了的感受。server

 

四、创建一张名叫count的表(3)blog

create table count (x int);
Query OK, 0 rows affected (0.02 sec)

 

五、而2,3,4咱们知道表有两种建法、一是加反引号的 二是加空格的;可是对于函数count的调用也能够有两种写法吗?rem

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兼容函数的两种写法呢?

  有它就是咱们今天的主角igonre_space 这个SQL_MODE

 

六、经过sql_mode来兼容两种count函数的写法

select @@sql_mode;
+---------------------------------------------------------+
| @@sql_mode                                              |
+---------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION |
+---------------------------------------------------------+
1 row in set (0.00 sec)

mysql> set @@session.sql_mode=concat(@@sql_mode,',IGNORE_SPACE');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@sql_mode;
+----------------------------------------------------------------------+
| @@sql_mode                                                           |
+----------------------------------------------------------------------+
| IGNORE_SPACE,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select count (*) from t;
+-----------+
| count (*) |
+-----------+
|         6 |
+-----------+
1 row in set (0.00 sec)

mysql> select count(*) from t;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

 

sql_mode加上ignore_space 有什么不足的地方?

  一、因为它直接忽略了空格、因此就形成了有的语法就不起做用了、如:create table count (x int);

  二、出现这种需求时多半是很差的编程习惯引发的、如刚才的例子当中、竟然给表起了一个叫“count”的名字。 

 

 

 

----

相关文章
相关标签/搜索