简体   繁体   English

什么是rowID&rowNum(ROWID vs ROWNUM)

[英]What is rowID & rowNum (ROWID vs ROWNUM)

I'd like to know difference between rowID and rowNUM 我想知道rowIDrowNUM之间的rowNUM

And how to see both of these in our table. 以及如何在我们的表中看到这两个。

when I execute this: 当我执行这个:

SELECT * FROM emp WHERE rownum=1

It returns one query but when I do the same for rowid it says 它返回一个查询,但是当我对rowid执行相同操作时,它说

inconsistent datatypes: expected ROWID got NUMBER 不一致的数据类型:预期ROWID获得NUMBER

And even in some of the tables, rownum returns null . 甚至在某些表中,rownum返回null。 Why so? 为什么这样?

Please clarify this: rowid vs rownum?(Demo query) 请澄清一下:rowid vs rownum?(演示查询)

Thank you 谢谢

EDIT: Require to use alias to display ROWID and ROWNUM (as they're pseudocolumn) like: 编辑:需要使用别名来显示ROWIDROWNUM (因为它们是伪列),如:

SELECT rownum r1, rowid r2 FROM emp

Both, rownum and rowed are pseudo columns. rownum和rowed都是伪列。

Rowid ROWID

For each row in the database, the ROWID pseudocolumn returns the address of the row. 对于数据库中的每一行,ROWID伪列返回行的地址。

An example query would be: 一个示例查询将是:

SELECT ROWID, last_name  
   FROM employees
   WHERE department_id = 20;

More info on rowid here: https://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm 有关rowid的更多信息,请访问: https//docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns008.htm

Rownum ROWNUM

For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. 对于查询返回的每一行,ROWNUM伪列返回一个数字,表示Oracle从一个表或一组连接行中选择行的顺序。 The first row selected has a ROWNUM of 1, the second has 2, and so on. 选择的第一行的ROWNUM为1,第二行的数量为2,依此类推。

You can limit the amount of results with rownum like this: 您可以使用rownum限制结果量,如下所示:

SELECT * FROM employees WHERE ROWNUM < 10;

More info on rownum here: https://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm 关于rownum的更多信息: https//docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm

Difference 区别

The actual difference between rowid and rownum is, that rowid is a permanent unique identifier for that row. rowidrownum之间的实际区别在于,rowid是该行的永久唯一标识符。 However, the rownum is temporary. 然而,rownum是暂时的。 If you change your query, the rownum number will refer to another row, the rowid won't. 如果更改查询,则rownum编号将引用另一行,而rowid则不会。

So the ROWNUM is a consecutive number which applicable for a specific SQL statement only. 因此ROWNUM是一个连续的数字,仅适用于特定的SQL语句。 In contrary the ROWID, which is a unique ID for a row. 相反,ROWID是一行的唯一ID。

Rownum (numeric) = Generated Sequence Number of your output. Rownum(数字) =输出的生成序列号。
Rowid (hexadecimal) = Generated automatically at the time of insertion of row. Rowid(十六进制) =在插入行时自动生成。

SELECT rowid,rownum fROM EMP


ROWID ROWNUM                 
----- ---------------------- 
AAAR4AAAFAAGzg7AAA, 1                      
AAAR4AAAFAAGzg7AAB, 2                      
AAAR4AAAFAAGzg7AAC, 3                      
AAAR4AAAFAAGzg7AAD, 4                      
AAAR4AAAFAAGzg7AAE, 5      
  1. Rowid gives the address of rows or records. Rowid给出行或记录的地址。 Rownum gives a count of records Rownum提供了一系列记录
  2. Rowid is permanently stored in the database. Rowid永久存储在数据库中。 Rownum is not stored in the database permanently Rownum不会永久存储在数据库中
  3. Rowid is automatically assigned with every inserted into a table. 每次插入表时,会自动为Rowid分配。 Rownum is a dynamic value automatically retrieved along with select statement output. Rownum是一个自动检索的动态值以及select语句输出。
  4. It is only for display purpose. 它仅用于显示目的。

row id shows the unique identification for row rownum shows the unique default series of numbers. 行ID显示行rownum的唯一identification显示唯一的默认数字系列。

select * from emp
where rownum<=5;   (it will execute correctly and gives output first 5 rows in your table )

select * from emp
where rowid<=5;  (wrong because rowid helpful to identify the unique value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM