如何在 Oracle 11g 中连接多个用户/模式?

Lui*_*uis 3 oracle sql-server oracle-11g-r2 permissions

我几乎是数据库的新用户,所以我认为我做错了什么。

  • 我已经创建了数据库。
  • 我创建了 3 个用户,1 个是管理员,2 个只是用户。
  • 我创建了 2 个角色,1 个用于管理员,2 个用于其他用户。

我正在学习中的模式OracleSQL Server. 例如,在 SQL Server 中,我创建了分配给架构的表:CREATE TABLE schema1.table1然后我能够看到所有这些表,以及分配给该数据库的用户;对于 Oracle,我认为模式是用户拥有的序列、同义词等的总和。(如果不是,请纠正我)

我想在 Oracle 中实现这一点,所以我创建了用户/模式;之后,我user1与该用户建立了联系并创建了表;后来,我连接user2并创建了与用户相关的表。

现在,当我尝试使用管理员创建更改相关user1表和user2表时,它说我没有足够的权限。

改变我正在尝试做的事情:

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

我创建了这些用户,因为如果我想查看与电话(user1)相关的所有表,我已连接到该用户,这应该是全部,我只想让数据库有一些订单。

管理员权限:

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;
Run Code Online (Sandbox Code Playgroud)

用户权限:

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;
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 5

您需要REFERENCES将引用表的权限授予user2。(参见GRANT表权限部分。请注意,这不能授予角色,必须直接授予用户。)

这是一个演示:

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
Run Code Online (Sandbox Code Playgroud)

如果你想admin完成所有的工作,它需要CREATE ANY INDEX特权,而且你仍然需要REFERENCES授权给user2.

这是如何工作的:

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.
Run Code Online (Sandbox Code Playgroud)