【TABLE】11g中只讀表(Read-only Table)技術特性

secooler發表於2010-05-09
Oracle 11g中可以將一個表從普通表修改為只讀表,在這種只讀狀態下,表的操作被限制,具體的限制如下:
不允許增刪改DML操作;
不允許select ... for update悲觀鎖定;
不允許影響只讀表T資料的DDL操作;
允許索引操作。

具體看一下只讀狀態下的表的行為特徵。

1.建立測試表T並初始化一條資料
sec@11gR2> create table t (x varchar2(8));

Table created.

sec@11gR2> insert into t values ('secooler');

1 row created.

sec@11gR2> commit;

Commit complete.

2.使表T處於只讀狀態
sec@11gR2> alter table t read only;

Table altered.

3.驗證只讀表不允許DML操作
1)插入操作不允許
sec@11gR2> insert into t values ('HOU');
insert into t values ('HOU')
            *
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

2)刪除操作不允許
sec@11gR2> delete from t;
delete from t
            *
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

3)更新操作不允許
sec@11gR2> update t set x = 'HOU';
update t set x = 'HOU'
       *
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

4.驗證只讀表不可悲觀鎖定
sec@11gR2> select * from t for update;
select * from t for update
              *
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

5.影響只讀表T資料的DDL操作不允許
1)增加新欄位的DDL操作不允許
sec@11gR2> alter table t add y varchar2(8);
alter table t add y varchar2(8)
*
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

2)修改欄位型別操作不允許
sec@11gR2> alter table t modify x varchar2(10);
alter table t modify x varchar2(10)
*
ERROR at line 1:
ORA-12081: update operation not allowed on table "SEC"."T"

6.只讀表上允許索引類操作
1)為只讀表建立索引
sec@11gR2> create index i_t on t(x);

Index created.

2)刪除只讀表上的索引
sec@11gR2> drop index i_t;

Index dropped.

7.USER_TABLES中的READ_ONLY欄位指示表的只讀狀態
如果read_only欄位是“YES”表示是隻讀表,如果是“NO”表示非只讀表
sec@11gR2> select table_name,read_only from user_tables;

TABLE_NAME                     REA
------------------------------ ---
T                              YES

8.恢復只讀表為普通表
sec@11gR2> alter table t read write;

Table altered.

sec@11gR2> select table_name,read_only from user_tables;

TABLE_NAME                     REA
------------------------------ ---
T                              NO

9.小結
Oracle在11g之前的版本中實現只讀表的方法是透過授權間接實現的,影響面很大。使用文中介紹的只讀表(read-only table)技術我們就掌握了細粒度控制錶行為的方法。

Good luck.

secooler
10.05.09

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-662308/,如需轉載,請註明出處,否則將追究法律責任。

相關文章