Read-Only Tables in Oracle Database 11g

In previous Oracle releases, tables could be made to appear read-only to other users by only granting the SELECT object privilege to them, but the tables remained read-write for the owner. Oracle 11g allows tables to be marked as read-only using the ALTER TABLE command.

ALTER TABLE table_name READ ONLY;
ALTER TABLE table_name READ WRITE;

The following script creates a table, inserts a row, then sets the table to read-only.

CREATE TABLE ro_tab (
  id  NUMBER
);

INSERT INTO ro_tab VALUES (1);
ALTER TABLE ro_tab READ ONLY;

Any DML statements that affect the table data and SELECT ... FOR UPDATE queries result in an ORA-12081 error message.

SQL> INSERT INTO ro_tab VALUES (2);
INSERT INTO ro_tab VALUES (2)
            *
ERROR at line 1:
ORA-12081: update operation not allowed on table "TEST"."RO_TAB"


SQL> UPDATE ro_tab SET id = 2;
UPDATE ro_tab SET id = 2
       *
ERROR at line 1:
ORA-12081: update operation not allowed on table "TEST"."RO_TAB"


SQL> DELETE FROM ro_tab;
DELETE FROM ro_tab
            *
ERROR at line 1:
ORA-12081: update operation not allowed on table "TEST"."RO_TAB"

DDL statements that affect the table data are also restricted,但是只读模式的表可以用drop命令删除。

SQL> TRUNCATE TABLE ro_tab;
TRUNCATE TABLE ro_tab
               *
ERROR at line 1:
ORA-12081: update operation not allowed on table "TEST"."RO_TAB"


SQL> ALTER TABLE ro_tab ADD (description VARCHAR2(50));
ALTER TABLE ro_tab ADD (description VARCHAR2(50))
*
ERROR at line 1:
ORA-12081: update operation not allowed on table "TEST"."RO_TAB"

Operations on indexes associated with the table are unaffected by the read-only state. DML and DDL operations return to normal once the table is switched back to read-write mode.

SQL> ALTER TABLE ro_tab READ WRITE;

Table altered.

SQL> DELETE FROM ro_tab;

1 row deleted.

SQL>

The read-only status of tables is displayed in the READ_ONLY column of the [DBA|ALL|USER]_TABLES views.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值