Monday, May 26, 2008

What is View in Oracle

A view in oracle is nothing but a stored sql scripts. View itself contain no data. Whenever we query on a view underlying SQL scripts is executed. Suppose I have created a view as CREATE VIEW TEST_VIEW AS SELECT NAME FROM TEST; Now whenever I query on view TEST_VIEW underlying script SELECT NAME FROM TEST is executed.

A view can be queried, and the data it represents can be changed. Data in a view can be updated or deleted, and new data inserted. These operations directly alter the tables on which the view is based and are subject to the integrity constraints and triggers of the base tables.

Workaround Example:
Connect as a dba user and grant only insert privilege to table TEST on arju schema table to india.

SQL> CONN ARJU/A
Connected.

SQL> revoke SELECT ON TEST from india;
Revoke succeeded.

SQL> grant insert ON TEST TO INDIA;
Grant succeeded.

SQL> CONN INDIA/T
Connected.
SQL> CREATE VIEW ARJU_SCHMEA_VIEW AS SELECT * FROM ARJU.TEST;
View created.

Since india has only insert privilege on the base table so he can create view but can't select any row from the base table or view.

SQL> SELECT * FROM ARJU_SCHMEA_VIEW;
SELECT * FROM ARJU_SCHMEA_VIEW
*
ERROR at line 1:
ORA-01031: insufficient privileges
Now grant him select privilege on the base table.

SQL> CONN ARJU/A

Connected.
SQL> grant SELECT ON TEST TO INDIA;
Grant succeeded.

SQL> CONN INDIA/T

Connected.
SQL> SELECT * FROM ARJU_SCHMEA_VIEW;
no rows selected

Now insert row in to the view.
SQL> INSERT INTO ARJU_SCHMEA_VIEW VALUES(1);
1 row created.

SQL> COMMIT;

Commit complete.

SQL> SELECT * FROM ARJU_SCHMEA_VIEW;
A
----------
1

SQL> SELECT * FROM ARJU.TEST;
A
----------
1

So if underlying view is modified base table is affected.

No comments:

Post a Comment