Translate

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                  ROWNUM
---------- ------------------ ----------
       102 AAASd8AAEAAAAIcAAB          1
       104 AAASd8AAEAAAAIcAAD          2
       105 AAASd8AAEAAAAIcAAE          3
       107 AAASd8AAEAAAAIcAAG          4
       110 AAASd8AAEAAAAIcAAJ          5
       103 AAASd8AAEAAAAIfAAA          6
       106 AAASd8AAEAAAAIfAAB          7
       108 AAASd8AAEAAAAIfAAC          8
       109 AAASd8AAEAAAAIfAAD          9

9 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 As
  2  SELECT ID, NAME From EMP_TEST Where ROWNUM<=4;

Table created.

SQL> SELECT ROWID, ROWNUM, ID, NAME From MyTab;

ROWID                  ROWNUM         ID NAME
------------------ ---------- ---------- ----------
AAAS7GAAEAAAARjAAA          1        102 Ankit
AAAS7GAAEAAAARjAAB          2        104 Nikhil
AAAS7GAAEAAAARjAAC          3        105 Rajan
AAAS7GAAEAAAARjAAD          4        107 Karan

SQL> ALTER Table MyTab MOVE;

Table altered.

SQL> SELECT ROWID, ROWNUM, ID, NAME From MyTab;

ROWID                  ROWNUM         ID NAME
------------------ ---------- ---------- ----------
AAAS7HAAEAAAARrAAA          1        102 Ankit
AAAS7HAAEAAAARrAAB          2        104 Nikhil
AAAS7HAAEAAAARrAAC          3        105 Rajan
AAAS7HAAEAAAARrAAD          4        107 Karan



Get involved and leave your Comments in the Box Below. The more people get involved, the more we all benefit. So, leave your thoughts before you leave the page. 

12 comments:

  1. alter table mytab move?
    what is the purpose of move here.

    ReplyDelete
  2. Nice helpful content, I am inspired by you and started running software interview questions blog --> https://www.sqlinterviewsquestions.com/2022/02/list-of-sql-tips-and-interview.html

    ReplyDelete
    Replies
    1. https://www.sqlinterviewsquestions.com/2022/02/list-of-sql-tips-and-interview.html

      Delete
  3. This is a fantastic article. Your site contains a wealth of useful information, particularly in the section on discourse. IAPPC, a leading Netsuite Partner in India for ERP installation and solution provider, provides end-to-end implementation services and support to meet the organization's demands.

    ReplyDelete
  4. I truly appreciate the time and work you put into sharing your knowledge. I found this topic to be quite effective and beneficial to me. Thank you very much for sharing. Continue to blog.

    Data Engineering Services 

    AI & ML Solutions

    Data Analytics Services

    Data Modernization Services

    ReplyDelete
  5. If you're looking for help in using it, just dial Quickbooks Customer Service +1 888-210-4052 to speak with a live representative.

    ReplyDelete
  6. Thank you for this informative blog it really helps me and also have a look on this Best Gynecologist In Faridabad

    ReplyDelete
  7. Usability Testing is a significant type of software testing technique. It is primarily used in user-centered design and development of software. It encompasses the understanding of users' requirements and building consumer-friendly applications. In order to test a project, it should be reviewed carefully while creating the user-centric model and then, it will help them envision their product through the eyes of the user. Click to know more about this article : Usability testing in software testing

    ReplyDelete
  8. christmas sign up 2022 This holiday season, make sure thanksgiving baskets for needy families near me customer research is one of the most essential tools in your UX toolkit.

    ReplyDelete