Monday, May 26, 2008

Privileges Required to Create Views

To create a view, you must meet the following requirements.

•To create a view on a user own schema he must be granted CREATE VIEW or CREATE ANY VIEW system privilege either explicitly or via a role.

GRANT CREATE VIEW TO user_name;
•To create a view on another user schema user must be granted CREATE ANY VIEW system privilege either explicitly or via a role.
GRANT CREATE ANY VIEW TO user_name;

•You must have been explicitly granted one of the following privileges.
1)On all base objects user have the SELECT or INSERT or UPDATE or DELETE object privileges underlying the view.

2)User have the SELECT ANY TABLE or INSERT ANY TABLE or UPDATE ANY TABLE or DELETE ANY TABLE system privileges.

•User must have the received object privileges to the base objects with the GRANT OPTION clause or
appropriate system privileges with the ADMIN OPTION clause. If you have not, then grantees cannot access view.

An workaround example:
------------------------------

As user india I have created a table named a and try to create view on a_v.
SQL> create table a(a number);
Table created.

SQL> create view a_v as select * from a;
create view a_v as select * from a
*
ERROR at line 1:
ORA-01031: insufficient privileges

Let's look at session privilege.CREATE VIEW privilege is not there.
SQL> select * from session_privs;
PRIVILEGE
----------------------------------------
CREATE SESSION
CREATE TABLE

Log on as a user of dba privilege and grant create view permission on india user.
SQL> conn arju/a
Connected.

SQL> GRANT CREATE VIEW TO INDIA;
Grant succeeded.

SQL> CONN INDIA/T
Connected.

SQL> create view a_v as select * from a;

View created.

So now india can create view on his own schema. To create view on another schema CREATE ANY VIEW privilege is required. Let's have a look his session privilege now.
SQL> select * from session_privs;
PRIVILEGE
----------------------------------------
CREATE SESSION
CREATE TABLE
CREATE VIEW

To create a view in another schema CREATE ANY VIEW privilege is required.
SQL> conn arju/a
Connected.

SQL> GRANT CREATE any VIEW TO INDIA;
Grant succeeded.

SQL> CREATE TABLE TEST(A NUMBER);
Table created.

SQL> CONN INDIA/T
Connected.

SQL> CREATE VIEW ARJU_SCHMEA_VIEW AS SELECT * FROM ARJU.TEST;

CREATE VIEW ARJU_SCHMEA_VIEW AS SELECT * FROM ARJU.TEST
*
ERROR at line 1:
ORA-00942: table or view does not exist

It will fail because user don't have either any SELECT or INSERT or UPDATE or DELETE privilege on the base table ARJU.TEST.

SQL> CONN ARJU/A
Connected.

SQL> GRANT SELECT ON TEST TO INDIA;
Grant succeeded.

SQL> CONN INDIA/T
Connected.

SQL> CREATE VIEW ARJU_SCHMEA_VIEW AS SELECT * FROM ARJU.TEST;
View created.

After granted SELECT ON base table he can successfully create view.

