第25讲 触发器的类型建立修改删除应用Word文件下载.docx

上传人:b****4 文档编号:16568341 上传时间:2022-11-24 格式:DOCX 页数:14 大小:20.61KB
下载 相关 举报
第25讲 触发器的类型建立修改删除应用Word文件下载.docx_第1页
第1页 / 共14页
第25讲 触发器的类型建立修改删除应用Word文件下载.docx_第2页
第2页 / 共14页
第25讲 触发器的类型建立修改删除应用Word文件下载.docx_第3页
第3页 / 共14页
第25讲 触发器的类型建立修改删除应用Word文件下载.docx_第4页
第4页 / 共14页
第25讲 触发器的类型建立修改删除应用Word文件下载.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

第25讲 触发器的类型建立修改删除应用Word文件下载.docx

《第25讲 触发器的类型建立修改删除应用Word文件下载.docx》由会员分享,可在线阅读,更多相关《第25讲 触发器的类型建立修改删除应用Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。

第25讲 触发器的类型建立修改删除应用Word文件下载.docx

4)AFTER触发器:

在insert,update,delete语句之后运行。

OLD.字段名

NEW.字段名

在审计应用程序中经常使用AFTER行级触发器,因为直到行被修改才触发这些触发器。

行的成功修改表明数据已经通过表的各种约束。

5)INSTEADOF触发器:

使用INSTEADOF触发器,告诉Oracle要作的事情,而不是执行调用触发器的操作。

6)模式触发器(Schematrigger)

可以在模式级操作上建立触发器,如:

createtable,altertable,droptable,rename,revoke,grant等操作上建立触发器。

模式级触发器的作用为:

阻止执行DDL操作或发生DDL操作时进行额外的安全监控。

7)数据库级触发器(DatabaseLevelTrigger)

可以在数据库级事件上建立触发器,如错误,注册,注销,关闭,启动等事件。

使用数据库级触发器进行自动的数据库维护,或审计活动。

4.触发器创建和修改

语法:

(只给出最常用的触发器类型定义)

CREATEORREPLACETRIGGER[schema_name.]trigger_name

{BEFORE|AFTER|INSTEADOF}

{delete|insert|update[ofcolumn_name,…][ordelete|insert|update[ofcolumn,….]]

ONtable_name

[FOREACHROW]

[WHENcondition]

[DECLARE]

变量定义;

BEGIN

语句;

END;

1)触发器的名字在方案下必须唯一。

不能与表,视图,存储过程,函数等同名。

2)触发器的名字必须时合法的Oracle标识,长度为30字符以内。

3)使用FOREACHROW为行级触发器,否则为语句级触发器。

4)使用WHEN子句进一步限定触发器执行,当满足条件时才执行触发器。

5)WHEN子句的条件中可以使用新值,旧值。

在WHEN条件中引用新值和旧值不用使用:

OLD,:

NEW,直接使用NEW.字段名,OLD.字段名。

6)在PL/SQL中使用新值和旧值,必须使用冒号开头的NEW和OLD.:

NEW,:

OLD。

7)触发器中的事务处理类型为:

inserting,deleting,updating.当在一个触发器中同时使用2种以上的DML语句时,应该判断执行的是什么操作。

如果是insert语句,则INSERTING为true,如果是update语句,则UPDATING为true,如果执行DELETE语句,则DELETING为true.一般使用IFinsertingTHEN语句进行判断。

8)不要在触发器中编写大段的代码,应该将代码写在存储过程中,在触发器中调用存储过程。

Call存储过程名(参数,。

);

最简单的触发器创建语法:

createorreplacetriggerdocument_insert_event

afterinsert

onDocument

foreachrow

declare

begin

end;

触发器创建的例子:

createorreplacetriggerTrigger_001

beforeupdate

onemp

when(new.sal>

old.sal)

insertintoTEMP001(no,name)values(:

old.ename,:

old.sal);

