1.关于ROWID
在用户向表中插入一行数据时,ORACLE会自动在这一行数据加上一个ROWID,每行都有一个唯一ROWID,ORACLE利用ROWID定位数据行。ROWID并不显式存储为一列的值(伪列--不是存在表中的实际数据,可能是内部采用函数什么的根据行所在块的信息转换的),是访问一个表中行的最快机制。索引中存储的有索引行的值及索引行的ROWID的值--实际数据。
ORACLE ROWID分为物理ROWID,逻辑ROWID。
索引组织表(IOTs)使用逻辑ROWID,其它类型的表使用物理ROWID。
ROWID采用Base64编码,共18位代表80位二进制数,占用10个字节。
每组字符代表不同的含义,18位最大寻址空间“32G”
对一条行ID的解析:AAASKG.AAF.AAAAWz.AAA --.是为了方便观看手动增加
AAASKG: 1-6位:对象id
AAF: 7-9位:文件id
AAAAWz: 10-15位:块id
AAA: 16-18位:行id
对于Base64编码,共18位代表80位二进制数,计算方法是:
32bit obj# + 10bit file# + 22bit block# + 16bit row#
关于通过ROWID计算数据库中对表空间、数据文件、数据库中行数等的一些限制之类,详见:http://blog.csdn.net/q947817003/article/details/16847005
2.查询表中某行数据的ROWID
BYS@ bys3>select * from test;
--test表信息如下:
OBJECT_NAME OBJECT_ID STATUS
------------ ---------- -------
UNDO$ 15 VALID
ICOL$ 20 VALID
CON$ 28 VALID
I_USER1 46 VALID
查询ROWID相关信息有如下两个语句可用:
select rowid,dbms_rowid.rowid_object(rowid) object_id,dbms_rowid.rowid_relative_fno(rowid) file_id,dbms_rowid.rowid_block_number(rowid) block_id,dbms_rowid.rowid_row_number(rowid) row_id from test1;
select a.rowid,a.object_id,a.file_id,a.block_id,a.row_id,b.object_name from (select rowid,dbms_rowid.rowid_object(rowid) object_id,dbms_rowid.rowid_relative_fno(rowid) file_id,dbms_rowid.rowid_block_number(rowid) block_id,dbms_rowid.rowid_row_number(rowid) row_id from test1) a,dba_objects b where a.object_id=b.object_id;
或者使用如下语句查询ROWID及表中一列的信息(方便查看对照具体行):
--使用了变量可以输入表名或列名
col object_name for a12
col colname for a10
select a.rowid,a.object_id,a.file_id,a.block_id,a.row_num,b.object_name,a.colname from
(select rowid,
dbms_rowid.rowid_object(rowid) object_id,
dbms_rowid.rowid_relative_fno(rowid) file_id,
dbms_rowid.rowid_block_number(rowid) block_id,
dbms_rowid.rowid_row_number(rowid) row_num,
&colname as colname from &tablename t) a,
dba_objects b
where a.object_id=b.object_id;
运行上述语句,按提示输入:&colname 列名 ;&tablename 表名即可显示类似以下信息: 我这里是输入 test 表的object_name列 -
--注意,如果&colname 列名输入的列是NUMBER类型的数据,要在col colname for a10中改一下,改为:col colname for 999.999.999 不然数字无法显示,会显示成#######
输入的列名对应的显示字段名是:COLNAME,,,输入的表名对应的显示字段是:OBJECT_NAME
ROWID OBJECT_ID FILE_ID BLOCK_ID ROW_NUM OBJECT_NAME COLNAME
------------------ ---------- ---------- ---------- ---------- ------------ ----------
AAAFSJAAEAAAACrAAD 21641 4 171 3 TEST I_USER1
AAAFSJAAEAAAACrAAC 21641 4 171 2 TEST CON$
AAAFSJAAEAAAACrAAB 21641 4 171 1 TEST ICOL$
AAAFSJAAEAAAACrAAA 21641 4 171 0 TEST UNDO$
AAAFSJAAEAAAACrAAD----这个ROWID标记下,将与下一步实验的对照
附:dbms_rowid.ROWID_INFO Procedure Parameters ---ROWID_INFO进程各参数意义,来自官方文档
rowid_in ----ROWID to be interpreted. This determines if the ROWID is a restricted (0) or extended (1) ROWID.
ts_type_in ----The type of the tablespace (bigfile/smallfile) to which the row belongs.
rowid_type -----Returns type (restricted/extended).
object_number ----Returns data object number (rowid_object_undefined for restricted).
relative_fno -----Returns relative file number.
block_number -----Returns block number in this file.
row_number ----Returns row number in this block.
3.依次根据OBJECT_ID,FILE_ID,BLOCK_ID,ROW_NUM来查询一行数据的ROWID
关于DBMS_ROWID.ROWID_CREATE 大致如下: --来自官方文档-附有
.ROWID_CREATE的 各字段的解释:
DBMS_ROWID.ROWID_CREATE (
rowid_type IN NUMBER,
object_number IN NUMBER,
relative_fno IN NUMBER,
block_number IN NUMBER,
row_number IN NUMBER)
RETURN ROWID;
rowid_type ----Type (restricted or extended).Set the rowid_type parameter to 0 for a restricted ROWID. Set it to 1 to create an extended ROWID.If you specify rowid_type as 0, then the required object_number parameter is ignored, and ROWID_CREATE returns a restricted ROWID.
object_number ----Data object number (rowid_object_undefined for restricted).
relative_fno ----Relative file number.
block_number ----Block number in this file.
row_number ------Returns row number in this block.
SYS@ bys3>select DBMS_ROWID.ROWID_CREATE(1,21641, 4, 171, 3) from dual;
--使用上一步查出的值为: I_USER1的列的对象号,FILE_ID,BLOCK_ID等信息,
DBMS_ROWID.ROWID_C
------------------
AAAFSJAAEAAAACrAAD ----这个ROWID与上一步的可以对照上
4.通过文件号、块号查出对象号--文件号4,块号253
SYS@ bys3>select a.objd,b.object_name,b.owner from v$bh a,dba_objects b where a.objd=b.object_id and file#=4 and BLOCK#=253;
OBJD OBJECT_NAM OWNER
---------- ---------- ------------------------------
22327 DEPT BYS
还有一种方法是结合X$BH查,需要SYS权限。用V$BH就好了。 另一种是DUMP 这个数据块,在数据块的BH中就有对象信息。