ROWNUM Vs. ROWID
What is the difference between ROWNUM and ROWID? Does Oracle make a distinction between a ROWID and
ROWNUM?
Rownum (Numeric) = Generated Sequence Number of your output. For each row returned by a query,
the ROWNUM pseudo column returns a number indicating the order in which Oracle selects the row from a
table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.
So you can limit the number of rows in your result.
Rowid (Hexadecimal) = Generated automatically at the time of insertion of row. For each row in the database,
the ROWID pseudo column returns the address of the row.
| ROWID | ROWNUM |
| 1.Physical address of the rows. | 1.Rownum is the sequential number, allocated to each returned row during query execution. |
2.Rowid is permanent | 2.Rownum is temporary. |
3.Rowid is 16-bit hexadecimal | 3.Rownum is numeric |
4.Rowid gives address of rows or records | 4.Rownum gives count of records |
5.Rowid is automatically generated unique id of a row and it is generated at the time of insertion of row. | 5.Rownum is a dynamic value automatically |
6.ROWID is the fastest means of accessing data. | 6.Retrieved along with select statement output. |
7.They are unique identifiers for the any row in a table. | 7.It represents the sequential order in which Oracle has retrieved the row. |
Check the below Query displaying placeholder and physical location of the row.
SQL> SELECT ID, ROWID, ROWNUM From EMP_TEST;ID ROWID ROWNUM102 AAASd8AAEAAAAIcAAB 1---------- ------------------ ----------104 AAASd8AAEAAAAIcAAD 2110 AAASd8AAEAAAAIcAAJ 5105 AAASd8AAEAAAAIcAAE 3107 AAASd8AAEAAAAIcAAG 4103 AAASd8AAEAAAAIfAAA 6106 AAASd8AAEAAAAIfAAB 7109 AAASd8AAEAAAAIfAAD 9108 AAASd8AAEAAAAIfAAC 89 rows selected.
When a ROWID changes?
A ROWID is assigned to a row upon insert and is immutable (never changing). Changes happens in the following situations-
- When you update a partition key and rows moves the partition.
- Flashback a table, since the flashback table command really issues a DELETE+INSERT to put the data back the way it was.
- If you use alter table test shrink space compact, the ROWID could change.
- When you move table with Alter command- would change the ROWID’s.
SQL> CREATE TABLE MyTab As2 SELECT ID, NAME From EMP_TEST Where ROWNUM<=4;Table created.ROWID ROWNUM ID NAMESQL> SELECT ROWID, ROWNUM, ID, NAME From MyTab;AAAS7GAAEAAAARjAAA 1 102 Ankit------------------ ---------- ---------- ----------AAAS7GAAEAAAARjAAB 2 104 NikhilSQL> ALTER Table MyTab MOVE;AAAS7GAAEAAAARjAAC 3 105 RajanAAAS7GAAEAAAARjAAD 4 107 KaranAAAS7HAAEAAAARrAAA 1 102 AnkitTable altered.SQL> SELECT ROWID, ROWNUM, ID, NAME From MyTab;ROWID ROWNUM ID NAME------------------ ---------- ---------- ----------AAAS7HAAEAAAARrAAD 4 107 KaranAAAS7HAAEAAAARrAAB 2 104 NikhilAAAS7HAAEAAAARrAAC 3 105 Rajan