5.创建DDL级触发器:

1)建立在DDL操作上的触发器。

2)可以利用DDL事件触发器来执行在簇,函数,索引,包,过程,角色,序列,同义词,表,表空间,触发器,类型,用户,视图上执行create,alter和drop命令时的执行代码。

3)

创建DDL级触发器的例子:

createorreplacetriggerTT02

aftercreateonschema

callinsert_audit_record(ora.dict_obj_name);

6.创建数据库级的触发器

Table16-1SystemDefinedEventAttributes 

Attribute

Type

Description

Example

ora_client_ip_address

VARCHAR2

ReturnstheIPaddressoftheclientinaLOGONevent,whentheunderlyingprotocolisTCP/IP

if(ora_sysevent='

LOGON'

thenaddr:

=

ora_client_ip_address;

endif;

ora_database_name

VARCHAR2(50)

Databasename.

DECLARE

db_nameVARCHAR2(50);

db_name:

=ora_database_name;

ora_des_encrypted_password

TheDESencryptedpasswordoftheuserbeingcreatedoraltered.

IF(ora_dict_obj_type='

USER'

THENINSERTINTOevent_table

(ora_des_encrypted_password);

ENDIF;

ora_dict_obj_name

VARCHAR(30)

NameofthedictionaryobjectonwhichtheDDLoperationoccurred.

INSERTINTOevent_table

('

Changedobjectis'

||

ora_dict_obj_name'

);

ora_dict_obj_name_list

(name_listOUT

ora_name_list_t)

BINARY_INTEGER

Returnthelistofobjectnamesofobjectsbeingmodifiedintheevent.

ASSOCIATE

STATISTICS'

thennumber_modified:

(name_list);

ora_dict_obj_owner

OwnerofthedictionaryobjectonwhichtheDDLoperationoccurred.

INSERTINTOevent_table('

object

owneris'

ora_dict_obj_owner'

ora_dict_obj_owner_list(owne

r_listOUTora_name_list_t)

Returnsthelistofobjectownersofobjectsbeingmodifiedintheevent.

then

number_of_modified_objects:

ora_dict_obj_owner_list(owner_li

st);

ora_dict_obj_type

VARCHAR(20)

TypeofthedictionaryobjectonwhichtheDDLoperationoccurred.

This

objectisa'

ora_dict_obj_type);

ora_grantee(

user_list

OUTora_name_list_t)

ReturnsthegranteesofagranteventintheOUTparameter;

returnsthenumberofgranteesinthereturnvalue.

GRANT'

)then

number_of_users:

ora_grantee(user_list);

ora_instance_num

NUMBER

Instancenumber.

IF(ora_instance_num=1)

THENINSERTINTOevent_table

1'

ora_is_alter_column(

column_nameINVARCHAR2)

BOOLEAN

Returnstrueifthespecifiedcolumnisaltered.

ALTER'

and

ora_dict_obj_type='

TABLE'

thenalter_column:

ora_is_alter_column('

FOO'

ora_is_creating_nested_table

ReturnTRUEifthecurrenteventiscreatinganestedtable

CREATE'

and

ora_is_creating_nested_table)

theninsertintoevent_tab

values('

Anestedtableis

created'

ora_is_drop_column(

Returnstrueifthespecifiedcolumnisdropped.

thendrop_column:

ora_is_drop_column('

ora_is_servererror

ReturnsTRUEifgivenerrorisonerrorstack,FALSEotherwise.

IF

(ora_is_servererror(error_number

))

Servererror!

!

'

ora_login_user

VARCHAR2(30)

Loginusername.

SELECTora_login_user

FROMdual;

ora_partition_pos

InanINSTEADOFtriggerforCREATETABLE,thepositionwithintheSQLtextwhereyoucouldinsertaPARTITIONclause.

--Retrieveora_sql_txtinto

--sql_textvariablefirst.

n:

=ora_partition_pos;

new_stmt:

substr(sql_text,1,n-1)||

'

||my_partition_clause||

||substr(sql_text,n));

ora_privilege_list(

privilege_listOUT

ReturnsthelistofprivilegesbeinggrantedbythegranteeorthelistofprivilegesrevokedfromtherevokeeintheOUTparameter;

returnsthenumberofprivilegesinthereturnvalue.

or

ora_sysevent='

REVOKE'

thennumber_of_privileges:

ora_privilege_list(priv_list);

ora_revokee(

user_listOUT

ReturnstherevokeesofarevokeeventintheOUTparameter;

returnsthenumberofrevokeesinthereturnvalue..

then

ora_revokee(user_list);

ora_server_error

Givenaposition(1fortopofstack),itreturnstheerrornumberatthatpositiononerrorstack

top

stackerror'

ora_server_error

(1));

ora_server_error_depth

Returnsthetotalnumberoferrormessagesontheerrorstack.

=ora_server_error_depth;

--Thisvalueisusedwith

--otherfunctionssuchas

--ora_server_error

ora_server_error_msg

(positioninbinary_integer)

Givenaposition(1fortopofstack),itreturnstheerrormessageatthatpositiononerrorstack

stackerrormessage'

ora_server_error_msg

(1));

ora_server_error_num_params

Givenaposition(1fortopofstack),itreturnsthenumberofstringsthathavebeensubstitutedintotheerrormessageusingaformatlike"

%s"

.

ora_server_error_num_params

(1);

ora_server_error_param

(positioninbinary_integer,

paraminbinary_integer)

Givenaposition(1fortopofstack)andaparameternumber,returnsthematching"

"

%d"

andsoonsubstitutionvalueintheerrormessage.

--E.g.the2rd%sinamessage

--like"

Expected%s,found%s"

param:

ora_server_error_param(1,2);

ora_sql_txt(sql_textout

ReturnstheSQLtextofthetriggeringstatementintheOUTparameter.Ifthestatementislong,itisbrokenupintomultiplePL/SQLtableelements.ThefunctionreturnvaluespecifieshowmanyelementsareinthePL/SQLtable.

sql_textora_name_list_t;

stmtVARCHAR2(2000);

...

=ora_sql_txt(sql_text);

FORiIN1..nLOOP

stmt:

=stmt||sql_text(i);

ENDLOOP;

text

oftriggeringstatement:

stmt);

ora_sysevent

VARCHAR2(20)

Systemeventfiringthetrigger:

Eventnameissameasthatinthesyntax.

(ora_sysevent);

ora_with_grant_option

Returnstrueiftheprivilegesaregrantedwithgrantoption.

ora_with_grant_option=TRUE)

theninsertintoevent_table

withgrantoption'

space_error_info(

error_numberOUTNUMBER,

error_typeOUTVARCHAR2,

object_ownerOUTVARCHAR2,

table_space_nameOUT

VARCHAR2,

object_nameOUTVARCHAR2,

sub_object_nameOUT

VARCHAR2)

Returnstrueiftheerrorisrelatedtoanout-of-spacecondition,andfillsintheOUTparameterswithinformationabouttheobjectthatcausedtheerror.

if(space_error_info(eno,typ,

owner,ts,obj,subobj)=TRUE)

dbms_output.put_line('

The

object'

||obj||'

ownedby'

||owner||'

hasrunoutof

space.'

创建数据库级触发器的例子:

createorreplacetriggerTT003_ON_DATABASE

afterstartupondatabase

null;

ClientEvents

Clienteventsaretheeventsrelatedtouserlogon/logoff,DML,andDDLoperations.Forexample:

CREATEORREPLACETRIGGEROn_Logon

AFTERLOGON

ONThe_user.Schema

BEGIN

Do_Something;

7.触发器删除

DROPTRIGGERtrigger_name

触发器删除例子:

droptriggerTT001

8.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1