ORACLE 给用户授权truncate table 的实现方案

1,对其它用户下的表执行trundate table操作

开发说在用dwetl下执行调用shop用户下的表的时候提示没有权限操作,google了查了下,发现oracle账户没法直接赋予对某个表的truncate权限,那要怎么来实现呢? 
在shop用户下面,准备测试数据

SQL> create table Z_TRUNCATE_T(ID number);

Table created.

SQL> insert into Z_TRUNCATE_T select 1 from dual;

1 row created.

SQL> commit;

Commit complete.

SQL> select * from Z_TRUNCATE_T;

    ID
----------
     1

SQL> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19



2,比较粗鲁不安全的做法

通常赋予truncate的常规做法,是直接赋值drop any table给一个用户

SQL> grant drop any table to dwetl;

Grant succeeded.

SQL> 
SQL> grant select,insert,delete,update on Z_TRUNCATE_T to dwetl;

Grant succeeded.

SQL> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


干完活,需要赶紧马上收回权限因为drop any table权限是在太大了,一不小心就会造成误删除,到时候哭都来不及啊

SQL> revoke drop any table from dwetl;

Revoke succeeded.

SQL> revoke select,insert,delete,update on shop.PLAN6_TEMPLET_NODE_EDIT  from dwetl;

Revoke succeeded.

SQL> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9



3,比较安全的做法

建立一个存储过程p_truncate,在存储过来里面执行truncate table Z_TRUNCATE_T;然后赋予另外一个用户dwetl对这个存储过程的执行权限。

存储过程p_truncate如下:

 create or replace procedure p_truncate as   
    begin
    execute immediate 'truncate table Z_TRUNCATE_T';
    end;

 
 
  • 1
  • 2
  • 3
  • 4
  • 5


建立存储过程:

SQL> 
 create or replace procedure p_truncate as   
begin
execute immediate 'truncate table Z_TRUNCATE_T';
  4  end;
  5  /

Procedure created.

SQL>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


赋予存储过程的执行权限给dwetl,并且赋予表的增删改查权限,因为truncate后,紧接着的基本就是insert、update、delete了

SQL> grant execute on p_truncate to dwetl;

Grant succeeded.

SQL> 
SQL> grant select,insert,delete,update on Z_TRUNCATE_T to dwetl;

Grant succeeded.

SQL> 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10


通过dwetl账号登陆,执行存储过程查看效果,看到shop用户下的表Z_TRUNCATE_T已经被清空了,ok,如此也证明了通过存储过程这种方案是可行的,可以对别的用户下的表进行truncate table操作。 
–查看

SQL> call shop.p_truncate();

Call completed.

SQL> select * from shop.Z_TRUNCATE_T;

no rows selected

SQL> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值