Kamran Agayev's Oracle Blog

Oracle Certified Master

Getting "ORA-00942: table or view does not exist" when missing REFERENCES privilege

Posted by Kamran Agayev A. on November 3rd, 2009

SQL> create user usr1 identified by usr1;
User created.
SQL> grant dba to usr1;
Grant succeeded.
SQL> create user usr2 identified by usr2;
User created.
SQL> grant dba to usr2;
Grant succeeded.
SQL> conn usr1/usr1
Connected.
SQL> create table tbl_usr1 (id number);
Table created.
SQL> conn usr2/usr2
Connected.
SQL> create table tbl_usr2 (id number);
Table created.
SQL> insert into tbl_usr2 values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> alter table tbl_usr2 add primary key(id);
Table altered.
SQL> create public synonym tbl_usr2 for tbl_usr2;
Synonym created.
SQL> disc
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production With the Partitioning, OLAP and Data Mining options
SQL> conn usr1/usr1
Connected.
SQL> select * from tbl_usr2;
        ID
----------
         1
SQL> alter table tbl_usr1 add foreign key (id) references tbl_usr2(id);
alter table tbl_usr1 add foreign key (id) references tbl_usr2(id)
                                                     *
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from tbl_usr2;
        ID
----------
         1

Ops… Although I can query the table, while creating foreign key and referencing to that table, I get “ORA-00942: table or view does not exist” error. The reason is that the user hasn’t REFERENCES privilege which should be granted

GRANT REFERENCES is a privilege required by a user on a table so that this user can create new tables referencing such tables in foreign keys where he/she would otherwise be restricted.

SQL> conn usr2/usr2
Connected.
SQL> grant references on tbl_usr2 to usr1;
Grant succeeded.
SQL> conn usr1/usr1
Connected.
SQL> alter table tbl_usr1 add foreign key (id) references tbl_usr2(id);
Table altered.
SQL>

13 Responses to “Getting "ORA-00942: table or view does not exist" when missing REFERENCES privilege”

  1. Laurent Schneider Says:

    even as dba? that’s surprising indeed !

    another seldom used privilege is GRANT INDEX ON TBL_USER2 TO USR1;

    There is however no INDEX ANY TABLE or REFERENCES ANY TABLE privilege, worth an enhancement request? maybe 😉

  2. Kamran Agayev A. Says:

    Hi Laurent. Nice to meet you here in my Blog :)
    Why it’s strange is because I’ve dba privilege and I can query the table. Why Oracle returns “table or view doesn’t exits”? Is it normal? Shouldn’t it return a different error such as “You haven’t privilege to REFERENCE object” error? :)

  3. coskan Says:

    Kamran, looks like there is a problem with copy paste. How can you connect with a non-existing user usr2 before creating it ?

    SQL> create user usr1 identified by usr1;
    SQL> conn usr2/usr2
    Connected.
    SQL> create table tbl_usr2 (id number);
    User created.
    SQL> grant dba to usr1;
    Grant succeeded.
    SQL> create user usr2 identified by usr2;
    User created.

  4. Mohamed Azar Says:

    step2 &3 : how can you connect usr2 before creating “usr2” user.
    Step 9 : how can you insert data into table of user2.because you login as a user1.
    Sir, this is bug on your steps..please modify your steps.

    but i did it.i understand about ora-00942 reference privilege.

    Thanks & Regards

    Azar
    DBA

  5. Kamran Agayev A. Says:

    Thanks Mohammad. It was typo (typing error) Let me correct it. As you’ve created the second table in the same schema, you didn’t get this error. Try it once more :)

  6. Laurent Schneider Says:

    well, if the REFERENCE ANY TABLE would exist, the privilege would be granted to dba, and the dba would get no error :)

  7. Kamran Agayev A. Says:

    Nice explanation Laurent :)

  8. Laurent Schneider Says:

    quite funny that a google search of “grant index any table” gives you a dozen of links describing how to grant this inexistant privilege :mrgreen:

  9. Kamran Agayev A. Says:

    Yeap. That’s funny. But have you tried this kind of search? :)
    http://tinyurl.com/yhfxjp7

  10. Turkel M. Says:

    Hi All

    Another Similar issue is

    user A(dba) can select user B’s table – tabB. But when user A executes select on B.tabB inside procedure, he gets the same error.

    and the solution is grant select on B.tabB specifically

  11. Kamran Agayev A. Says:

    Hi Coskan. Sure, it was typo

    P.S. Just saw your post, it was at spam, thus haven’t approved it. Sorry :)

    What’s your opinion on this issue?

  12. kasante Says:

    dear Mr.karman. thanks for your support but i want to learn more about pl/sql but i am a newbie so please kindly show me what to do so that i will be expert in pl/sql. thanks.

  13. Kamran Agayev A. Says:

    Dear Kasante

    I’d suggest you to read the following boook:
    http://www.amazon.com/Oracle-PL-SQL-Example-3rd/dp/0131172611

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>