oracle学习记录之授权(2)

本文介绍了一种高效地将一个Oracle用户的所有表查询权限批量授予另一个用户的方法。通过使用SQL查询构造授权语句,避免了手动逐个表授权的繁琐步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值