The difference between rownum and rowid in oracle

The difference between rownum and rowid in oraclehttp
://www.cnblogs.com/qqzy168/archive/2013/09/08/3308648.html
Both rownum and rowid are pseudo-columns, but the two are fundamentally different, rownum is based on The result of the sql query assigns a logical number to each row, so your sql is different, the final rownum will be different,
but the rowid is physical structure, when each record is inserted into the database, there will be a unique physical Record (will not change),
such as AAAMgzAAEAAAAAgAAB 7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
The physical location of AAAMgzAAEAAAAAgAAB here corresponds to this record, this record will not change with the change of sql.
Therefore, this leads to their different usage scenarios. Usually, we use rownum when sql pagination or when looking for records in a certain range.
1. rownum
For example:
find records in the range of 2 to 10 (including records of 2 and 10 here)
select *
  from (select rownum rn, a.* from emp a) t
where t.rn between 2 and 10;

find the first three The name of the record
select * from emp a where rownum < 3; here we should pay attention that the range that is directly searched by rownum must contain 1; because rownum is recorded from 1, of course, you can find out rownum and put it in a virtual The fields in the table as this virtual table are then queried according to the conditions.
For example:
select *
  from (select rownum rn, a.* from emp a) t
where t.rn > 2; that's it.


Points :
The use of ROWNUM in Oracle. http://www.blogjava.net/conans/articles/219693.html
1. ROWNUM is a pseudo-column. After returning a result, each returned record will generate a corresponding ROWNUM value;
2. The returned result record ROWNUM is sorted from 1, so the first record is always 1; in
this way, when the first record is queried, the ROWNUM of the record is 1, but the condition requires ROWNUM>1, so it does not meet, continue to query the next record; Because there is no record that meets the requirements before, the ROWNUM of the next record is still 1, and this cycle will not produce results. The above query can be replaced by a subquery.
Summary: It is to grow sequentially on the result set that satisfies the conditions (such as where clause conditions).
Start to find the records that meet the conditions: if
the 1st item does not match, find the 2nd item if
the 2nd item does not match, find the 3rd item,
find the 3rd item, if the 3rd item matches, then the rownum of the 3rd item is 1
and the 4th item does not Complies with
Article 5, the rownum of Article 5 is 2
and so on
...


So if you don't use a subquery but directly use the condition rownum>1, although the third item satisfies the SQL query condition, but rownum=1 does not satisfy the rownum>1 condition, so the third item becomes unqualified again, then the fifth item The rownum becomes 1 again, so no records can be found.


2. We often use rowid
when dealing with duplicate records in a table. Of course, you can also use a very primitive method, which is to import the data in the table with duplicate records to another table, and then go back.
SQL>create table stu_tmp as select distinct* from stu;
SQL>truncate table sut; //Empty table records
SQL>insert into stu select * from stu_tmp; //Add the data in the temporary table back to the original table but it is the stu table If the data is in millions or even more than tens of millions, this method is obviously unwise, so we can process it according to the rowid. The rowid is unique and the query efficiency is very high.
For example, the student table The name in the table will be repeated, but the student number will not be repeated. If we want to delete the record of the student with the largest student number in the student table, what should we do?
delete from stu a
    where rowid not in (select max(rowid)
                          from stu b
                         where a.name = b.name
                           and a.stno < b.stno);
that's it.

Guess you like

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