码迷,mamicode.com
首页 > 数据库 > 详细

sql_mode 之 ignore_space

时间:2017-05-23 11:22:51      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:今天   key   prim   span   div   row   class   mode   错误   

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

还是给个形像的说明吧如:count   (*) 通过设置ignore_space 这个sql_mode 就可以把空格给忽略变成count(*)

 

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

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!

 

2、建立一张名叫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是关键字、是不是要明确的标记出来呢? 于是

 

3、建立一张名叫count的表(2)

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

  惊不惊喜? 我想答案一定的否定的、因为我们今天主讲的是ignore_space这个sql_mode 然而它到这里了还没有登场! 就已经有了一种快完了的感觉。

 

4、建立一张名叫count的表(3)

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

 

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

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

 

6、通过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 有什么不足的地方?

  1、由于它直接忽略了空格、所以就造成了有的语法就不起作用了、如:create table count (x int);

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

 

 

 

----

sql_mode 之 ignore_space

标签:今天   key   prim   span   div   row   class   mode   错误   

原文地址:http://www.cnblogs.com/JiangLe/p/6892831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!