oracle中dual表相关的知识

本文介绍了Oracle数据库中DUAL表的基本概念、结构及其特殊用途。DUAL表由Oracle自动创建,只有一个VARCHAR2类型的DUMMY列,用于返回常量表达式。文章还探讨了DUAL表的一行数据特性及其实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、  根据查看DUAL是什么:

SELECT T.OWNER,T.OBJECT_NAME,T.OBJECT_TYPE,T.STATUS

FROM DBA_OBJECTS T

WHERE T.OBJECT_NAME LIKE '%DUAL%'

 

     OWNER  OBJECT_NAME    OBJECT_TYPE     STATUS

1     SYS DUAL     TABLE    VALID

2     PUBLIC  DUAL     SYNONYM    VALID

可以看出存在sysdual表,给它建的公共同义词名为dual

 

2、《SQL Reference》上的解释

DUAL is a table automatically created by Oracle 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.

 

2、  结构:

SQL> desc dual

Name  Type        Nullable Default Comments

----- ----------- -------- ------- --------

DUMMY VARCHAR2(1) Y                        

 

SQL> select sysdate from dual;

 

SYSDATE

-----------

2009-1-8 10

      

4、一个有趣的测试:

查询:

SQL> SELECT * FROM DUAL;

 

DUMMY

-----

X

1

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

删除:

SQL> delete from dual;

 

1 row deleted

       查询:

SQL> select * from dual;

 

DUMMY

-----

1

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

 

5ORACLE关于DUAL表不同寻常特性的解释

There is internalized code that makes this happen. Code checks that ensure that a table scan of SYS.DUAL only returns one row. Svrmgrl behaviour is incorrect but this is now an obsolete product.

The base issue you should always remember and keep is: DUAL table should always have 1 ROW. Dual is a normal table with one dummy column of varchar2(1). This is basically used from several applications as a pseudo table for getting results from a select statement that use functions like sysdate or other prebuilt or application functions. If DUAL has no rows at all some applications (that use DUAL) may fail with NO_DATA_FOUND exception. If DUAL has more than 1 row then applications (that use DUAL) may fail with TOO_MANY_ROWS exception.

So DUAL should ALWAYS have 1 and only 1 row

 

6.在删除时使用编辑数据才能保证查到最终的数据。

       使用deleteselect for update只能锁定到X行,其余的行无法查询到

 

7、用来测试应用服务器是否与数据库保持连接:

Dual has another use when you try to test if you are connected with the DB remotely by JDBC in your AS such as Weblogic. This is a common table for any schema.

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值