Lecture Notes Unit5 chapter16 Trigger Creation

Murugan146644 1,112 views 18 slides Jul 31, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental principles and advanced topics in RDBMS, offering a structured appr...


Slide Content

RDBMS -Unit V
Chapter 16
TRIGGER
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
AlagappaGovernment Arts College, Karaikudi.
(Affiliated by AlagappaUniversity)
Mailid: [email protected]
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO

TRIGGER -Definition
➢Triggersareroutinesorproceduresthatareusedwhen
anINSERT,UPDATE,orDELETEcommandis
executedinatableoraview..
DifferencebetweenTriggerandProcedure
➢Atriggerisautomaticallyexecutedwithoutanyaction
requiredbytheuser.
➢Astoredprocedure,ontheotherhand,needstobe
explicitlyinvoked.

TRIGGER –Merits and Demerits
AdvantagesofTrigger
➢Themajorapplicationofatriggeristocreate
consistenciestothedatabase.
➢Createvalidationmechanisms.
Restrictions
➢AtriggercannotexecutetheCOMMIT,ROLLBACK,
orSAVEPOINTcommands.
➢Italsocannotcallproceduresorfunctions

TRIGGER –Structure

Components of a Trigger
Atriggerhasthreeparts:
1.SQLcommandthatactivatesthetrigger(triggering
event)
ThetriggercanbeactivatedbyaSQLcommandorbya
userevent
2.Triggerrestriction
RepresentedbytheWHENclause,itspecifieswhat
conditionmustbetrueforthetriggertobetriggered.
3.Triggeraction
ThePL/SQLblock,orJavaorCroutineexecutedbythe
trigger.

Types of Triggers
Oracle8ihasfourtypesoftriggers,dependingonthe
application:
➢TabletriggersappliedtoDMLcommands.
➢INSTEADOFtriggersappliedtoviews.
➢SYSTEMEVENTtriggersappliedtothedatabasein
thestartup,shutdown,anderrorhandlingoperations.
➢USEREVENTtriggersappliedtoauserorobject
schema.

Types of Triggers -Table Triggers
➢Therearetwodistincttypesoftriggersthatcanbe
usedinatable.
➢Thefirsttype,statement-levelandtheothertype,row-
level.
➢Statement-leveltriggersaretriggeredonlyonce.For
example,whenanUPDATEcommandupdates15
rows,thecommandscontainedinthetriggerare
executedonlyonce,andnotwitheveryprocessedrow.
➢row-leveltriggersareexecutedonalltherowsthat
areaffectedbythecommandthatenabledthetrigger.

Types of Triggers –Instead of Triggers
➢TheINSTEADOFtriggerisavailableonlyinthe
EnterpriseversionofOracle.

Types of Triggers –System Event and User Event Triggers
➢AtriggeriscreatedwiththeCREATETRIGGER
command.Ithasthefollowingsyntax:

Modifying a Trigger -Enabling/Disabling a Trigger
➢Whenatriggeriscreated,itisautomaticallyenabled
andistriggeredwheneverthetriggeringcommandand
theexecutioncommandistrue.
➢Todisabletheexecutionofthetrigger,usetheALTER
TRIGGERcommandwiththeDISABLEclause.To
enableitagain,usetheENABLEclause:

Deleting a Trigger
➢Todeleteatrigger,usetheDROPTRIGGER
command:
Syntax:
DROPTRIGGERname_of_trigger;
➢Thisremovesthetriggerstructurefromthedatabase

References to Columns Inside a Trigger
➢Youcanaccessthevalueofacolumninsidearow-
leveltrigger.
➢ForanINSERTcommand,thevaluesofthefieldsthat
willbewrittenmustbeprecededby:new.
➢ForaDELETEcommand,thevaluesoftherowfields
thatarebeingprocessedmustbeprecededby:old.
➢ForanUPDATEcommand,theoriginalvaluethatis
beingwrittenisaccessedwith:old.Thenewvalues
thatwillbewrittenmustbeprecededby:new.

Validating the Data Entry
➢createtableemp1(namevarchar(20),salary
number(7),enonumber(5));
➢CREATEORREPLACETRIGGERTESTASAL
BEFOREINSERTORUPDATEOFSALARYON
SYSTEM.EMP1FOREACHROW
BEGIN
IF:NEW.SALARY>8000THEN
RAISE_APPLICATION_ERROR( -
20000,'INCORRECTVALUE');
ENDIF;
END;
/
➢Executetheprogramandtrytoinsertvaluesbelow
8000andabove8000.seetheresult

Validating the Data Entry
➢Here,ifthesalaryisbelow8000acceptedotherwise
thetriggerdisplaystheerrormesssage.

Replicating with Triggers
➢Triggersareanefficientmechanismtoreplicatedata
amongtables.
➢Wecaneasilycreatetriggersthatinsert,delete,or
modifyanothertablebasedontheoperations
performedinthecurrenttable.

Replicating with Triggers -insertion
1.createtableemp3(namevarchar(20),salary
number(7),enonumber(5));
2.CREATETRIGGERREP_INS
AFTERINSERTONEMP1
FOREACHROW
BEGIN
INSERTINTOEMP3VALUES(:NEW.name,
:NEW.salary,:NEW.eno);
END;
3.Executethetrigger
4.insertintoemp1values('ramesh',5000,105);
(Whateverthechangesmadeinemp1,itwillreflectto
emp2.)
5.Select*fromemp3;

Replicating with Triggers -Deletion
1.Createthetrigger
CREATETRIGGERREP_DEL
BEFOREDELETEONEMP1
FOREACHROW
BEGIN
DELETEFROMEMP3
WHEREeno=:OLD.eno;
END;
/
2.Deletetherecordfromemp1.
Deletefromemp1whereeno=105.
3.Checktheemp3table,theeno=105alsodeleted.
Select*fromemp3;

Obtaining Information about a Trigger
➢To view the DBA_TRIGGERS. (using
System/manager )
SQL> descdba_triggers;
➢To view the user’s triggers, you use the view
USER_TRIGGERS:
SQL> describe user_triggers;
➢To view the trigger name and body of the trigger
SQL> select trigger_name,trigger_bodyfrom
user_triggerswhere trigger_name=’REP_INS’;