ROWNUM returns a number indicating the sequence in which a row was selected from a table. The first row selected has a ROWNUM of 1, the second row has a ROWNUM of 2, and so on. If a SELECT statement includes an ORDER BY clause, ROWNUMs are assigned to the selected rows before the sort is done.
You can use ROWNUM to limit the number of rows returned by a SELECT statement. Also, you can use ROWNUM in an UPDATE statement to assign unique values to each row in a table. Using ROWNUM in the WHERE clause does not stop the processing of a SELECT statement; it just limits the number of rows retrieved. The only meaningful use of ROWNUM in a WHERE clause is
... WHERE ROWNUM < constant;
because the value of ROWNUM increases only when a row is retrieved. The following search condition can never be met because the first four rows are not retrieved:
... WHERE ROWNUM = 5;
-----------------------------------------------------------------------------------------
The ROWID Pseudocolumn
Each table in an Oracle database internally has a pseudocolumn named
ROWID. This pseudocolumn is not evident when listing the structure of a table by executing aSELECT*FROM... statement, or aDESCRIBE... statement using SQL*Plus, nor does the pseudocolumn take up space in the table. However, each row's address can be retrieved with a SQL query using the reserved wordROWIDas a column name, for example:SELECT ROWID, last_name FROM employees;You cannot set the value of the pseudocolumn
ROWIDinINSERTorUPDATEstatements, and you cannot delete aROWIDvalue. Oracle uses theROWIDvalues in the pseudocolumnROWIDinternally for the construction of indexes.You can reference rowids in the pseudocolumn
ROWIDlike other table columns (used inSELECTlists andWHEREclauses), but rowids are not stored in the database, nor are they database data. However, you can create tables that contain columns having theROWIDdatatype, although Oracle does not guarantee that the values of such columns are valid rowids. The user must ensure that the data stored in theROWIDcolumn truly is a validROWID.
How Rowids Are Used
Oracle uses rowids internally for the construction of indexes. Each key in an index is associated with a rowid that points to the associated row's address for fast access. End users and application developers can also use rowids for several important functions:
-
Rowids are the fastest means of accessing particular rows.
-
Rowids can be used to see how a table is organized.
-
Rowids are unique identifiers for rows in a given table.
Before you use rowids in DML statements, they should be verified and guaranteed not to change. The intended rows should be locked so they cannot be deleted. Under some circumstances, requesting data with an invalid rowid could cause a statement to fail.
You can also create tables with columns defined using the ROWID datatype. For example, you can define an exception table with a column of datatype ROWID to store the rowids of rows in the database that violate integrity constraints. Columns defined using the ROWID datatype behave like other table columns: values can be updated, and so on. Each value in a column defined as datatype ROWID requires six bytes to store pertinent column data.
本文介绍了Oracle数据库中ROWNUM和ROWID的功能和用途。ROWNUM用于标识从表中选择行的顺序,并可用于限制SELECT语句返回的行数。ROWID作为伪列,存储每行的地址信息,用于快速访问特定行,也可用于查看表组织结构和作为唯一标识符。
1632

被折叠的 条评论
为什么被折叠?



