Introduction and Comparison of ROWID and ROWNUM

Source: Internet
Author: User
Tags dname
About ROWID: When you insert a row of data into a table, ORACLE automatically adds a ROWID to the row. Each row has a unique ROWID. ORACLE uses ROWID to locate the row. ROWID is not explicitly stored as a column (a pseudo column -- instead of the actual data in the table, it may be converted based on the information of the block where the row is located using functions internally), yes

About ROWID: When you insert a row of data into a table, ORACLE automatically adds a ROWID to the row. Each row has a unique ROWID. ORACLE uses ROWID to locate the row. ROWID is not explicitly stored as a column (a pseudo column -- instead of the actual data in the table, it may be converted based on the information of the block where the row is located using functions internally), yes

About ROWID:

When you insert a row of data into a table, ORACLE automatically adds a ROWID to the row. Each row has a unique ROWID. ORACLE uses ROWID to locate the row. ROWID is not explicitly stored as a column value (a pseudo column -- instead of the actual data in the table, it may be converted based on the information of the block where the row is located using functions internally ), is the fastest way to access the row in a table. The value of the index row stored in the index and the value of the ROWID of the index row-actual data.

Oracle rowid is divided into physical ROWID and logical ROWID.
For details, see: get information about data blocks through rowid.
About ROWNUM: For rownum, it is the number of the row that the oracle System sequentially assigns to return from the query. the first row of the returned value is 1, the second row is 2, and so on, this pseudo field can be used to limit the total number of rows returned by the query, and rownum cannot be prefixed with any table name.
ROWNUM examples are summarized as follows: When = is used, only rownum = 1 is useful, = other values will return empty sets.
Use <和<=时,能返回所需的行。
When using> and> =, only> = 1 returns the full table data, while others can only return empty sets. For query conditions with rownum greater than a certain value, rownum> 2 cannot be used to query records because rownum is a pseudo column always starting from 1, oracle considers that the condition rownum> n (Natural Number of n> 1) is still not true, so records cannot be found.
Less than or equal:
BYS @ bys3> select * from dept where rownum <2;
DEPTNO DNAME LOC
-------------------------------------
10 ACCOUNTING NEW YORK
BYS @ bys3> select * from dept where rownum <= 2;
DEPTNO DNAME LOC
-------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS

Greater than or equal:

BYS @ bys3> select * from dept where rownum> = 1;
DEPTNO DNAME LOC
-------------------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
BYS @ bys3> select * from dept where rownum> 1;
No rows selected
BYS @ bys3> select * from dept where rownum> 2;
No rows selected
BYS @ bys3> select * from dept where rownum> = 2;
No rows selected

Equal:
BYS @ bys3> select * from dept where rownum = 1;
DEPTNO DNAME LOC
-------------------------------------
10 ACCOUNTING NEW YORK
BYS @ bys3> select * from dept where rownum = 2;
No rows selected

Not equal to: -- if the condition is not true, an empty set is returned.
BYS @ bys3> select * from dept where rownum <> 1;
No rows selected
#############
Example of ROWNUM and ROWID changes in the DML operation: the system assigns the number to the record row in the order of record insertion, And the rowid is also allocated sequentially.
Rownum indicates the position of a query record in the entire result set, which is sequentially allocated in the query result set.
1. query row numbers and ROWNUM in ROWID and ROWID.
BYS @ bys3> select rowid, dbms_rowid.rowid_row_number (rowid) rowid_num, rownum, dept. * from dept order by deptno desc;
ROWID ROWID_NUM ROWNUM DEPTNO DNAME LOC
---------------------------------------------------------------------------
AAAFT7AAEAAAAIFAAD 3 4 40 OPERATIONS BOSTON
AAAFT7AAEAAAAIFAAC 2 3 30 SALES CHICAGO
AAAFT7AAEAAAAIFAAB 1 2 20 RESEARCH DALLAS
AAAFT7AAEAAAAIFAAA 0 1 10 ACCOUNTING NEW YORK
Take one record as an example:
AAAFT7AAEAAAAIFAAC 2 3 30 SALES CHICAGO
The ROWID of this row is AAAFT7AAEAAAAIFAAC. According to the ROWID algorithm, 2nd rows in the data block can be obtained.
However, the ROWNUM of this row is 3, which is the sorting in the query result set. We can intuitively compare the rows of data in a ROWID in a data block with the number of ROWNUM.
2. delete a piece of data
BYS @ bys3> delete dept where deptno = 30;
1 row deleted.
BYS @ bys3> commit;
Commit complete.
3. query row numbers and ROWNUM in ROWID and ROWID. It is found that the row numbers in ROWID and ROWID have been deleted, but ROWNUM is automatically allocated sequentially.
BYS @ bys3> select rowid, dbms_rowid.rowid_row_number (rowid) rowid_num, rownum, dept. * from dept order by deptno desc;
ROWID ROWID_NUM ROWNUM DEPTNO DNAME LOC
---------------------------------------------------------------------------
AAAFT7AAEAAAAIFAAD 3 3 40 OPERATIONS BOSTON
AAAFT7AAEAAAAIFAAB 1 2 20 RESEARCH DALLAS
AAAFT7AAEAAAAIFAAA 0 1 10 ACCOUNTING NEW YORK
4. Insert a data entry
BYS @ bys3> insert into dept values (99, 'chedan ', 'bj ');
1 row created.
BYS @ bys3> commit;
Commit complete.
5. query row numbers and ROWNUM in ROWID and ROWID. It is found that ROWID and row number in ROWID are automatically allocated downward instead of reusing the ROWID of the row deleted in step 1. ROWNUM is automatically allocated sequentially.
BYS @ bys3> select rowid, dbms_rowid.rowid_row_number (rowid) rowid_num, rownum, dept. * from dept order by deptno desc;
ROWID ROWID_NUM ROWNUM DEPTNO DNAME LOC
---------------------------------------------------------------------------
AAAFT7AAEAAAAIFAAE 4 4 99 chedan bj
AAAFT7AAEAAAAIFAAD 3 3 40 OPERATIONS BOSTON
AAAFT7AAEAAAAIFAAB 1 2 20 RESEARCH DALLAS
AAAFT7AAEAAAAIFAAA 0 1 10 ACCOUNTING NEW YORK

ROWNUM is used for different sorting results. Will ROWNUM be changed? The experiment results will not change BYS @ bys3> select rownum, dept. * from dept;

ROWNUM DEPTNO DNAME LOC
-----------------------------------------------
1 10 ACCOUNTING NEW YORK
2 20 RESEARCH DALLAS
3 40 OPERATIONS BOSTON
4 99 chedan bj
BYS @ bys3> select rownum, dept. * from dept order by loc;
ROWNUM DEPTNO DNAME LOC
-----------------------------------------------
3 40 OPERATIONS BOSTON
2 20 RESEARCH DALLAS
1 10 ACCOUNTING NEW YORK
4 99 chedan bj

Method for querying the last row of record in the Table: BYS @ bys3> select * from dept where rowid in (select max (rowid) from dept );
DEPTNO DNAME LOC
-------------------------------------
40 OPERATIONS BOSTON
BYS @ bys3> select * from dept where rownum <= (select count (*) from dept) minus select * from dept where rownum <= (select count (*) -1 from dept );
DEPTNO DNAME LOC
-------------------------------------
40 OPERATIONS BOSTON

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.