Exforsys

H I D E

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle 11g Read Only Tables Page - 2

Page 2 of 2
Author: Saurabh Gupta     Published on: 16th Jun 2011

Oracle 11g Read Only Tables

READ ONLY: Notes

As the name suggests and I stated earlier too, the READ-ONLY tables are only query tables. Except the SELECT and DROP commands, DML and DDL commands are ineffective in this mode.

Here, we shall see the impact of DML operations on the table T_READ. Oracle raises exception ORA-12081 to restrict the DML operations on the table.

Code [4]
Sample Code
  1. SQL> INSERT INTO T_READ VALUES (1, 'INSERT');
  2. INSERT INTO T_READ VALUES (1, 'INSERT')
  3.             *
  4. ERROR at line 1:
  5. ORA-12081: UPDATE operation NOT allowed ON TABLE "SCOTT"."T_READ"
Copyright exforsys.com


Likewise INSERT statement; same exception would be raised for UPDATE and DELETE operations on the table T_READ. Similar to DML operations, few DDL operations like TRUNCATE and ALTER are also restricted for their user.

Code [5]
Sample Code
  1. SQL> TRUNCATE TABLE T_READ;
  2. TRUNCATE TABLE T_READ
  3.                *
  4. ERROR at line 1:
  5. ORA-12081: UPDATE operation NOT allowed ON TABLE "SCOTT"."T_READ"
Copyright exforsys.com


Note the exception number and error message is same for all the restricted actions, DML or DDL on the table.

The ALTER command below attempts to add a column COL3 to the T_READ. The ALTER command fails since T_READ is Read-Only table.

Code [6]
Sample Code
  1. SQL> ALTER TABLE T_READ
  2.      ADD COL3 NUMBER;
  3. ALTER TABLE T_READ
  4. *
  5. ERROR at line 1:
  6. ORA-12081: UPDATE operation NOT allowed ON TABLE "SCOTT"."T_READ"
Copyright exforsys.com


But the DDL commands, which are not concerned with the data and structure of the table execute successfully. The ANALYZE, RENAME, and DROP commands execute successfully on READ-ONLY tables.

Code [7]
Sample Code
  1. SQL> ANALYZE TABLE T_READ COMPUTE STATISTICS;
  2.  
  3. TABLE analyzed.
  4.  
  5. SQL> RENAME T_READ1 TO T_READ;
  6.  
  7. TABLE renamed.
Copyright exforsys.com


Indexes can be created on the columns of READ-ONLY tables. These indexes are used by the optimizer in SELECT statements using READ-ONLY tables.

Code [8]
Sample Code
  1. SQL> CREATE INDEX IDX_READ ON T_READ (COL1);
  2.  
  3. INDEX created.
Copyright exforsys.com


Similarly, partitions can be created, DDL commands like DROP

READ-ONLY Mode Switch

A table in READ-ONLY mode can be switched back to READ WRITE mode using ALTER TABLE command.

Code [9]
Sample Code
  1. SQL> ALTER TABLE T_READ READ WRITE;
  2.  
  3. TABLE altered.
Copyright exforsys.com


The READ_ONLY status of the T_READ table in USER_TABLES is changed to ‘NO’.

Code [10]
Sample Code
  1. SQL> SELECT TABLE_NAME, READ_ONLY FROM USER_TABLES WHERE TABLE_NAME='T_READ';
  2.  
  3. TABLE_NAME                     REA
  4. ------------------------------ ---
  5. T_READ                         NO
Copyright exforsys.com


Since T_READ is now a normal read/write supported table, data can be easily inserted, modified or deleted.

Code [11]
Sample Code
  1. SQL> INSERT INTO T_READ VALUES (1,'INSERT');
  2.  
  3. 1 row created.
  4.  
  5. SQL> INSERT INTO T_READ VALUES (2,'INSERT');
  6.  
  7. 1 row created.
  8.  
  9. SQL> UPDATE T_READ
  10.      SET COL2 = 'INSERT'||COL1;
  11.  
  12. 2 rows updated.
  13.  
  14. SQL> COMMIT;
  15.  
  16. Commit complete.
  17.  
Copyright exforsys.com


The mode can be toggled over to READ-ONLY mode to restrict further data changes.

Code [12]
Sample Code
  1. SQL> ALTER TABLE T_READ READ ONLY;
  2.  
  3. TABLE altered.
  4.  
  5. SQL> SELECT * FROM T_READ;
  6.  
  7.       COL1 COL2
  8. ---------- ----------
  9.          1 INSERT1
  10.          2 INSERT2
  11.  
Copyright exforsys.com


Applications of Read-Only Tables

Read-Only tables provide a secure way to ensure data integrity and data access. On a highly secure production system, a table containing confidential data can be declared READ ONLY, so as to make sure that data can only be selected, but not frequently modified.

Read Next: Oracle 11g Subprogram Overloading


 
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.

Oracle 11g Tutorial

 

Comments