DUAL table

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The DUAL table is a special one-row, one-column table present by default in all Oracle database installations. It is suitable for use in selecting a pseudo column such as SYSDATE or USER. The table has a single VARCHAR2(1) column called DUMMY that has a value of 'X'.

Example use[edit]

Oracle's SQL syntax requires the FROM clause but some queries don't require any tables - DUAL can be readily used in these cases.

SELECT 1+1
FROM DUAL;
 
SELECT 1 
FROM   DUAL;
 
SELECT USER 
FROM   DUAL;
 
SELECT SYSDATE 
FROM dual;

History[edit]

The DUAL table was created by Charles Weiss of Oracle Corporation to provide a table for joining in internal views:

I created the DUAL table as an underlying object in the Oracle Data Dictionary. It was never meant to be seen itself, but instead used inside a view that was expected to be queried. The idea was that you could do a JOIN to the DUAL table and create two rows in the result for every one row in your table. Then, by using GROUP BY, the resulting join could be summarized to show the amount of storage for the DATA extent and for the INDEX extent(s). The name, DUAL, seemed apt for the process of creating a pair of rows from just one.[1]

The original DUAL table had two rows in it (hence its name), but subsequently it only had one row.

Optimization[edit]

DUAL is 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.

Beginning with 10g Release 1 Oracle Database have been optimized[2] and the database no longer performs physical or logical IO on the DUAL table even though the DUAL table still actually exists.

DUAL is readily available for all the users in database. It is not limited to system administrator only.

In other database systems[edit]

  • Firebird has a one-row system table RDB$DATABASE that is used in the same way as Oracle's DUAL, although it also has a meaning of its own.
  • IBM DB2 has a view that resolves DUAL when using Oracle Compatibility [3]
  • Microsoft Access: A table named DUAL may be created and the single-row constraint enforced via ADO (Table-less UNION query in MS Access)
  • MySQL allows DUAL to be specified as a table in queries that do not need data from any tables.[4]
  • PostgreSQL: A DUAL-view can be added to ease porting from Oracle.[5]
  • SQLite: A VIEW named "dual" that works the same as the Oracle "dual" table can be created as follows: "CREATE VIEW dual AS SELECT 'x' AS dummy;"
  • SAP HANA has a table called DUMMY that works the same as the Oracle "dual" table.

Notes[edit]