Oracle 11g新特性之只读表

2015-08-31 19:59:48 · 作者: · 浏览: 34

测试环境
? ? 我们在Oracle11g(11.2.0.3)进行测试。


SQL>


SQL> select * from v$version;


BANNER


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


Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production


PL/SQL Release 11.2.0.3.0 - Production


CORE 11.2.0.3.0 Production


TNS for Linux: Version 11.2.0.3.0 - Production


NLSRTL Version 11.2.0.3.0 - Production


SQL>


?


创建测试表
? ? 我们创建一个测试表,命名为linuxidc;然后,插入两条测试数据。


SQL>


SQL> create table linuxidc(id number,name varchar2(20));


Table created.


SQL> insert into linuxidc values(1,'linuxidc');


1 row created.


SQL> insert into linuxidc values(10,'linuxidc');


1 row created.


SQL> commit;


Commit complete.


SQL> select * from linuxidc;


? ? ? ? ID NAME


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


? ? ? ? 1 linuxidc


? ? ? ? 10 linuxidc


SQL>


SQL>
?


将普通表设为只读表
? ? 我们通过alter table ... read only;语句来实现只读表;而且,我们可以通过数据字典视图 (ALL_TABLES,DBA_TABLES,USER_TABLES,TABS)中的 READ_ONLY 列查询表的只读属性,如下所示:


SQL>


SQL> alter table linuxidc read only;


Table altered.


SQL>


SQL> select table_name ,read_only from user_tables;


TABLE_NAME REA


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


linuxidc YES


SQL>
?


执行DML语句报错
?
? ? 只读表不允许任何事务对其执行任何 DML(Insert, Update, Delete) 操作,否则系统会报ORA-12081错误,提示操作不被允许。


SQL>


SQL> insert into linuxidc values(100,'linuxidc');


insert into linuxidc values(100,'linuxidc')


? ? ? ? ? ? *


ERROR at line 1:


ORA-12081: update operation not allowed on table "linuxidc"."linuxidc"


SQL>


SQL> update linuxidc set id=100 where id=10;


update linuxidc set id=100 where id=10


? ? ? *


ERROR at line 1:


ORA-12081: update operation not allowed on table "linuxidc"."linuxidc"


SQL>


SQL> delete from linuxidc where id=10;


delete from linuxidc where id=10


? ? ? ? ? ? *


ERROR at line 1:


ORA-12081: update operation not allowed on table "linuxidc"."linuxidc"


SQL>
?


执行TRUNCATE语句报错
? ? 只读表除了不能执行所有DML语句操作外,部分DDL语句也不能执行,比如TRUNCATE,否则系统同样会报ORA-12081错误,提示操作不被允许。


SQL>


SQL> truncate table linuxidc;


truncate table linuxidc


? ? ? ? ? ? ? *


ERROR at line 1:


ORA-12081: update operation not allowed on table "linuxidc"."linuxidc"


SQL>
?


执行DROP语句成功
?


? ? 针对只读表的DROP操作,是被允许的。


SQL> drop table linuxidc;


Table dropped.


SQL>
?


将只读表设为普通表
? ? 我们通过alter table ... read write;语句来实现将只读表设为普通读写表。参看下面SQL语句:


SQL>


SQL> alter table linuxidc read write;


Table altered.


SQL> select table_name ,read_only from user_tables;


TABLE_NAME REA


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


linuxidc NO


SQL>


SQL> insert into linuxidc values(100,'linuxidc');


1 row created.


SQL> commit;


Commit complete.


SQL> update linuxidc set id=11 where id=10;


1 row updated.


SQL> commit;


Commit complete.


SQL> select * from linuxidc;


? ? ? ? ID NAME


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


? ? ? ? 1 linuxidc


? ? ? ? 11 linuxidc


? ? ? 100 linuxidc


SQL>