Related Documents
http://arjudba.blogspot.com/2009/12/oracle-object-type-exercises-varray.html
http://arjudba.blogspot.com/2009/12/practice-oracle-joins-examples.html
http://arjudba.blogspot.com/2009/12/oracle-security-practices.html
http://arjudba.blogspot.com/2009/12/exercises-with-oracle-create-table-add.html
http://arjudba.blogspot.com/2009/12/oracle-database-creation-exercises.html
http://arjudba.blogspot.com/2009/12/basic-oracle-sql-exercise.html
http://arjudba.blogspot.com/2009/08/format-model-modifiers-fx-and-fm.html
http://arjudba.blogspot.com/2009/08/number-format-models-in-oracle.html
http://arjudba.blogspot.com/2009/08/format-models-in-oracle.html
http://arjudba.blogspot.com/2009/07/sql-decode.html
http://arjudba.blogspot.com/2009/07/how-to-know-row-of-table-belong-to.html
http://arjudba.blogspot.com/2009/06/how-to-know-which-objects-are-being.html
http://arjudba.blogspot.com/2009/06/ddl-with-wait-option-in-11g.html
http://arjudba.blogspot.com/2009/06/ora-00939-too-many-arguments-when-case.html
http://arjudba.blogspot.com/2009/03/oracle-datatype-internal-code.html
http://arjudba.blogspot.com/2009/03/how-to-know-list-of-constraints-and.html
http://arjudba.blogspot.com/2009/02/how-to-know-dependent-objectswhich.html
http://arjudba.blogspot.com/2009/02/how-to-search-stringkey-value-from.html
http://arjudba.blogspot.com/2009/02/how-to-know-when-tableobjects-ddlcode.html
http://arjudba.blogspot.com/2009/02/ora-00920-invalid-relational-operator.html
http://arjudba.blogspot.com/2009/01/adding-default-value-to-column-on-table.html
http://arjudba.blogspot.com/2009/01/ora-12838-cannot-readmodify-object.html
http://arjudba.blogspot.com/2009/01/ora-01779-cannot-modify-column-which.html
http://arjudba.blogspot.com/2009/01/updating-table-based-on-another-table.html
http://arjudba.blogspot.com/2009/01/ora-00054-resource-busy-and-acquire.html
http://arjudba.blogspot.com/2008/12/troubleshoot-ora-02292-ora-02449-and.html
http://arjudba.blogspot.com/2008/06/ora-00903-oracle-database-reserved.html
http://arjudba.blogspot.com/2008/06/hints-in-oracle.html
http://arjudba.blogspot.com/2008/06/examples-of-usage-of-composite-index.html
http://arjudba.blogspot.com/2008/06/find-indexes-and-assigned-columns-for.html
http://arjudba.blogspot.com/2008/06/reasons-for-using-alter-table-statement.html
http://arjudba.blogspot.com/2008/06/alter-table-rename-table-add-column.html
http://arjudba.blogspot.com/2008/06/ora-01830-date-format-picture-ends.html
http://arjudba.blogspot.com/2008/06/default-date-timestamp-and-timestamp.html
http://arjudba.blogspot.com/2008/06/create-temporary-table-in-oracle.html
http://arjudba.blogspot.com/2008/06/example-of-antijoin-semijoin-curtesian.html
http://arjudba.blogspot.com/2008/12/ora-02297-cannot-disable-constraint.html
http://arjudba.blogspot.com/2008/10/convert-decimal-to-hexadecimal-on.html
http://arjudba.blogspot.com/2008/10/how-to-generate-fibonacci-series-in.html
http://arjudba.blogspot.com/2008/10/same-sounded-words-in-oracle.html
http://arjudba.blogspot.com/2008/09/type-of-constraint-in-oracle.html
http://arjudba.blogspot.com/2008/09/how-to-move-lob-data-to-another.html
http://arjudba.blogspot.com/2008/08/subqueries-in-oracle-with-example.html
http://arjudba.blogspot.com/2008/08/how-to-monitor-alert-log-file-in-oracle.html
http://arjudba.blogspot.com/2008/08/solution-of-ora-01873-leading-precision.html
http://arjudba.blogspot.com/2008/07/literals-and-literal-types-in-oracle.html
http://arjudba.blogspot.com/2008/07/ora-01722-invalid-number.html
http://arjudba.blogspot.com/2008/07/ora-00936-missing-expression.html
http://arjudba.blogspot.com/2008/07/ora-01756-quoted-string-not-properly.html
http://arjudba.blogspot.com/2008/07/pls-00428-into-clause-is-expected-in.html
http://arjudba.blogspot.com/2008/07/schema-object-naming-rules.html
http://arjudba.blogspot.com/2008/06/datetime-and-interval-datatypes.html
http://arjudba.blogspot.com/2008/06/large-object-lob-datatypes-with-example.html
http://arjudba.blogspot.com/2008/06/history-of-sql.html
http://arjudba.blogspot.com/2008/06/what-is-sql.html

4 comments:

  1. Very nice post, no surprise that it comes so much high in google ranking.
    Great Work, yes,, simple and clean.
    Kepp it up.

    ReplyDelete
  2. Well, if a user uses a role to obtain the privileges (which is common practice in serious bizniss), then this won't help you at all ;)

    ReplyDelete
  3. Thanks for a great article.

    Just want to clear something up: In order to create a view on DBA_FREE_SPACE can you inherit the select privilege from a role or does in have to be granted explicitly?

    ReplyDelete
  4. Good one.Helped in understanding the things.......

    ReplyDelete