Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm almost new user to databases, so I think I'm doing something wrong.

  • I have the database created.
  • I created 3 users, 1 it's an Admin, 2 are just users.
  • I created 2 roles, 1 for the Admin and 2 for the other users.

I'm learning that the schemas in Oracle are different from SQL Server. In SQL Server I created tables assigned to a schema, for example: CREATE TABLE schema1.table1 and then I was be able to see them all with users asigned to that DB; with Oracle, I think a schema is the sum of the sequence, synonyms, etc that a user has. (If not, please correct me)

I want to achieve that in Oracle, so I created the users/schemas; after that I got connected with user1 and created the tables with that user; later, I got connected with user2 and created the tables related to the user.

Now, when I'm trying to create the alter relating user1 tables and user2 tables with admin, it says I don't have enough privileges.

ALTER I'm trying to do:

ALTER TABLE user1.PhoneTable
    ADD (CONSTRAINT C_001 FOREIGN KEY (Status) 
            REFERENCES user2.ClientTable (Status) ON DELETE SET NULL);

I created those users because if I wanna see all the tables related to the Phone(user1), I got connected to that user and that should be all, I just wanna have the DB with some order.

Admin privileges:

GRANT 
    CREATE SESSION,
    UNLIMITED TABLESPACE,
    CREATE TABLE,
    DROP ANY TABLE,
    CREATE CLUSTER,
    CREATE SYNONYM,
    CREATE PUBLIC SYNONYM,
    CREATE VIEW,
    CREATE SEQUENCE,
    CREATE DATABASE LINK,
    CREATE PROCEDURE,
    CREATE TRIGGER,
    CREATE MATERIALIZED VIEW,
    CREATE ANY DIRECTORY,
    DROP ANY DIRECTORY,
    CREATE TYPE,
    CREATE LIBRARY,
    CREATE OPERATOR,
    CREATE INDEXTYPE,
    CREATE DIMENSION,
    CREATE ANY CONTEXT,
    SELECT ANY DICTIONARY,
    CREATE JOB,
    ALTER ANY TABLE,
TO myAdmin;

User privileges:

GRANT 
    CREATE session, 
    CREATE table, 
    CREATE view
    CREATE procedure, 
    CREATE synonym,
    ALTER ANY table, 
    ALTER view, 
    ALTER procedure, 
    ALTER synonym,
    DROP table, 
    DROP view, 
    DROP procedure, 
    DROP synonym
TO myUsers;
share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

You need to grant the REFERENCES privilege on the reference table to user2. (See GRANT, Table Privileges section. Note that this cannot be granted to a role, must be granted to the user directly.)

Here's a demo:

SQL> create user user1 identified by user1;
User created.
SQL> grant create session, create table, unlimited tablespace to user1;
Grant succeeded.

SQL> create user user2 identified by user2;
User created.
SQL> grant create session, create table, unlimited tablespace to user2;
Grant succeeded.

SQL> create table user1.ref_table (id number primary key);
Table created.
SQL> insert into user1.ref_table values (1);
1 row created.
SQL> insert into user1.ref_table values (2);
1 row created.
SQL> commit;
Commit complete.

SQL> grant references on user1.ref_table to user2;
Grant succeeded.

SQL> connect user2/user2;
Connected.
SQL> create table oth_table (thing number, fk number);
Table created.
SQL> alter table oth_table add(constraint fk1 foreign key (fk)
  2      references user1.ref_table on delete set null);
Table altered.

SQL> insert into oth_table values (42, 1);
1 row created.
SQL> insert into oth_table values (256, 2);
1 row created.
SQL> commit;
Commit complete.

SQL> connect user1/user1;
Connected.
SQL> delete from ref_table where id = 1;
1 row deleted.
SQL> commit;
Commit complete.

SQL> connect user2/user2;
Connected.
SQL> select * from oth_table;

     THING     FK
---------- ----------
    42
       256      2

If you want admin to do all the work, it'll need the CREATE ANY INDEX privilege, and you still need the REFERENCES grant to user2.

Here's how that could work:

SQL> create user user1 identified by user1;
User created.
SQL> create user user2 identified by user2;
User created.
SQL> create user admin identified by admin;
User created.

--- Grants
----------------
SQL> grant create session, create table, unlimited tablespace to user1;
Grant succeeded.
SQL> grant create session, create table, unlimited tablespace to user2;
Grant succeeded.
SQL> grant create session, create any table,
  2        create any index, alter any table to admin;
Grant succeeded.

--- Create reference table as admin
----------------
SQL> connect admin/admin
Connected.
SQL> create table user1.ref_table (id number primary key);
Table created.

--- Do the "references" grant
----------------
SQL> connect / as sysdba
Connected.
SQL> grant references on user1.ref_table to user2;
Grant succeeded.

--- Create the second table as admin
----------------
SQL> connect admin/admin
Connected.
SQL> create table user2.oth_table (foo number, bar number);
Table created.

--- Add the constraint
----------------
SQL> alter table user2.oth_table add(constraint fk
  2      foreign key (bar) references user1.ref_table(id)
  3      on delete set null);
Table altered.
share|improve this answer
    
Ok I see, but what if I have N tables? Is there something like GRANT REFERENCE ANY TABLE ON user2? –  Luis Nov 15 '12 at 13:23
    
As far as I know, you need to grant that privilege per object, no wildcard support. –  Mat Nov 15 '12 at 13:43
    
It works with user2 trying to access the objects of user1, but I've tried with admin and it's not working. I used: GRANT REFERENCES ON user1.PhoneTable TO admin; GRANT REFERENCES ON user2.ClientTable TO admin; and the ALTER is still not working, any ideas? –  Luis Nov 15 '12 at 15:18
    
I'm not sure I understand what you're trying to do. You want admin to be able to create those tables and the foreign key? –  Mat Nov 15 '12 at 16:19
    
To create the alter with admin relating 2 tables on 2 different schemas(user1 and user2)... Honestly I don't know if this is the best approach... –  Luis Nov 15 '12 at 16:28
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.