Welcome 微信登录

首页 / 数据库 / MySQL / Oracle 11g新特性之只读表

在Oracle 11g之前的版本中,若想对表设置只读,可以通过赋予SELECT对象权限给指定用户,但是表的拥有者仍然拥有读写权限。而Oracle 11g 允许通过ALTER  TABLE 命令将表标记为只读(read-only)。只读表跟普通的表没有区别,只是不允许任何事务对其执行任何 DML(Insert, Update, Delete) 操作。测试环境
    我们在Oracle11g(11.2.0.3)进行测试。SQL>SQL> select * from v$version;BANNER--------------------------------------------------------------------------------Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ProductionPL/SQL Release 11.2.0.3.0 - ProductionCORE 11.2.0.3.0 ProductionTNS for Linux: Version 11.2.0.3.0 - ProductionNLSRTL Version 11.2.0.3.0 - ProductionSQL> 创建测试表
    我们创建一个测试表,命名为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 linuxidcSQL>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 YESSQL>
 执行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 NOSQL>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 linuxidcSQL>更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址