The difference between Rowid and Rownum in oracle

Both rowid and rownum are imaginary columns, but have completely different meanings. rowid is the physical address, which is used to locate the physical storage location of specific data in oracle, and rownum is the sorting of the output result of sql. In layman's terms: rowid is relatively unchanged, rownum will change, especially when using order by.
rowid: used to locate the position of a certain piece of data in the data table, it is unique and will not change

rownum: indicates the position of a query in the entire result set, the rownum corresponding to different query conditions for the same record is different and rowid It is an invariant
rowid:
we often use it when dealing with duplicate records in a table, and deduplication in
sql server:
Select * from stuinfo a where not exists(select 1 from stuinfo where stuName=a.stuName and ID<a. ID) 

has another idea, since rowid is unique, why not use it as the primary key, and define another primary key column?
Reason 1: (RowID can be changed at any time) First, RowID is also a pseudo column, but he is also unique.
Each value represents the address of the data block. The primary key can be done, but it is easy to take effect. When you move the data, such as import and export operations, the RowID will also change. So RowID is not suitable for primary key.
Reason 2 (the primary key also contains an automatically created unique index). The process of index creation is bound by RowID and column value.
When the address of the data block changes, such as deleting a record, the index will also be automatically maintained
. That is to say, RowID will change automatically, which is also the method

rownum of ROWID used in ORACLE:
In Oracle, to query the first N records according to specific conditions, you can do it with a rownum. select * from emp whererownum<= 5 and the book also warns that ">" cannot be used for rownum, which means that if you want to use select * from emp whererownum> 5, it will fail. To know why it fails, you need to understand the mechanism behind rownum:
1. The database executes the query
2. Oracle reads the first row and calls it row 1.
3. Does the data we read in the past meet the standards? If it does not match, then Oracle discards the row, if it matches, Oracle will return the row.
4. Oracle reads the number of rows proposed by the next row (2, then 3, then 4, etc.).
5. Go back to the third step. After
understanding the principle, you will know that rownum> will not succeed, because the row queried in the third step has been discarded, and the rownum found in the fourth step is still 1, which will never be success.

In the same way, if rownum is used alone =, it is only useful when rownum=1.
rownum can be used in pagination queries:
the following is the code:
  SELECT * FROM

  (

  SELECT A.*, ROWNUM RN

  FROM (SELECT * FROM table name) A

  WHERE ROWNUM <= 40

  )

  WHERE RN >= 21

  The innermost query SELECT * FROM table name represents the original query statement without page turning. ROWNUM <= 40 and RN >= 21 control the extent of each page for paginated queries.

  The Oracle paging query statement given above has high efficiency in most cases. The purpose of paging is to control the size of the output result set and return the results as soon as possible. In the above paging query statement, this consideration is mainly reflected in the sentence WHERE ROWNUM <= 40.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360007&siteId=291194637