oracle学习记录之授权(2)

来源:互联网 发布:大数据人才的薪资待遇 编辑:程序博客网 时间:2024/06/06 17:48

oracle学习记录,授权,新的需求:把某用户的所有表的查询权限授给另一个用户。用oracle学习记录之授权(1)逐个表授权有点耗时费力。

思路:用所有表名构造grant语句,复制粘贴执行,省事。练习过程如下:

一、准备
sys用户连接数据库:
connect sys/oracle as sysdba;
建立test1、test2用户:
create user test1 identified by 123;
create user test2 identified by 123;
授予dba角色给test1:
grant dba to test1;
dba角色权限太大,收回dba角色:
revoke dba from test1;
授予test1、test2用户创建会话的权限:
grant create session to test1,test2;
授予test1用户创建表及拥有资源的权限:
grant create table,resource to test1;

test1用户连接数据库,建表,插入数据:
connect test1/123;
create table a(a number(10));
create table b(b number(10));
create table c(c number(10));
insert into a values(1);
insert into b values(2);
insert into c values(3);
commit;

从视图all_tables中查询以test开头共5个字符的拥有者的表:
--upper、lower大小写转换
select owner, table_name from all_tables
where lower(owner) like 'test_';

二、把test1用户的所有表上的查询权限授给test2用户的方法:

1、sys登录
2、执行以下语句:
--test1为拥有表的用户,test2为被授权用户,|| 为连接符
select 'grant select on '|| lower(table_name) ||' to test2;'
from all_tables
where owner = upper('test1');
3、复制以上查询结果(如下),粘贴执行,最后按一下回车执行最后一句。
grant select on a to test2;
grant select on b to test2;
grant select on c to test2;

三、测试授权成功:
connect test2/123
select * from test1.a;
select * from test1.b;
select * from test1.c;

原创粉丝点击