oracle用户授权访问其他用户的表,Oracle授权A用户查询B用户的所有表

Oracle授权A用户查询B用户的所有表

需求:

新建的用户userA,要授权给他访问用户scott的所有表

有三种两方法:

1)

SQL> conn / as sysdba;

SQL> grant select any table on userA

这种方法的缺点是授予的权限过大,userA不仅可以访问scott下的所有表,也可以访问其他用户包括sys,system下的所有表。

2)

SQL> conn scott/tiger;

SQL> select 'GRANT SELECT ON' || table_name || 'to userA;'  from user_tables

得到的结果如下

grant select on emp to userA;

grant select on dept to userA;

grant select on bonus to userA;

grant select on loc to userA;

再把上面得到的结果逐一执行一遍:

SQL> grant select on emp to userA;

SQL> grant select on dept to userA;

SQL> grant select on bonus to userA;

SQL> grant select on loc to userA;

这种方法的缺点是要执行比较多的语句,如果有100个表,就得执行100个grant语句;

另外scott新建的表不在被授权的范围内,新建的表要想被userA访问,也得执行grant语句:

grant select on 新建的表 to userA;

(3)使用游标

先创建两个用户

SQL> create user test1 identified by oracle;

User created.

SQL> create user test2 identified by oracle;

User created.

授权

SQL> grant connect, resource to test1;

Grant succeeded.

SQL> grant connect, resource to test2;

Grant succeeded.

在test2下建立一个表作测试用

SQL> conn test2/oracle;

Connected.

SQL> create table t(id number);

Table created.

创建角色并用游标给角色授权

SQL> conn /as sysdba;

Connected.

SQL> create role select_all_test2_tab;

Role created

SQL>

declare

CURSOR c_tabname is select table_name from dba_tables where owner = 'TEST2';

v_tabname dba_tables.table_name%TYPE;

sqlstr    VARCHAR2(200);

begin

open c_tabname;

loop

fetch c_tabname into v_tabname;

exit when c_tabname%NOTFOUND;

sqlstr := 'grant select on test2.' || v_tabname ||' to select_all_test2_tab';

execute immediate sqlstr;

end loop;

close c_tabname;

end;

/

PL/SQL procedure successfully completed.

把角色授权给test1

SQL> grant select_all_test2_tab to test1;

Grant succeeded.

尝试用test1访问test2的表

SQL> conn test1/oracle;

Connected.

SQL> select * from test2.t;

no rows selected

在test2下新建表

SQL> conn test2/oracle;

Connected.

SQL> create table ta(id number);

Table created.

尝试用test1访问新建的表

SQL> conn test1/oracle;

Connected.

SQL> select * from test2.ta;

select * from test2.ta

*

ERROR at line 1:

ORA-00942: table or view does not exist

结论:与第二种方案相比,用这种方式不需逐一把test2下的表授权给test1访问,但test2新建的表无法被test1访问。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30373263/viewspace-2123445/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值