数据库用户授予2个模式中的同一个表

时间:2016-02-18 10:56:21

标签: oracle

我尝试了以下内容 - 并且期待出现错误,但似乎工作正常 User1被赋予对表XYZ的选择授权,其存在于两个模式中 test1.XYZ和test2.XYZ

现在这个用户 - User1被给予' SELECT'授予两个模式中的表XYZ - test1和test2

我原以为以下选择会给出一些例外情况,例如“模棱两可”。等等 - 但它工作:(以User1身份登录)

select * from XYZ;

似乎从架构test1中的XYZ表中获取详细信息 有点困惑为什么这会起作用。

1 个答案:

答案 0 :(得分:0)

假设在模式中不存在具有该名称的表,它可能取决于同义词; 例如,在不同的模式中创建具有相同名称的表

SQL> conn test1/test1@main
Connected.
SQL> create table xyz(id number);

Table created.

SQL> grant select on xyz to user1;

Grant succeeded.

SQL> conn test2/test2@yourDB
Connected.
SQL> create table xyz(id number);

Table created.

SQL> grant select on xyz to user1;

Grant succeeded.

这样您的用户就会毫不怀疑,但不会看到任何表格:

SQL> conn user1/user1@yourDB
Connected.
SQL> select * from xyz;
select * from xyz
              *
ERROR at line 1:
ORA-00942: table or view does not exist

如果添加架构,则可以查询:

SQL> select * from test1.xyz;

no rows selected

SQL> select * from test2.xyz;

no rows selected

现在,您可以为其中一个表创建公共同义词:

SQL> conn test1/test1@yourDB
Connected.
SQL> create public synonym xyz for xyz;

Synonym created.

这样,您的用户只能看到包含同义词的表格:

SQL> conn user1/user1@yourDB
Connected.
SQL> select * from xyz;

no rows selected

即使在创建同义词后,您也可以通过编写'schema.table'来访问表。