Tuesday, November 27, 2012

Two ways to make a table read-only

Here are two ways to make a table read-only:
First lets do it the 11g way:


nikos@NIKOSDB> create table lala (x int, y int)
  2  /

Table created.

nikos@NIKOSDB> insert into lala values (1,1)
  2  /

1 row created.

nikos@NIKOSDB> insert into lala values (2,2)
  2  /

1 row created.

nikos@NIKOSDB> commit
  2  /

Commit complete.

nikos@NIKOSDB> alter table lala read only
  2  /

Table altered.

nikos@NIKOSDB> insert into lala values (3,3)
  2  /
insert into lala values (3,3)
            *
ERROR at line 1:
ORA-12081: update operation not allowed on table "NIKOS"."LALA"


nikos@NIKOSDB> alter table lala read write
  2  /

Table altered.

nikos@NIKOSDB> insert into lala values (3,3)
  2  /

1 row created.

nikos@NIKOSDB> commit
  2  /

Commit complete.

Second, lets do it the old way: we will create a constraint on the table and then make the constraint DISABLE, VALIDATE:


nikos@NIKOSDB> alter table lala add constraint lalaunq unique(x,y)
  2  /

Table altered.

nikos@NIKOSDB> insert into lala values (4,4)
  2  /

1 row created.

nikos@NIKOSDB> -- still I can insert rows but now ...
nikos@NIKOSDB> alter table lala modify constraint lalaunq disable validate
  2  /

Table altered.

nikos@NIKOSDB> insert into lala values (5,5)
  2  /
insert into lala values (5,5)
*
ERROR at line 1:
ORA-25128: No insert/update/delete on table with constraint (NIKOS.LALAUNQ) disabled and validated


nikos@NIKOSDB> --VOILA!  The table is read-only now

Cheers,
OL


No comments:

Post a Comment