The dual
table "works" almost just the way any other table works: it is a table from which you can select records.
This means, for example, you can describe the table. Here, in SQL*Plus
:
SQL> set lines 50
SQL> desc dual
Name Null? Typ
----------------------- -------- ----------------
DUMMY VARCHAR2(1)
So, the table has one column, named dummy
which is a varchar2(1)
.
The table has, by design, one record (at least if nobody fiddled with it):
SQL> select count(*) from dual;
COUNT(*)
----------
1
So, in order to get the same behaviour with dual2
as you have with dual
, you have to insert one record into dual. Better yet, create it with a create table as select
(ctas):
SQL> create table dual2 as select * from dual;
Now, your query works:
SQL> select 4*5 from dual2;
4*5
----------
20
Earlier, I said that dual almost works like any other table. So, when does it not work like any other table?
It behaves differently, if no value from the table itself is selected. Again, with your queries, I let Oracle explain them ...
SQL> set lines 150
SQL> explain plan for select 4*5 from dual2;
EXPLAIN PLAN ausgef³hrt.
... in order to see how the table is accessed:
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
Plan hash value: 3445655939
-------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| DUAL2 | 1 | 3 (0)| 00:00:01 |
-------------------------------------------------------------------
It can be seen that the statement does a full table access
on dual2
.
Now, same thing with dual
:
SQL> explain plan for select 4*5 from dual;
EXPLAIN PLAN ausgef³hrt.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------
Plan hash value: 1388734953
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------
This is where the dual
table behaves differently: the value of dummy
is not needed, so a fast dual
operation is executed, in order for the instance not to read the actual value on the disk.