Exforsys

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle 11g Read Only Tables

Page 1 of 2
Author:      Published on: 16th Jun 2011

Before the release of Oracle 11g, READ-ONLY mode could only be associated with the database and tablespace. Oracle 11g extends this feature by introducing behavioral modes for database tables.

A Read-Only table is a normal data storage table, which restricts any transactional action on it. In the tutorial, we shall study the behavior of READ-ONLY tables under different scenarios.

Read-Only tables: History

Prior to Oracle 11g, all tables were used to be transactional. The table owner and the privileged users used to perform DML and DDL operations on the table. Possible way to impose restriction on transactions was through check constraint method or through triggers. We shall see them as below.

Method 1: DML trigger method

A table TAB_ORCL_10G was created. A Before DML trigger TRG_ORCL_10G was created to restrict the DML operations on the table. Note that DDL commands are still executed on the table.

Refer the illustration below.

Sample Code
  1. SQL> CREATE TABLE TAB_ORCL_10G
  2.      (A NUMBER, B NUMBER);
  3.  
  4. TABLE created.
  5.  
  6. SQL> CREATE OR REPLACE TRIGGER TRG_ORCL_10G
  7.      BEFORE INSERT OR UPDATE OR DELETE
  8.      ON TAB_ORCL_10G
  9.      BEGIN
  10.      RAISE_APPLICATION_ERROR (-20001, 'OPERATION RESTRICTED.');
  11.      END;
  12.      /
  13.  
  14. TRIGGER created.
  15.  
  16. SQL> INSERT INTO TAB_ORCL_10G VALUES(1,3);
  17. INSERT INTO TAB_ORCL_10G VALUES(1,3)
  18.             *
  19. ERROR at line 1:
  20. ORA-20001: OPERATION RESTRICTED.
  21. ORA-06512: at "SCOTT. TRG_ORCL_10G ", line 2
  22. ORA-04088: error during execution of TRIGGER SCOTT. TRG_ORCL_10G
Copyright exforsys.com


Method 2: Disable Validate Constraint method

The second method creates a table and a check constraint in disable validated state. Note that the DDL commands like ALTER, TRUNCATE and DROP are executed successfully.

Sample Code
  1. SQL> CREATE TABLE TAB_ORCL_10G
  2.      (A NUMBER, B NUMBER);
  3.  
  4. TABLE created.
  5.  
  6. SQL> ALTER TABLE TAB_ORCL_10G ADD CONSTRAINT CHECK_CONST CHECK(1=1) DISABLE VALIDATE;
  7.  
  8. TABLE altered.
  9.  
  10. SQL> INSERT INTO TAB_ORCL_10G VALUES(1,3);
  11. INSERT INTO TAB_ORCL_10G VALUES(1,3)
  12. *
  13. ERROR at line 1:
  14. ORA-25128: No INSERT/UPDATE/DELETE ON TABLE WITH constraint
  15. (SCOTT.TEST_READ_ONLY) disabled AND validated
  16.  
  17. SQL> TRUNCATE TABLE t_read_ol2;
  18. TRUNCATE TABLE t_read_ol2
  19. *
  20. ERROR at line 1:
  21. ORA-25128: No INSERT/UPDATE/DELETE ON TABLE WITH constraint
  22. (CCPDEV.TEST_READ_ONLY) disabled AND validated
Copyright exforsys.com


Read-Only tables: the Purpose

In spite of smooth feature flow, Oracle considered both the workaround solutions as implementation overhead. Very soon, this consideration was molded into action because read only is an object property and it must not be imposed with a workaround solution.

With 11g release, a table can be established in READ-ONLY mode. The mode restricts all DML operations and DDL operations (TRUNCATE and ALTER). Surely and simply, this way enhances security at table level.

Oracle 11g release doesn’t provide READ-ONLY specification at table creation level. Therefore, a table has to be created in transactional mode still as in Code [1].

Code [1]
Sample Code
  1. SQL> CREATE TABLE T_READ
  2.      (COL1 NUMBER,
  3.       COL2 VARCHAR2(10))
  4.       /
  5.  
  6. TABLE created.
Copyright exforsys.com


Now, above table can be switched over to READ-ONLY mode as shown in Code [2].

Ads

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


A new column READ_ONLY has been added in [USER | DBA | ALL] _TABLES views to reveal the mode of the table.

Code [3]
Sample Code
  1. SQL> SELECT TABLE_NAME, READ_ONLY FROM USER_TABLE
  2. WHERE TABLE_NAME='T_READ'
  3.   /
  4.  
  5. TABLE_NAME                     REA
  6. ------------------------------ ---
  7. T_READ                         YES
Copyright exforsys.com




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

Oracle 11g Tutorial

 

Comments