Oracle的伪列

昨天做了一个Oracle PL/SQL 相关的测试,其中有一道这样的题目:

下列那些是Oracle的伪列(ACD) 
A、ROWID  
B、ROW_NUMBER() 
C、LEVEL 
D、ROWNUM 
E、COLUMN

虽然我坚持认为Oracle的技术是用来实际应用的,而非用于进行类似于出题,但是作为学生,考试必须是一个提升自己细节把控的最好途径。

那么什么是Oracle的伪列呢?寻遍能找到的中文资料,并没有对Oracle所谓的伪列有明确的解释,最终在Oracle官方文档的Oracle  Database SQL Language Reference 11g Release 2 (11.2) E41084-02找到了对伪劣的定义:

pseudocolumn behaves like a table column, but is not actually stored in the table. You can select from pseudocolumns, but you cannot insert, update, or delete their values. A pseudocolumn is also similar to a function without arguments . However, functions without arguments typically return the same value for every row in the result set, whereas pseudocolumns typically return a different value for each row.

大致的意思如下:

伪列的操作类似于表中的列,但是它并不存实际保存在表中。你可以对其进行查询操作,但是你却不能对其进行增加、修改或者是删除,一个伪列也类似于一个没有参数的函数。但是,没有参数的函数通常在结果集中为每一列返回相同的结果,伪列通常为每一列返回不同的值。

然后,Oracle的官方给出了如下几个伪列:

Hierarchical Query Pseudocolumns
Sequence Pseudocolumns
Version Query Pseudocolumns
COLUMN_VALUE Pseudocolumn
OBJECT_ID Pseudocolumn
OBJECT_VALUE Pseudocolumn
ORA_ROWSCN Pseudocolumn
ROWID Pseudocolumn
ROWNUM Pseudocolumn
XMLDATA Pseudocolumn

相关的地址放在这个地方,有需要继续深入研究的可以点击其相关的链接,继续看其详细介绍的部分:

但是我觉着分析函数ROW_NUMBER()的结果也应该算作一个伪列,例如分层查询当中的CONNECT_BY_ISCYCLE  或者是CONNECT_BY_ISLEAF 亦或是LEVEL 与分析函数ROW_NUMBER()OVER()有本质的区别么?

但是至少有一点是明确的,Oracle明确的说明ROWID属于其伪列的范畴:

ROWID Pseudocolumn

For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row:

The data object number of the object

The data block in the data file in which the row resides

The position of the row in the data block (first row is 0)

The data file in which the row resides (first file is 1). The file number is relative to the tablespace.

Usually, a rowid value uniquely identifies a row in the database. However, rows in different tables that are stored together in the same cluster can have the same rowid.

Values of the ROWID pseudocolumn have the data type ROWID or UROWID. Refer to "Rowid Data Types" and "UROWID Data Type" for more information.

Rowid values have several important uses:

They are the fastest way to access a single row.
They can show you how the rows in a table are stored.
They are unique identifiers for rows in a table.

You should not use ROWID as the primary key of a table. If you delete and reinsert a row with the Import and Export utilities, for example, then its rowid may change. If you delete a row, then Oracle may reassign its rowid to a new row inserted later.

Although you can use the ROWID pseudocolumn in the SELECT and WHERE clause of a query, these pseudocolumn values are not actually stored in the database. You cannot insert, update, or delete a value of the ROWID pseudocolumn.

(尽管你可以在SELECT..WHERE..查询语句中得到ROWID,但是该列的值并不是实际存储在数据库当中的,你不能够插入、修改或者删除ROWID伪列的值)

作者 陈字文(热衷于PM\ORACLE\JAVA等,欢迎同行交流)EMAIL:ziwen@163.com  QQ:409020100

之所以会感觉ROWID是实实在在存储在数据库当中,是因为受到两个方面的影响:

1、感觉在调优计划中ACCESS BY INDEX ROWID的速度是最快的,那么经验盒子告诉我,ROWID极有可能是存在数据块中的。
2、在数据块BLOCK的结构中,存在数据块头、表目录区、行目录区、可用空间区一级行数据区。不求甚解,理所当然认为行目录区中存放的就是ROWID的信息。现在回想一下,应该是存放了ROWID的部分相关信息,例如应该保存的是类似于C语言中的物理地址信息,而不是我们可见的形如ROWID的信息。

需要跳出自己的经验盒子,千万不能不求甚解。
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页