Oracle rowid

SYS@ prod>select rowid from scott.emp;

ROWID
------------------
AAASPXAAEAAAACVAAA
AAASPXAAEAAAACVAAB
AAASPXAAEAAAACVAAC
AAASPXAAEAAAACVAAD
AAASPXAAEAAAACVAAE 

ROWID的格式如下:

数据对象编号 文件编号 块编号 行编号
AAASPX AAE AAAACV AAA

我们可以看出,从上面的rowid可以得知:
AAASPX 是数据对象编号 (data_object)
AAE 是数据文件编号 (datafile)
AAAACV 是块编号 (block)
AAA 是行编号 (row)

怎么依据这些编号得到具体的十进制的编码值呢

这里需要明白rowid的是基于64位编码的18个字符显示(数据对象编号(6) +文件编号(3) +块编号(6)+ 行编号 (3)=18位),其中
A-Z <==> 0 - 25 (26)
a-z <==> 26 - 51 (26)
0-9 <==> 52 - 61 (10)
+/ <==> 62 - 63 (2)

共64位,明白这个后,就可以计算出10进制的编码值,计算公式如下:
d * (b ^ p)
其中:b就是基数,这里就是64,p就是从右到左,以0开始的位置数
比如:上面的例子
对象号AAASPX ,具体的计算应该是:
0*(64^5)=5;0*(64^4)=0;0*(64^3)=0;18*(64^2)=73728;15*(64^1)=960;23*(64^0)=23;

对象号就是73728+960+23=74711

刚才提到的是rowid的显示方式:基于64位编码的18个字符显示,其实rowid的存储方式是:10 个字节即80位存储,其中数据对象编号需要32 位,相关文件编号需要10 位,块编号需要22,位行编号需要16 位,由此,我们可以得出:
32bit的object number,每个数据库最多有4G个对象
10bit的file number,每个对象最多有1022个文件(2个文件预留)
22bit的block number,每个文件最多有4M个BLOCK
16bit的row number,每个BLOCK最多有64K个ROWS

2. rowid相关的有用的sql

通过dbms_rowid这个包,可以直接的得到具体的rowid包含的信息:

SYS@ prod>select dbms_rowid.rowid_object(rowid) object_id, dbms_rowid.rowid_relative_fno(rowid) file_id,
  2  dbms_rowid.rowid_block_number(rowid) block_id ,dbms_rowid.rowid_row_number(rowid) num from scott.emp 
  3  where rownum<5;

 OBJECT_ID    FILE_ID   BLOCK_ID        NUM
---------- ---------- ---------- ----------
     74711          4        149          0
     74711          4        149          1
     74711          4        149          2
     74711          4        149          3 

一些使用ROWID的函数
ROWIDTOCHAR(rowid) :将ROWID转换成STRING
CHARTOROWID('rowid_string') :将STRING转换成ROWID

如果没有dbms包的权限可以用下面的方法

select
(case when to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,1,1)),'xxx') = 47 then 63 end)*power(64,5)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,2,1)),'xxx') = 47 then 63 end)*power(64,4)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,3,1)),'xxx') = 47 then 63 end)*power(64,3)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,4,1)),'xxx') = 47 then 63 end)*power(64,2)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,5,1)),'xxx') = 47 then 63 end)*power(64,1)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,6,1)),'xxx') = 47 then 63 end)*power(64,0) as object_cd,
(case when to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,7,1)),'xxx') = 47 then 63 end)*power(64,2)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,8,1)),'xxx') = 47 then 63 end)*power(64,1)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,9,1)),'xxx') = 47 then 63 end)*power(64,0) as file_no,
(case when to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,10,1)),'xxx') = 47 then 63 end)*power(64,5)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,11,1)),'xxx') = 47 then 63 end)*power(64,4)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,12,1)),'xxx') = 47 then 63 end)*power(64,3)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,13,1)),'xxx') = 47 then 63 end)*power(64,2)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,14,1)),'xxx') = 47 then 63 end)*power(64,1)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,15,1)),'xxx') = 47 then 63 end)*power(64,0) as block_no,
(case when to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,16,1)),'xxx') = 47 then 63 end)*power(64,2)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,17,1)),'xxx') = 47 then 63 end)*power(64,1)+
(case when to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx') between 48 and 58 then to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx')+4 when to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx') between 65 and 91 then to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx') between 97 and 123 then to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx')-65 when to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx') = 43 then 62 when to_number(utl_raw.cast_to_raw(substr(rowid,18,1)),'xxx') = 47 then 63 end)*power(64,0) as row_no
from scott.emp;

 

posted @ 2019-05-27 15:45  赵春义  阅读(424)  评论(0编辑  收藏  举报