关于ORACLE特殊的数据表DUAL
现在工作项目中有使用到一个数据表DUAL,后来就专门搜了一下关于这个表的信息,果然发现它有许多的特殊性。
先看下官方解释:
DUAL is a table automatically created by Oracle Database along with the data dictionary. DUAL is in the schema of the user SYS but is accessible by the name DUAL to all users. It has one column, DUMMY, defined to be VARCHAR2(1), and contains one row with a value X. Selecting from the DUAL table is useful for computing a constant expression with the SELECT statement. Because DUAL has 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是一个虚拟表,用来构成select语法规则,oracle保证DUAL里面永远只有一条数据,字段名 DUMMY,字段值 X;当我们增删改这个表数据时,再次查询显示数据仍是原来那一条信息,原因是ORACLE对DUAL做了内容处理,保证DUAL表只返回一条记录。
就像官方解释所说,我们可以用它来查询一些常量信息,如:
–获取当前系统时间
select sysdate from dual;
–获得一个随机数
select dbms_random.random from dual;
–获取当前用户
select user from dual;
–作为计算器
select 7*8 from dual;
其他还有许多信息可以查询,那么DUAL属于什么类型的对象呢?使用以下语句:
select owner, object_name , object_type from dba_objects where object_name = ‘DUAL’;
能够看到它是属于sys用户的一个表,然后以PUBLIC / SYNONYM (同义词)的方式供其他用户使用。
然后我找到用户sys下的数据表,展开后果然找到DUAL表,并且能够查看该表的定义和数据。
在我们项目组中,我们在项目配置文件中使用select * from dual; 作为启动服务时测试数据库是否正常连接的标志。