Oracle僞列

oracle10g11g下,僞列包括以下內容: session

 

l        Hierarchical Query Pseudocolumns oracle

 

分級查詢是oracle提供的遞歸查詢語法,在這裏不作展開。只有在分級查詢下,才能夠使用如下僞列: 函數

    1.CONNECT_BY_ISCYCLE Pseudocolumn 編碼

     2.CONNECT_BY_ISLEAF Pseudocolumn spa

    3.LEVEL Pseudocolumn orm

 

l        Sequence Pseudocolumns 遞歸

 

Sequence有如下2個僞列: 事務

    1.CURRVAL:返回當前sequence ci

    2.NEXTVAL:增長sequence並返回下一個值 get

    通常用法:

    sequence.CURRVAL

    sequence.NEXTVAL

 

l        Version Query Pseudocolumns

 

Version Query僞列只有在Flashback Version Query時纔有效,內容以下:

    1.VERSIONS_STARTSCN and VERSIONS_STARTTIME

    2.VERSIONS_ENDSCN and VERSIONS_ENDTIME

    3.VERSIONS_XID

    4.VERSIONS_OPERATION

 

l        COLUMN_VALUE Pseudocolumn

l        OBJECT_ID Pseudocolumn

l        OBJECT_VALUE Pseudocolumn

l        ORA_ROWSCN Pseudocolumn

l        ROWID Pseudocolumn

l        ROWNUM Pseudocolumn

l        XMLDATA Pseudocolumn

 

在對普通表作查詢時,比較經常使用的僞列有:ORA_ROWSCNROWIDROWNUM

ORA_ROWSCN

雖然叫ORA_ROWSCN,不過默認狀況下,查詢出的該值是從數據文件塊頭獲取的,也就是說,查詢出的是block的最近事務的scn,而不是精確到rowscn

在建立表時,能夠指定ROWDEPENDENCIES來使ora_rowscn真正記錄行一級的scn

 

看一下ROWDEPENDENCIES 的定義:Specify ROWDEPENDENCIES if you want to enable row-level dependency tracking. This setting is useful primarily to allow for parallel propagation in replication environments. It increases the size of each row by 6 bytes.

 

從這裏也能夠看出一個SCN佔用的空間(6 bytes)。SCN的最大值是0xffff.ffffffff,共48位。包括2bytes的高位字節(SCN wrap)和4bytes的低位字節(SCN base)。

SQL>select ora_rowscn,username from t;

 

ORA_ROWSCN USERNAME

---------- ------------------------------

 86516279 SYSTEM

 86516279 SYS

 86516279 OUTLN

 86516279 LINCINQ

 86516279 TEST

 86516279 LINC

 86516279 SPLEXUC

 86516279 DIP

  86516279 TSMSYS

 86516279 WMSYS

 86516279 DBSNMP

11 rows selected.

 

須要查詢scn對於的具體時間,能夠用函數SCN_TO_TIMESTAMP

SQL>alter session set nls_timestamp_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

 

SQL>select scn_to_timestamp(ora_rowscn),username from t;

SCN_TO_TIMESTAMP(ORA_ROWSCN)  USERNAME

------------------------------ ------------------------------

2011-06-01 13:13:31           SYSTEM

2011-06-01 13:13:31           SYS

2011-06-01 13:13:31           OUTLN

2011-06-01 13:13:31           LINCINQ

2011-06-01 13:13:31           TEST

2011-06-01 13:13:31           LINC

2011-06-01 13:13:31           SPLEXUC

2011-06-01 13:13:31           DIP

2011-06-01 13:13:31           TSMSYS

2011-06-01 13:13:31           WMSYS

2011-06-01 13:13:31           DBSNMP

11 rows selected.

  

ROWID

 

Rowid是基於64位編碼的18個字符,格式以下:

data_object_id       file_id      block_number  row_number

OOOOOO      FFF      BBBBBB       RRR

 

SQL>select rowid,username from t;

ROWID             USERNAME

------------------ ------------------------------

AAAE0LAAOAABQqMAAA SYSTEM

AAAE0LAAOAABQqMAAB SYS

AAAE0LAAOAABQqMAAC OUTLN

AAAE0LAAOAABQqMAAD LINCINQ

AAAE0LAAOAABQqMAAE TEST

AAAE0LAAOAABQqMAAF LINC

AAAE0LAAOAABQqMAAG SPLEXUC

AAAE0LAAOAABQqMAAH DIP

AAAE0LAAOAABQqMAAI TSMSYS

AAAE0LAAOAABQqMAAJ WMSYS

AAAE0LAAOAABQqMAAK DBSNMP

11 rows selected.

 

經過dbms_rowid包,咱們能夠得到rowid對應的記錄詳細信息。

SQL>select dbms_rowid.rowid_object('&1') data_object_id#,

 2        dbms_rowid.rowid_relative_fno('&1') rfile#,

 3        dbms_rowid.rowid_block_number('&1') block#,

 4        dbms_rowid.rowid_row_number('&1') row# from dual;

Enter value for 1: AAAE0LAAOAABQqMAAA

old  1: select dbms_rowid.rowid_object('&1') data_object_id#,

new  1: select dbms_rowid.rowid_object('AAAE0LAAOAABQqMAAA') data_object_id#,

Enter value for 1: AAAE0LAAOAABQqMAAA

old  2:       dbms_rowid.rowid_relative_fno('&1') rfile#,

new  2:       dbms_rowid.rowid_relative_fno('AAAE0LAAOAABQqMAAA') rfile#,

Enter value for 1: AAAE0LAAOAABQqMAAA

old  3:       dbms_rowid.rowid_block_number('&1') block#,

new  3:       dbms_rowid.rowid_block_number('AAAE0LAAOAABQqMAAA') block#,

Enter value for 1: AAAE0LAAOAABQqMAAA

old  4:       dbms_rowid.rowid_row_number('&1') row# from dual

new  4:       dbms_rowid.rowid_row_number('AAAE0LAAOAABQqMAAA') row# from dual

DATA_OBJECT_ID#    RFILE#    BLOCK#      ROW#

--------------- ---------- ---------- ----------

         19723        14    330380         0

 

ROWNUM

 

在對錶的查詢中,每返回一條記錄,rownum僞列就返回一個數字,表明查詢返回的行的編號。

SQL>select rownum,username from t;

   ROWNUM USERNAME

---------- ------------------------------

        1 SYSTEM

        2 SYS

        3 OUTLN

        4 LINCINQ

        5 TEST

        6 LINC

        7 SPLEXUC

        8 DIP

        9 TSMSYS

       10 WMSYS

       11 DBSNMP

11 rows selected.

 

從下面的例子能夠看出,rownum返回的是查詢過程當中返回記錄的順序,並非查詢結果的序列號。

SQL>select rownum,username from t order by username;

   ROWNUM USERNAME

---------- ------------------------------

       11 DBSNMP

        8 DIP

        6 LINC

        4 LINCINQ

        3 OUTLN

        7 SPLEXUC

        2 SYS

        1 SYSTEM

        5 TEST

        9 TSMSYS

       10 WMSYS

11 rows selected.

 

SQL>select rownum,username from (select username from t order by username);

   ROWNUM USERNAME

---------- ------------------------------

        1 DBSNMP

        2 DIP

        3 LINC

        4 LINCINQ

        5 OUTLN

        6 SPLEXUC

        7 SYS

        8 SYSTEM

        9 TEST

       10 TSMSYS

       11 WMSYS

11 rows selected.

相關文章
相關標籤/搜索