国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何使用觸發器實現數據庫級守護,防止DDL操作

2024-07-21 02:06:59
字體:
來源:轉載
供稿:網友

    

如何使用觸發器實現數據庫級守護,防止ddl操作

--對于重要對象,實施ddl拒絕,防止create,drop,truncate,alter等重要操作

last updated: sunday, 2004-10-31 12:06 eygle
    
 
 

不管是有意還是無意的,你可能會遇到數據庫中重要的數據表等對象被drop掉的情況,這可能會給我們帶來巨大的損失.

通過觸發器,我們可以實現對于表等對象的數據庫級守護,禁止用戶drop操作.

以下是一個簡單的范例,供參考:

rem this script can be used to monitor a objectrem deny any drop operation on it.create or replace trigger trg_dropdeny before drop on databasebegin if lower (ora_dict_obj_name ()) = 'test' then raise_application_error (num => -20000, msg => '你瘋了,想刪除表 ' || ora_dict_obj_name () || ' ?!!!!!' || '你完了,警察已在途中.....' ); end if;end;/

測試效果:

 

sql> connect scott/tigerconnected.sql> create table test as select * from dba_users;table created.sql> connect / as sysdbaconnected.sql> create or replace trigger trg_dropdeny 2 before drop on database 3 begin 4 if lower(ora_dict_obj_name()) = 'test' 5 then 6 raise_application_error( 7 num => -20000, 8 msg => '你瘋了,想刪除表 ' || ora_dict_obj_name() || ' ?!!!!!' ||'你完了,警察已在途中.....'); 9 end if; 10 end; 11 /trigger created.sql> connect scott/tigerconnected.sql> drop table test;drop table test*error at line 1:ora-00604: error occurred at recursive sql level 1ora-20000: 你瘋了,想刪除表 test ?!!!!!你完了,警察已在途中.....ora-06512: at line 4

 

oracle從oracle8i開始,允許實施ddl事件trigger,可是實現對于ddl的監視及控制,以下是一個進一步的例子:

create or replace trigger ddl_denybefore create or alter or drop or truncate on databasedeclare l_errmsg varchar2(100):= 'you have no permission to this operation';begin if ora_sysevent = 'create' then raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg); elsif ora_sysevent = 'alter' then raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg); elsif ora_sysevent = 'drop' then raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg); elsif ora_sysevent = 'truncate' then raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg); end if;exception when no_data_found then null;end;/

 

我們看一下效果:

[[email protected] tools]$ sqlplus "/ as sysdba"

sql*plus: release 9.2.0.4.0 - production on sun oct 31 11:38:25 2004

copyright (c) 1982, 2002, oracle corporation. all rights reserved.


connected to:
oracle9i enterprise edition release 9.2.0.4.0 - production
with the partitioning option
jserver release 9.2.0.4.0 - production

sql> set echo on
sql> @ddlt
sql> create or replace trigger ddl_deny
2 before create or alter or drop or truncate on database
3 declare
4 l_errmsg varchar2(100):= 'you have no permission to this operation';
5 begin
6 if ora_sysevent = 'create' then
7 raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg);
8 elsif ora_sysevent = 'alter' then
9 raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg);
10 elsif ora_sysevent = 'drop' then
11 raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg);
12 elsif ora_sysevent = 'truncate' then
13 raise_application_error(-20001, ora_dict_obj_owner || '.' || ora_dict_obj_name || ' ' || l_errmsg);
14 end if;
15
16 exception
17 when no_data_found then
18 null;
19 end;
20 /

trigger created.

sql>
sql>
sql> connect scott/tiger
connected.
sql> create table t as select * from test;
create table t as select * from test
*
error at line 1:
ora-00604: error occurred at recursive sql level 1
ora-20001: scott.t you have no permission to this operation
ora-06512: at line 5


sql> alter table test add (id number);
alter table test add (id number)
*
error at line 1:
ora-00604: error occurred at recursive sql level 1
ora-20001: scott.test you have no permission to this operation
ora-06512: at line 7


sql> drop table test;
drop table test
*
error at line 1:
ora-00604: error occurred at recursive sql level 1
ora-20001: scott.test you have no permission to this operation
ora-06512: at line 9


sql> truncate table test;
truncate table test
*
error at line 1:
ora-00604: error occurred at recursive sql level 1
ora-20001: scott.test you have no permission to this operation
ora-06512: at line 11



 

我們可以看到,ddl語句都被禁止了,如果你不是禁止,可以選擇把執行這些操作的用戶及時間記錄到另外的臨時表中.以備查詢.

 

 

 

本文作者:
eygle,oracle技術關注者,來自中國最大的oracle技術論壇itpub.
www.eygle.com是作者的個人站點.你可通過[email protected]來聯系作者.歡迎技術探討交流以及鏈接交換.

原文出處:

http://www.eygle.com/faq/use.trigger.to.implement.ddl.deny.htm

 

如欲轉載,請注明作者與出處.并請保留本文的連接.


回首頁

 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凉城县| 定南县| 靖宇县| 沈阳市| 白银市| 崇礼县| 芜湖县| 福贡县| 全椒县| 合作市| 南皮县| 吴川市| 广汉市| 威远县| 贵阳市| 巧家县| 大方县| 剑河县| 章丘市| 沈阳市| 荣成市| 大竹县| 三都| 邵阳市| 昌平区| 荥阳市| 七台河市| 盐池县| 扬州市| 秭归县| 忻城县| 林周县| 莒南县| 临漳县| 娄烦县| 格尔木市| 新巴尔虎左旗| 抚顺县| 岚皋县| 桦川县| 临安市|