首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >MySQL里面的dual

MySQL里面的dual

作者头像
jeanron100
发布2018-12-19 11:21:01
发布2018-12-19 11:21:01
7.6K1
举报

这是学习笔记的第 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是一种什么态度呢。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档