MySQL里面的dual
这是学习笔记的第 1814篇文章
MySQL里面的dual算是一个特殊的存在,如果接触过Oracle的同学会很熟悉。因为语法习惯和完整性的原因会更倾向于使用这种方式。
和Oracle大不同的是,MySQL里面的这个dual是一种更虚的表,在数据字典中无法查证。
mysql> select *from tables where table_name='DUAL';
Empty set (0.00 sec)
mysql> select *from tables where table_name='dual';
Empty set (0.00 sec)
而且不提供对外访问的数据接口。
mysql> desc dual;
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 'dual' at line 1
mysql> select *from dual;
ERROR 1096 (HY000): No tables used
mysql>
在MySQL代码中有着这样的注释,可以看到是为了兼容Oracle的语法,最后饶有兴趣的用了一个嗯来默认。
| FROM DUAL_SYM where_clause opt_limit_clause
/* oracle compatibility: oracle always requires FROM clause,
and DUAL is system table without fields.
Is "SELECT 1 FROM DUAL" any better than "SELECT 1" ?
Hmmm :) */
除此之外dual对于我们还有什么特别之处。
dual在MySQL里面是保留字,所以我们要建一个表是dual也是不行的。
mysql> create table dual(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 'dual(id int)' at line 1
mysql>
mysql> create table duals1(id int);
Query OK, 0 rows affected (0.14 sec)
在代码中还看到了这样一个针对dual的定制逻辑,对于select xx from where 2的方式,MySQL专门做了定制处理。
mysql> SELECT 1 FROM DUAL WHERE 2;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
所以在大家的使用中,对于dual是一种什么态度呢。