Oracle dual table details

Source: Internet
Author: User

Read itpub previous post: http://www.itpub.net/viewthread.php? Tid = 981212 & extra = & page = 1
Want to learn the following Oracle dual table.

Oracle asktom explanation: http://asktom.oracle.com/pls/asktom/f? P = 100: 11: 0: p11_question_id: 1562813956388

Source: http://wzhcn.spaces.live.com/blog/cns! Fba7ef989c66522e! 515. Entry

1. Purpose of the dual table
dual is an existing table in Oracle and can be read by any user, commonly Used in select statement blocks without a target table
-- view the current connected user
SQL> Select User from dual;
User
----------------------------
System
-- view the current date and time
SQL> select sysdate from dual;
sysdate
-----------
2007-1-24 1
SQL> select to_char (sysdate, ''yyyy-mm-dd hh24: MI: s '') from dual;
to_char (sysdate, ''yyyy-MM-DDHH2
----------------------------
15:02:47
-- used as a calculator
SQL> select 1 + 2 from dual;
1 + 2
----------
3
-- view the sequence value
SQL> Create sequence AAA increment by 1 start with 1;
SQL> select AAA. nextval from dual;
nextval
----------
1
SQL> select AAA. currval from dual;
currval
----------
1

2. Dual table testing and analysis
Dual is a table with one row and one column. If you execute insert, delete, and truncate operations in the table, manyProgramProblem. The results also vary with SQL * Plus, PL/SQL Dev, and other tools.
-- View what dual is.
-- Dual is a table in SYS schema, which is then used by other database users in the form of public synonym.
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

-- View the table structure. There is only one field dummy, which is of the varchar2 (1) type.
SQL> DESC dual
Name type nullable default comments
---------------------------------------
Dummy varchar2 (1) y

-- Dual table structure:
Create Table SYS. Dual
(
Dummy varchar2 (1)
)
Tablespace System
Pctfree 10
Pctused 40
Initrans 1
Maxtrans 255
Storage
(
Initial 16 K
Next 16 K
Minextents 1
Maxextents 505
Pctincrease 50
);

/*
I am very confused. Why does Oracle use varchar (1) and char (1? From the perspective of such a table structure, the dual table is designed to be as simple as possible to reduce the retrieval overhead.
Also, the dual table is created in the system tablespace. The first reason is that the dual table is created by the user SYS. The default tablespace is system, it is good for system performance to store the table that may be frequently queried separately from the user table.
It is not enough to create a table or a synonym. Dual is under the Sys schema. Therefore, other users cannot log on to the table. Therefore, authorization is required:
Grant select on SYS. Dual to public with grant option;
Grant the select permission to the public.
Next, let's look at the data in the dual table. In fact, the data in the dual table has a very important relationship with the Oracle database environment (Oracle will not be paralyzed by this, however, many stored procedures and some queries cannot be correctly executed ).
*/

-- Query the number of rows
-- After the database is created, a record is inserted in the dual table. In my opinion, the value of the dummy field does not matter. What is important is the number of records in the dual table.
SQL> select count (*) from dual;
Count (*)
----------
1

SQL> select * from dual;
Dummy
-----
X

-- Insert data and query records. Only one row of records is returned.
SQL> insert into dual values (''y '');
1 row created.
SQL> commit;
Commit complete.
SQL> insert into dual values (''x '');
1 row created.
SQL> insert into dual values (''z '');
1 row created.
SQL> commit;
Commit complete.
SQL> select count (*) from dual;
Count (*)
----------
4
SQL> select * from dual;
Dummy
-----
X

/*
-- If we insert a piece of data, the dual table does not return a row but multiple rows of records. What is the result?
SQL> insert into dual values (''y '');
1 row inserted
SQL> commit;
Submitted
SQL> select * from dual;
Dummy
-----
X
Y
SQL> select sysdate from dual;
Sysdate
-----------
2004-12-15
2004-12-15

At this time, two records are returned, which also causes problems. When using
Select sysdate into v_sysdate from dual;
Oracle throws a too_many_rows (ORA-01422) exception for stored procedures that get time or other information.
Therefore, ensure that there is only one record in the dual table. Of course, the dual table's update, insert, and delete permissions cannot be released at will, which is very dangerous to the system.
*/

-- Cut off the table
SQL> truncate table dual;
Table truncated.
SQL> select count (*) from dual;
Count (*)
----------
0
SQL> select * from dual;
No rows selected
SQL> select sysdate from dual;
No rows selected

-- Try to delete the data in the dual table to see what results will appear:
SQL> Delete from dual;
1 row deleted
SQL> select * from dual;
Dummy
-----
SQL> select sysdate from dual;
Sysdate
-----------
/*
We cannot get the system date. Because sysdate is a function that acts on every data row. If no data is available, the system date cannot be retrieved.
This is widely used.
Select sysdate into v_sysdate from dual;
This method is fatal for stored procedures that take system time and other information, because Oracle immediately throws a no_data_found (ORA-01403) exception, even if the exception is caught, stored Procedures cannot correctly complete required actions.
*/

-- for delete operations, Oracle performs some internal processing on the dual table operations, and tries to ensure that only one record is returned in the dual table. of course, this write operation is invisible
-- no matter how many records exist in the table (except for no records), Oracle only deletes one data record for each delete operation.
SQL> select count (*) from dual;
count (*)
----------
2
SQL> Delete from dual;
one row has been deleted
SQL> commit;
submitted
SQL> select count (*) from dual;
count (*)
----------
1

/*
Appendix: Oracle interpretation of the unusual features of dual tables
There is internalized code that makes this happen. Code checks that ensurethat 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.
So dual shoshould always have 1 and only 1 row
*/

Dual tables can be inserted, updated, or deleted, and can be dropped. But do not perform the drop table operation, otherwise the system will not be available, the database cannot start, will report the database startup crashes with ORA-1092 error.

3. If the dual table is "Unfortunately" deleted and restored:
Log On with the Sys user.
Create a dual table.
Grant select permission to the public (for example, SQL, but do not grant update, insert, and delete permissions ).
Insert a record to the dual table (only one record): insert into dual values (''x '');
Submit changes.
-- Log On with the Sys user.
SQL> Create pfile = 'd: \ pfile. Bak 'from spfile
SQL> shutdown immediate
-- Add the last entry in the D: \ pfile. Bak file: replication_dependency_tracking = false.
-- Restart the database:
SQL> startup pfile = 'd: \ pfile. Bak'
SQL> Create Table "sys". "dual"
("Dummy" varchar2 (1 ))
Pctfree 10 pctused 4;
SQL> insert into dual values ('x ');
SQL> commit;
SQL> grant select on dual to public;
Authorization successful.

SQL> select * from dual;
D
-
X

SQL> shutdown immediate
The database has been closed.
The database has been detached.
The Oracle routine has been disabled.
SQL> startup
The Oracle routine has been started.

Total system global area 135338868 bytes
Fixed size 453492 bytes
Variable Size 109051904 bytes
Database buffers 25165824 bytes
Redo buffers 667648 bytes
The database has been loaded.
The database has been opened.
SQL>

-- OK, which can be used normally below.

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.