热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

Oracle11g新特性之只读表

在Oracle11g之前的版本中,若想对表设置只读,可以通过赋予SELECT对象权限给指定用户,但是表的拥有者仍然拥有读写权限。而Orac

在Oracle 11g之前的版本中,若想对表设置只读,可以通过赋予SELECT对象权限给指定用户,但是表的拥有者仍然拥有读写权限。而Orac

在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 - 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>

本文永久更新链接地址

推荐阅读
author-avatar
又未命名
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有