Oracle Study Notes dual table

Source: Internet
Author: User

Recently, Oracle encountered a special table dual. I think oracle users have a special liking for dual. As a result, I found some posts on the Internet and thought it would be better to post them here for everyone to learn.

Dual is a virtual table used to form the select syntax rule. Oracle ensures that there will always be only one record in dual. We can use it to do a lot of things, as shown below:

1. view the current user. You can execute the following statement Select User from dual in SQL plus;

2. Used to call system functions

Select to_char (sysdate, 'yyyy-mm-dd hh24: MI: ss') from dual; -- get the current system time

Select sys_context ('userenv', 'terminal') from dual; -- get the host name

Select sys_context ('userenv', 'language') from dual; -- get the current locale

Select dbms_random.random from dual; -- obtain a random number

3. Obtain the next or current value of the sequence. Use the following statement.

Select your_sequence.nextval from dual; -- Obtain the next value of the sequence your_sequence

Select your_sequence.currval from dual; -- get the current value of the sequence your_sequence

4. You can use the calculator select 7*9 from dual;

 

5: This one is self-added. If you use an SQL statement to insert multiple data records at the same time, the dual table is also used. This speed must be much faster than inserting data in a for loop.

Insert into Table Name (Field 1, Field 2)

Select '1', '2' from dual

Union all

Select '3', '4' from dual

Union all

Select '5', '6' from dual

Union all

Select '7', '8' from dual

Union all

Select '9', '10' from dual

------

In Oracle, the dual table is a "mysterious" table. Many online users have tested this table. This table has only one row and one column, in fact, this table is the same as other tables in the system. You can perform insert, update, and delete operations as well as drop operations. But do not perform the drop table operation. Otherwise, the system will be unavailable and the database will report
Startup crashes with ORA-1092 error. In this case, do not be confused. You can perform the following steps to restore. You can use the Sys user to log on.

SQL> Create pfile = 'd: pfile. Bak 'from spfile

SQL> shutdown immediate

Add the last entry to the D: pfile. Bak file:

Replication_dependency_tracking = false

Restart the database:

SQL> startup pfile = 'd: pfile. Bak'

SQL> Create Table "sys". "dual"

[An error occurred while processing this directive]

=====

Dual? What's mysterious? If you want to get the time of the Oracle system, you can simply type a line of SQL? To learn more ....

SQL> select sysdate from dual;

Sysdate

---------

28-sep-03

Haha, it is indeed very convenient to use dual. But do you know what the dual is and what special behavior it has?
Let's take a look. First, let's figure out what dual is:

SQL> connect system/Manager

Connected.

SQL> select owner, object_name, object_type from dba_objects where
Object_name like '% dual %'; Owner object_name object_type

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

Sys dual table public dual Synonym

The original dual is a table in SYS schema and is then used by other database users in public synonym mode.

Let's look at its structure:

SQL> DESC dual name null? Type

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

Dummy varchar2 (1)

SQL>

There is only one empty column named dummy.

Query the data in the table:

SQL> select dummy from dual;

Dummy

----------

X

Oh, there is only one record. The dummy value is 'x'. It's normal. It's no surprise. Well, there's something amazing coming up!

Insert a record:

SQL> connect sys as sysdba

Connected.

SQL> insert into dual values ('y ');

1 row created.

SQL> commit;

Commit complete.

SQL> select count (*) from dual;

Count (*)

----------

2

So far, everything works. But when we query records again, something strange happened.

SQL> select * from dual;

Dummy

----------

X

The record just inserted is not displayed! The dual table has two records, but only one record is displayed!

Try to delete the file. Click "delete" to delete all files!

SQL> Delete from dual;/* try to delete all records without restrictions */

1 row deleted.

SQL> commit;

Commit complete.

Haha, only one record is deleted,

SQL> select * from dual;

Dummy

----------

Y

Why? Does SQL syntax not work for dual? With this question,

I have queried some official Oracle documents. Oracle performed some internal processing on the dual table operations and tried to ensure that only one record is returned in the dual table. Of course, this write internal operation is invisible.
. It seems that Oracle is an infinite mystery!

Appendix: Oracle interpretation of unusual features of dual tables 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 shoshould always remember and keep is: Dual table shoshould 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.

(Reproduced: http://zhoujingxian.javaeye.com/blog/494288)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.