select 1 from dual
begin 2018年7月14日15:06:29
select 1 from dual
Oracle下的select 1 from dual
今天在看公司代码的时候,发现有这一句SQL:
select 1 from dual
然后觉得有点奇怪,数据库里面都没有创建这个dual表,这个表是从何而来呢?然后Google了一波,理解了一下。
首先,公司用的是Oracle数据库,关于Oracle数据库中的dual表,官方文档说明(The DUAL Table):
DUALis a small table in the data dictionary that Oracle Database and user-written programs can reference to guarantee a known result. The dual table is useful when a value must be returned only once, for example, the current date and time. All database users have access toDUAL.
The
DUALtable has one column calledDUMMYand one row containing the valueX.
DUAL是一个在数据字典里的很小的表,Oracle数据库和用户写的程序可以引用它来保证一个已知的结果。当一个值(比如当前date和time)有且仅需返回一次的时候,这个dual表还是很管用的。所有数据库用户都可以访问DUAL。
DUAL表有一列,名叫DUMMY和有一行,值为X。
DUALis a table automatically created by Oracle Database along with the data dictionary.DUALis in the schema of the userSYSbut is accessible by the nameDUALto all users. It has one column, DUMMY, defined to be VARCHAR2(1), and contains one row with a value X.
Selecting from the
DUALtable is useful for computing a constant expression with theSELECTstatement. BecauseDUALhas only one row, the constant is returned only once. Alternatively, you can select a constant, pseudocolumn, or expression from any table, but the value will be returned as many times as there are rows in the table.
DUAL是一个随着Oracle数据库创建数据字典时自动创建的表。虽然DUAL在用户SYS模式下,但是还是可以被所有用户访问的。它有一列,DUMMY,定义为VARCHAR2(1),和包含一行数据,值为X。
对于用SELECT计算一个常量表达式来说,从DUAL选择是比较好用的。因为DUAL只有一行,所以常量只会返回一次。或者,你可以从任意一个表中选择常量、伪列和表达式,但是这个值将返回多次,次数和表的行数一样多。
我们可以在Oracle数据库查询:
SQL> select * from dual;
DUMMY
-----
X
好的,现在我们知道了dual这个表是长什么样了,也知道为什么会用这个表了。划重点:当一个值必须返回,且只返回一次,可以从dual表选择返回。
我看了一下项目代码,这句SQL是传给数据库连接池验证连接的,这样就很合理了:不需要返回太多的值,但是有必须有返回,选择从dual返回再正确不过了。
MySQL下的select 1 from dual
SELECTcan also be used to retrieve rows computed without reference to any table.
SELECT也可以在没有引用任何表,用来检索行。
For example:
比如:
mysql> SELECT 1 + 1;
-> 2
You are permitted to specify
DUALas a dummy table name in situations where no tables are referenced:
在没有引用表的情况下,你可以指定DUAL为一个虚拟表名。
mysql> SELECT 1 + 1 FROM DUAL;
-> 2
DUALis purely for the convenience of people who require that allSELECTstatements should haveFROMand possibly other clauses. MySQL may ignore the clauses. MySQL does not requireFROM DUALif no tables are referenced.
DUAL单纯是为那些要求所有SELECT语句应该有FROM或者其他子句的人们提供便利。MySQL可能忽略这个子句。即使没有表引用,MySQL也不要求FROM DUAL。
在MySQL中使用dual表并不总是对的:
mysql> select 1 from dual;
3013 - Unknown table ****.dual
其实MySQL就直接SELECT就行。
end 2018年7月14日17:36:24
select 1 from dual的更多相关文章
- select 1 from dual 中的1表示的含义
select 1 from dual 在这条sql语句中的1代表什么意思?查出来是个什么结果? 其实: select 1 from table; select anycol(目的表集合中的任意 ...
- Oacle 插入多条记录的语法理解;INSERT ALL INTO ....... SELECT 1 FROM dual;
单表Insert into的语句就不说了,这里主要说插入多条语句的说明 For a multitable insert operation, each expression in the values ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...
- ORACLE的DUAL表及DB2的SYSIBM.SYSDUMMY1
在ORACLE中,数据库用户通过访问DUAL表获取一些系统信息:DB2中,相应的表为SYSIBM.SYSDUMMY1表 ORACE中举例: SELECT SYSDATE FROM DUAL;获取当前日 ...
- oracle中dual的使用
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情,如下:1.查看当前用户,可以在 SQL Plus中执行下面语句select ...
- ORACLE中伪表 dual 的用法
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情,如下: 1.查看当前用户,可以在 SQL Plus中执行下面语句 sel ...
- oracle 中的dual表简介与用法
Dual表是每个数据库创建时默认生成的,该表仅有一列一行. 1)分析dual表执行,如下:
- 【转】详解Oracle的dual表
网址:http://www.adp-gmbh.ch/ora/misc/dual.html dual is a table which is created by oracle along with t ...
- oracle中dual表的使用
dual表是一个虚拟表,用来和select语句一起使用.1.查看当前用户select user from dual2.用来调用系统函数select to_char(sysdate,'yyyy-mm- ...
随机推荐
- Netty URL路由方案探讨
最近在用Netty做开发,需要提供一个http web server,供调用方调用.采用Netty本身提供的HttpServerCodec handler进行Http协议的解析,但是需要自己提供路由. ...
- [转载]strtok函数和strtok_r函数
1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{ char name[25]; char sex[1 ...
- js延迟几秒执行
var t=setTimeout("location.reload()",5000);
- 冲量:momentum
参见:http://www.jianshu.com/p/58b3fe300ecb,这个博客里有冲量的python实现的代码和讲解 “冲量”这个概念源自于物理中的力学,表示力对时间的积累效应. 在普通的 ...
- bash脚本里su命令执行
俩种方法 1.可以使用 <<EOF 参数实现. 脚本内容:cat test.sh代码如下: #!/bin/bashsu - test <<EOFpwd;exit;EOF 2.当 ...
- Task多线程进行多进程
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- C# 各种类型的转换
/// <summary> /// 一些常用的方法 /// 1.一些高效的转换方法 /// </summary> public class Util { #region Obj ...
- pre,html转义,abbr缩写,表格table
<pre></pre>预定义文本标签pre(保留换行和空格) <sdds>对html转义 <abbr title="sddsdsds"&g ...
- static, const 和 static const 变量的初始化问题
const 常量的在超出其作用域的时候会被释放,但是 static 静态变量在其作用域之外并没有释放,只是不能访问. static 修饰的是静态变量,静态函数.对于类来说,静态成员和静态函数是属于整个 ...
- sicily 1459. The Dragon of Loowater
Time Limit: 1sec Memory Limit:32MB Description Once upon a time, in the Kingdom of Loowa ...