What is Dual Table ?
Dual
is a table which is created by oracle along with the data dictionary.
It consists of exactly one column whose name is dummy and one record.
The value of that record is X.
The reason to use the DUAL table isn't just portability, but
optimization. The owner of dual is SYS but dual can be accessed by
every user.Dual is useful because it always exists, and has a single
row, which is handy for select statements with constant expressions.
Example:
SQL> select 1+1 from dual ;
1+1
----------
2
SQL> select sysdate from dual;
SYSDATE
---------
3-MAR-11
DUAL
was originally a table and the database engine would perform disk IO on
the table when selecting from DUAL. This disk IO was usually logical IO
(not involving physical disk access) as the disk blocks were usually
already cached in memory. This resulted in a large amount of logical IO
against the DUAL table.Later
versions of the Oracle database have been optimized and the database no
longer performs physical or logical IO on the DUAL table even though
the DUAL table still actually exists.
If we drop a dual table then it will have serious impact on the database functionality. so we should never drop dual table..
SQL> desc dual
Name Null? Type
----------------------------------------- -------- ----------------------------
DUMMY VARCHAR2(1)
SQL> select * from dual;
D
-
X
SQL> select count(*) from dual;
COUNT(*)
----------
1
SQL> select 'sdfsdfsdfsdfsdfsdf' from dual;
'SDFSDFSDFSDFSDFSD
------------------
sdfsdfsdfsdfsdfsdf
SQL> select 3434334 from dual;
3434334
----------
3434334
SQL> truncate table dual;
truncate table dual
*
ERROR at line 1:
ORA-04020: deadlock detected while trying to lock object
3510531527724648449742036
SQL> delete from dual;
delete from dual
*
ERROR at line 1:
ORA-01031: insufficient privileges
SQL> connect system/manager
Connected.
SQL> /
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from dual;
select * from dua
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from dual;
no rows selected
1.What is the dual table, what is its purpose.
2.Why does it contain only one column with datatype varchar2, why not
number .
3.Does it contain one row by default.
4.why do we
usually SELECT USER FROM DUAL,
why cant I do it like
SQL> select USER FROM EMP WHERE ROWNUM<2;
USER
------------------------------
SCOTT
5.Does this mean that we are using the dual table only for the convenience that it has
only one row, and it will return only one row back , when we give queries like
SELECT USER FROM DUAL
6.SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
2
SQL> INSERT INTO DUAL VALUES ('X');
1 row created.
SQL> INSERT INTO DUAL VALUES ('X');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
4
SQL> SELECT * FROM DUAL;
D
-
X
Why is it returning only one row , it has to return 4 rows , when it has allowed me to
insert 4 rows.
7.SQL> delete from dual;
1 row deleted.
SQL> select * from dual;
D
-
X
SQL> select count(*) from dual;
COUNT(*)
----------
3
I want to delete all the rows , I do a delete from dual, and oooo mama
look at it , it deletes only one row, why?
8.SQL> delete from dual;
1 row deleted.
SQL> delete from dual;
1 row deleted.
SQL> select count(*) from dual;
COUNT(*)
----------
1
NOw we have only one row in dual.
lets create a example function.
SQL> create or replace function foo return number
2 as
3 x number;
4 begin
5 x:=1;
6 return 1;
7 end;
8 /
Function created.
SQL> select foo from dual;
FOO
----------
1
SQL> insert into dual values ('X');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
2
SQL> select foo from dual;
FOO
----------
1
shouldnt it return 2 rows of value 1?
because , look below , I have got 14 rows back , because I had 14 rows in the table.
SQL> SELECT FOO FROM EMP;
FOO
----------
1
1
1
1
1
1
1
1
1
1
1
FOO
----------
1
1
1
14 rows selected.
SQL>
Kindly elucidate the concept of dual table, and if possible illustrate it.
Thank you
and we said...
Let me just start by saying -- DUAL is owned by SYS. SYS owns the data dictionary,
therefore DUAL is part of the data dictionary. You are not to modify the data dictionary
via SQL ever -- wierd things can and will happen -- you are just demonstrating some of
them. We can make many strange things happen in Oracle by updating the data dictionary.
It is neither recommend, supported nor a very good idea.
1.What is the dual table, what is its purpose.
dual is just a convienence table. You don't need to use it, you can use anything you
want. The advantage to dual is the optimizer understands dual is a special one row, one
column table -- when you use it in queries, it uses this knowledge when developing the
plan.
2.Why does it contain only one column with datatype varchar2, why not
number .
truly, why no. Why not a date you would ask then. The column, its name, its datatype
and even its value are NOT relevant. DUAL exists solely as a means to have a 1 row table
we can reliably select from. Thats all.
3.Does it contain one row by default.
yes, when we build the database, we build dual and put a single row in it.
4.why do we
usually SELECT USER FROM DUAL,
why cant I do it like
SQL> select USER FROM EMP WHERE ROWNUM<2;
truly, why can't you? is something preventing you from doing so?? You can if you want.
Me, I'll stick with "select user from dual". I know dual exists. I know it has at least
1 and at most 1 row. I know the optimizer knows all about dual and does the most
efficient thing for me.
5) yes
6) the optimizer understands dual is a magic, special 1 row table. It stopped on the
select * because there is to be one row in there. Its just the way it works. Hopefully
you reset dual back to 1 row after your testing or you just totally broke your database!
7) like I said, duals magic, the optimizer knows all about what DUAL should be and does
things based on that.
8) dual = magic. dual is a one row table however having more then 1 or less then one is
dangerous. You are updating the data dictionary. You should naturally expect very bad
things to happen.
Ok, here is some trivia for you out there. How did I do this:
SVRMGR> select * from dual;
D
-
X
1 row selected.
SVRMGR> ????????????????????;
Statement processed.
SVRMGR> select * from dual;
ADDR INDX INST_ID D
-------- ---------- ---------- -
01680288 0 1 X
1 row selected.
--------------
Some Examples:
SQL> desc dual
Name Null? Type
----------------------------------------- -------- ----------------------------
DUMMY VARCHAR2(1)
SQL> select * from dual;
D
-
X
SQL> select count(*) from dual;
COUNT(*)
----------
1
SQL> select 'sdfsdfsdfsdfsdfsdf' from dual;
'SDFSDFSDFSDFSDFSD
------------------
sdfsdfsdfsdfsdfsdf
SQL> select 3434334 from dual;
3434334
----------
3434334
SQL> truncate table dual;
truncate table dual
*
ERROR at line 1:
ORA-04020: deadlock detected while trying to lock object
3510531527724648449742036
SQL> delete from dual;
delete from dual
*
ERROR at line 1:
ORA-01031: insufficient privileges
SQL> connect system/manager
Connected.
SQL> /
1 row deleted.
SQL> commit;
Commit complete.
SQL> select * from dual;
select * from dua
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from dual;
no rows selected
1.What is the dual table, what is its purpose.
2.Why does it contain only one column with datatype varchar2, why not
number .
3.Does it contain one row by default.
4.why do we
usually SELECT USER FROM DUAL,
why cant I do it like
SQL> select USER FROM EMP WHERE ROWNUM<2;
USER
------------------------------
SCOTT
5.Does this mean that we are using the dual table only for the convenience that it has
only one row, and it will return only one row back , when we give queries like
SELECT USER FROM DUAL
6.SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
2
SQL> INSERT INTO DUAL VALUES ('X');
1 row created.
SQL> INSERT INTO DUAL VALUES ('X');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
4
SQL> SELECT * FROM DUAL;
D
-
X
Why is it returning only one row , it has to return 4 rows , when it has allowed me to
insert 4 rows.
7.SQL> delete from dual;
1 row deleted.
SQL> select * from dual;
D
-
X
SQL> select count(*) from dual;
COUNT(*)
----------
3
I want to delete all the rows , I do a delete from dual, and oooo mama
look at it , it deletes only one row, why?
8.SQL> delete from dual;
1 row deleted.
SQL> delete from dual;
1 row deleted.
SQL> select count(*) from dual;
COUNT(*)
----------
1
NOw we have only one row in dual.
lets create a example function.
SQL> create or replace function foo return number
2 as
3 x number;
4 begin
5 x:=1;
6 return 1;
7 end;
8 /
Function created.
SQL> select foo from dual;
FOO
----------
1
SQL> insert into dual values ('X');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT COUNT(*) FROM DUAL;
COUNT(*)
----------
2
SQL> select foo from dual;
FOO
----------
1
shouldnt it return 2 rows of value 1?
because , look below , I have got 14 rows back , because I had 14 rows in the table.
SQL> SELECT FOO FROM EMP;
FOO
----------
1
1
1
1
1
1
1
1
1
1
1
FOO
----------
1
1
1
14 rows selected.
SQL>
Kindly elucidate the concept of dual table, and if possible illustrate it.
Thank you
and we said...
Let me just start by saying -- DUAL is owned by SYS. SYS owns the data dictionary,
therefore DUAL is part of the data dictionary. You are not to modify the data dictionary
via SQL ever -- wierd things can and will happen -- you are just demonstrating some of
them. We can make many strange things happen in Oracle by updating the data dictionary.
It is neither recommend, supported nor a very good idea.
1.What is the dual table, what is its purpose.
dual is just a convienence table. You don't need to use it, you can use anything you
want. The advantage to dual is the optimizer understands dual is a special one row, one
column table -- when you use it in queries, it uses this knowledge when developing the
plan.
2.Why does it contain only one column with datatype varchar2, why not
number .
truly, why no. Why not a date you would ask then. The column, its name, its datatype
and even its value are NOT relevant. DUAL exists solely as a means to have a 1 row table
we can reliably select from. Thats all.
3.Does it contain one row by default.
yes, when we build the database, we build dual and put a single row in it.
4.why do we
usually SELECT USER FROM DUAL,
why cant I do it like
SQL> select USER FROM EMP WHERE ROWNUM<2;
truly, why can't you? is something preventing you from doing so?? You can if you want.
Me, I'll stick with "select user from dual". I know dual exists. I know it has at least
1 and at most 1 row. I know the optimizer knows all about dual and does the most
efficient thing for me.
5) yes
6) the optimizer understands dual is a magic, special 1 row table. It stopped on the
select * because there is to be one row in there. Its just the way it works. Hopefully
you reset dual back to 1 row after your testing or you just totally broke your database!
7) like I said, duals magic, the optimizer knows all about what DUAL should be and does
things based on that.
8) dual = magic. dual is a one row table however having more then 1 or less then one is
dangerous. You are updating the data dictionary. You should naturally expect very bad
things to happen.
Ok, here is some trivia for you out there. How did I do this:
SVRMGR> select * from dual;
D
-
X
1 row selected.
SVRMGR> ????????????????????;
Statement processed.
SVRMGR> select * from dual;
ADDR INDX INST_ID D
-------- ---------- ---------- -
01680288 0 1 X
1 row selected.
--------------
Hotel and Casino Council Bluffs - MapYRO
ReplyDeleteFind hotels, motels, and other lodging near Casino Council 충청북도 출장안마 Bluffs Casino and 충청북도 출장안마 other Hotels in Council Bluffs, IA. 당진 출장샵 See 영천 출장안마 nearby landmarks, amenities, and search for 나주 출장마사지