What is and what is the utility of the DUAL table for Oracle?

Asked

Viewed 7,922 times

18

I came across this scenario during the modification of a report in the system:

One of querys is returning to this such table DUAL:

select * from dual

But in the system there is no DUAL table, and the answer they gave me was that it was a native table of Oracle.

From that arose the doubts:

  • What is and what is the usefulness of the DUAL table?

  • If the goal is to return an impossible value, why not return a value that will return null of an existing table?

  • 1

    I’m reading, the utility I still don’t understand https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries009.htm, I just understood that it is created automatically and is accessible to all users, I hope the link helps someone get close to the answer ;)

  • it is an "imaginary" table for you to use functions, for example you want to use a sysdate(), you do not pdoe simply run it you have to call in a select

2 answers

18


It is a kind of false table with a single record, used to select when you are not really interested in the data, but want to receive the result of some function or variable.

In simplest words it is a table "imaginary" for you to use functions, for example, you want to use a sysdate, you can’t just run it, you should call in the scope of a select.

Example:

select sysdate from dual;

Source

EDIT

Oracle differs from SQL Server in that you can simply select without having a clause FROM. This is mainly because the dual table allows you to create a JOIN, unlike the way SQL Server works. In Oracle it is possible to duplicate the lines if necessary. In addition, conceptually every selection must have an origin. The dual table is of generic origin of a single record, with the intention of creating this necessary interaction between the elements.

Source

8

Just complementing the response of Luiz Santos, giving comparative examples with other Dbms:

In SQL Server or Mysql, when you want to select a value that is not contained in a specific table (such as a variable declared in a scope of query, or native function), we can simply use the command SELECT, without specifying where we select these values from:

SELECT 'EXEMPLO'

On the other hand, in the Oracle databases, we cannot carry out the above command, considering the way Oracle works its SELECT, obliging us to clarify where we seek this data, through the clause FROM. For the same example given above in an Oracle database, the action would be represented by the following clause:

SELECT 'EXEMPLO' FROM DUAL

In my opinion it is a concept somewhat equisito (with gambiarra face) this table "imaginary", used for relatively simple operations (a selection), which leaves the code even confusing for those reading it or coming from another context that applies SQL.

At this link there is a brief history of the table, told by its creator, Chuck Weiss, if you are interested, in English.

  • 1

    In fact, it should be strange to be able to make it selection without an origin. In the concept of database every selection should have an origin. The dual is generic origin of a single record, with the intention of creating this necessary interaction between the elements.

  • 2

    Also the dual allows you to create a Join, unlike the way sql serves work. In the oracle it is possible to duplicate the lines, if necessary.

  • 2

    @Luizsantos I don’t think so. For example: if I have a query in which I declare a variable within its scope, and I want to select this variable, why do I have to mention that this variable belongs to a table (dual in case)?

  • 2

    It makes no sense to put something that doesn’t belong to the other inside of it. One can take into account until relational Lgebra in this case (projecte x but x is not dual, why say it is there?)

  • 2

    It is not that it belongs, but it is for you to explain that you want to just one record of this, if you want two, you can do dual Join. There are not only cases where you want to return only a record equal to sql server allows, even more in oracle a database widely used as "bunch of rules"

  • 2

    I agree that there may be more applications for the table, but still in my opinion, it is a bizarre concept of Oracle.

  • 1

    I believe it is something of origin, I was born in the oracle. Probably you no longer.

  • 2

    Yes, this also influences kkk, I have customs with Sqlserver

  • 1

    I know very few people are fans of mathematics in the sense that we mathematicians are. But what Oracle does seems to me more appropriate from the point of view of relational algebra. About the link in English, it’s nice to see that it has this name because what you wanted originally were two lines.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.