Upload
yelena-bytenskaya
View
231
Download
0
Embed Size (px)
Citation preview
8/10/2019 PL SQL Packages and Triggers
1/41
8/10/2019 PL SQL Packages and Triggers
2/41
8/10/2019 PL SQL Packages and Triggers
3/41
+ #od!
4ere is the #asic s!ntax for a package speci&cation'
The 567(T7 P(58(97 ke!words are "sed to create the package speci&cation'
(nd the -owning- schema is speci&ed along with the package name*
schema'package:name
;"ltiple p"#lic o#$ects s"ch as f"nctions and proced"res can #e declared'
( "ser can call a p"#lic packaged o#$ect provided he or she has 7
8/10/2019 PL SQL Packages and Triggers
4/41
( package #od! consists of the exec"ta#le code for packaged o#$ects'
P"#lic and private o#$ects are coded in the same wa! in the package #od!'
"t an o#$ect coded in the package #od! is p"#lic if it is declared in the package
speci&cation'
(n application developer m"st have 567(T7 P6%573U67 privilege on the data#ase
to create a p"#lic package'
,o"r 3( can grant the necessar! privilege'
Let=s look at an example package called enroll:new'
4ere is the package speci&cation'
The package is named enroll:new and is owned #! the admission schema'
Two p"#lic proced"res are declared* charge:st" and grade:new'
( package speci&cation is created separatel! from the package #od! #! "sing a
script &le or #! entering the code interactivel! at the SQL> prompt'
,o" can examine package so"rce code and s!ntax errors #! 2"er!ing the
"ser:so"rce and "ser:error views of the owning schema'
4ere is the code for the example package #od!'
8/10/2019 PL SQL Packages and Triggers
5/41
Two proced"res are coded'
(nd #eca"se #oth proced"res were declared in the package speci&cation) the! are
p"#lic package o#$ects'
0hen the script containing the package #od! is created) the packaged proced"res
admission'enroll:new'charge:st" and admission'enroll:new'grade:new can #e called
#! an! application tool in the %racle environment'
3ot notation m"st #e "sed to access a packaged o#$ect*
schema:name'package:name'o#$ect:name
This is how a packaged o#$ect is exec"ted at the SQL> prompt'
8/10/2019 PL SQL Packages and Triggers
6/41
This is how a packaged o#$ect is called from within a PL/SQL #lock or s"#program'
The ke!word 7
8/10/2019 PL SQL Packages and Triggers
7/41
( package #od! is not needed #eca"se all the package contents are datat!pes'
%nce the package has #een created and stored in the data#ase) instances of the
"serde&ned datat!pes can #e declared in an! application'
4ere is an example PL/SQL #lock that declares an instance of the st"dent:record
datat!pe'
It is not necessar! to recode a T,P7 declaration in the calling program'
%racle will o#tain the T,P7 declaration for the "serde&ned datat!pe app:record
from the packaged o#$ect appdev'records'st"dent:record'
8/10/2019 PL SQL Packages and Triggers
8/41
The owning schema of the packaged o#$ect is appdev'
The package name is records'
(nd the packaged o#$ect name is st"dent:record'
,o" can pass an instance of a "serde&ned 675%63 or T(L7 datat!pe into apackaged s"#program as an act"al parameter'
.or example) in a calling program !o" can declare an instance of a "serde&ned
675%63 datat!pe #! calling a packed o#$ect that contains the T,P7 declaration'
(nd then pop"late and pass the 675%63 &elds into another packaged proced"re as
act"al parameters'
The called packaged proced"re appdev'maintain can also declare an instance of the
"serde&ned 675%63 #! calling the appdev'records package'
The important point to remem#er is that !o" can "se a package to contain a data
str"ct"re declaration'
(nd !o" can re"se that data str"ct"re an!where in the %racle environment witho"t
having to recode the declaration'
,o" can pass an instance of a "serde&ned 675%63 or T(L7 datat!pe into a
packaged s"#program as an act"al parameter'
.or example) in a calling program !o" can declare an instance of a "serde&ned
675%63 datat!pe #! calling a packed o#$ect that contains the T,P7 declaration'
(nd then pop"late and pass the 675%63 &elds into another packaged proced"re as
act"al parameters'
8/10/2019 PL SQL Packages and Triggers
9/41
The called packaged proced"re appdev'maintain can also declare an instance of the
"serde&ned 675%63 #! calling the appdev'records package'
The important point to remem#er is that !o" can "se a package to contain a data
str"ct"re declaration'
(nd !o" can re"se that data str"ct"re an!where in the %racle environment witho"t
having to recode the declaration'
%racle allows the same s"#program name to #e "sed for more than one PL/SQL
packaged s"#program) provided the formal parameters are di@erent'
This concept is called overloading'
%verloading can #e "sed to allow a single call to access m"ltiple f"nctions andproced"res'
.or example) a PL/SQL package can contain a proced"re that accepts a A(654(6B
formal parameter'
(nd the same package can contain a second proced"re of the same name that
accepts a NU;76 formal parameter'
%racle resolves overloading #! comparing the act"al parameters s"pplied at r"n
time with the formal parameters declared in the packaged s"#programs'
4ere is an example package that contains two proced"res called del:st"'
8/10/2019 PL SQL Packages and Triggers
10/41
If del:st" is called and has passed a A(654(6B parameter) the &rst proced"re will
exec"te'
(nd if del:st" is passed a NU;76 parameter) the second proced"re will exec"te'
%verloading works onl! if the formal parameters in each s"#program di@er inn"m#er) order) or datat!pe'
S"#t!pes of datat!pe NU;76 are not considered di@erent for the p"rpose of
overloading s"#program names'
%racleC "ses a package called standard to de&ne the PL/SQL environment'
The standard package declares datat!pes) exceptions) f"nctions) and proced"res'
(ll declarations in the standard package are p"#lic and are glo#all! visi#le to all
applications and "sers'
This means that an! o#$ect declared in the standard package is availa#le to an! tool
in the %racle environment that s"pports PL/SQL'
.or example) the no:data:fo"nd exception is declared in the standard package'
(nd it can #e "sed #! an! PL/SQL program'
8/10/2019 PL SQL Packages and Triggers
11/41
SQLDPl"s f"nctions s"ch as T%:NU;76 and 375%37 are also declared in the
standard package'
The standard package is created #! the 3( #! r"nning a script &le called
standard's2l'
The script that creates the standard package is "s"all! stored in the
EoracleErd#msEadmin director!'
%nce it is created) the standard package is stored in parsed format in the data#ase'
The standard package is like an! other stored PL/SQL package'
0hen a "ser calls a packaged o#$ect) the package is loaded and held -resident- in
memor!'
(nother "ser can "se an o#$ect in the package witho"t ca"sing disk I/%'
4ere is an extract from the script "sed to create the standard package'
e caref"l not to declare a f"nction or proced"re with the same name as an o#$ect
in the standard package'
8/10/2019 PL SQL Packages and Triggers
12/41
If !o" do) the local declaration will override the glo#al declaration and the core
f"nctionalit! of PL/SQL ma! #e a@ected'
( n"m#er of other packages are also s"pplied with %racleC'
These packages contain proced"res and f"nctions that can assist the eFcient
development and administration of the data#ase'
The d#ms:o"tp"t package is partic"larl! "sef"l in de#"gging applications'
.or example) !o" can o"tp"t varia#le val"es to screen'
The package d#ms:o"tp"t is created #! exec"ting the script d#msopt's2l'
UNIT B
,o" will now develop a sample PL/SQL package and calling program'
4ere is the speci&cation for the sample package and calling program'
To #egin) let=s examine what the package and calling program sho"ld do logicall!'
The package sho"ld contain a p"#lic f"nction that accepts a 675%63 datat!pe as a
formal parameter and inserts a new row in the st"dent ta#le'
(nd the f"nction sho"ld ret"rn a val"e to the calling program to indicate whether a
row was s"ccessf"ll! inserted'
The package sho"ld also contain a p"#lic proced"re that accepts a social sec"rit!
n"m#er as a parameter and deletes a row from the st"dent ta#le'
8/10/2019 PL SQL Packages and Triggers
13/41
(nd &nall!) the calling program sho"ld call and pass parameters to the packaged
f"nction to create a new st"dent'
That=s what the program sho"ld do logicall!'
The next step is to code the package speci&cation'
The speci&cation sho"ld declare the f"nction and proced"re as p"#lic packaged
o#$ects'
6emem#er) a calling program has access onl! to p"#lic packaged o#$ects declared
in the package speci&cation'
To #egin the speci&cation) a 567(T7 statement sho"ld #e coded'
Next) a 675%63 datat!pe is declared to hold the st"dent details'
It is not necessar! to declare an instance of this 675%63 in the package
speci&cation'
%nce the package is created) instances can #e declared #! an! calling PL/SQL
program'
Next) the f"nction to create a st"dent is declared'
The 567(T7 ke!word is not needed within the package'
(n instance of the 675%63 datat!pe sho"ld #e declared as a formal parameter in
the f"nction'
The 67TU6N cla"se speci&es that a NU;76 datat!pe is ret"rned to the calling
program'
8/10/2019 PL SQL Packages and Triggers
14/41
.inall!) the proced"re to delete a st"dent is declared'
( formal IN parameter is speci&ed in the proced"re declaration'
That=s the end of the package speci&cation'
The script is exec"ted to create and store the package speci&cation in the data#ase'
The next step is to code the package #od! to contain the exec"ta#le code for the
packaged f"nction and proced"re'
.irst) the 567(T7 statement sho"ld #e coded'
Next) the f"nction speci&cation is coded'
8/10/2019 PL SQL Packages and Triggers
15/41
(nd the f"nction #od! is coded'
Next) the proced"re speci&cation is coded'
(nd &nall!) the proced"re #od! is coded'
That=s the end of the package #od!'
8/10/2019 PL SQL Packages and Triggers
16/41
The next step is to code the calling program'
.irst) an instance of the packaged datat!pe is declared'
3ot notation is "sed to reference the packaged o#$ect'
Statements are coded to prompt the "ser to enter r"ntime val"es'
8/10/2019 PL SQL Packages and Triggers
17/41
Next) an I. statement sho"ld #e coded to call the f"nction and pass in the act"al
parameters'
That=s the end of the calling program'
Let=s examine what happens when the calling program is exec"ted'
.irst) a data str"ct"re called local:record is created in memor! "sing the T,P7
declaration contained in the packaged o#$ect appdev'st"dents'st"dent:row'
Next) the "ser is prompted to pop"late local:record with r"ntime val"es'
The packaged f"nction appdev'st"dents'inp"t:rec is called and passed local:record
as an act"al parameter'
The calling program is temporaril! s"spended'
(nd %racle #egins exec"ting the p"#lic packaged f"nctionappdev'st"dents'inp"t:rec'
.irst) the act"al parameter local:record is accepted into the formal parameter
st"dent:record'
The parameter st"dent:record is an instance of the "serde&ned datat!pe
st"dent:row'
st"dent:row was declared as a p"#lic packaged o#$ect in the package speci&cation'
Next) the INS76T statement creates a new row in the #ase ta#le st"dent'
If the insert is s"ccessf"l) the 67TU6N statement ret"rns a val"e of 1 to the calling
program'
If the "pdate failed for an! reason) the exception handler ret"rns a val"e of G to the
calling program'
The packaged f"nction &nishes exec"ting when the ret"rn statement is exec"ted'
8/10/2019 PL SQL Packages and Triggers
18/41
"t the package is still resident in memor!'
(nd an! "ser can call an! o#$ect in the package witho"t ca"sing disk I/%'
The calling program res"mes exec"ting and eval"ates the ret"rn val"e received
from the packaged f"nction'
(nd a s"ita#le message is o"tp"t to screen'
4ere is the r"ntime o"tp"t generated if the insert was carried o"t s"ccessf"ll!'
Look at how PL/SQL s"#stit"tes the r"ntime val"es entered #! the "ser for the
declared s"#stit"tion varia#les'
This sort of feed#ack can #e "sef"l in de#"gging an application'
"t end"sers sho"ld alwa!s #e -shielded- from "nfriendl! messages #! "sing front
end application development tools s"ch as 3eveloper/BGGG .orms'
Interface design is examined in detail in the 5T S!stems co"rse 3eveloper/BGGG
.orms* ."ndamentals'
8/10/2019 PL SQL Packages and Triggers
19/41
PL/SQL s"pports a stored program "nit called a data#ase trigger'
This is stored as a PL/SQL proced"re in the data#ase and is associated with a named
data#ase ta#le'
( data#ase trigger cannot #e associated with a view'
( data#ase trigger is like a stored f"nction or proced"re'
It consists of PL/SQL and SQLDPl"s code'
It can call and pass parameters to a packaged PL/SQL proced"re or f"nction'
4owever) it cannot #e called or exec"ted #! a "ser or application program'
( data#ase trigger is independent of "sers and applications'
( data#ase trigger is exec"ted or &red a"tomaticall! #! %racle when prede&ned
SQLDPl"s 3;L Hdata manip"lation lang"age statements are exec"ted on theassociated #ase ta#le'
This means that a trigger can &re onl! when one or more of these 3;L statements
are exec"ted*
+ INS76T
+ UP3(T7
+ 37L7T7
( data#ase trigger cannot &re when a S7L75T statement is exec"ted'
( data#ase trigger sho"ld not #e conf"sed with a 3eveloper/BGGG .orms trigger' The
role of a trigger in the 3eveloper/BGGG .orms environment is examined in the 5T
S!stems co"rse 3eveloper/BGGG .orms* ."ndamentals'
3ata#ase triggers can help protect the integrit! of the data#ase in several wa!s'
( data#ase trigger can carr! o"t complex validation to enforce #"siness r"les'
.or example) it can &re to prevent an application from inserting data that does not
meet prede&ned criteria'
( data#ase trigger can #e "sed to carr! o"t event logging'
.or example) in a transaction processing environment) it can insert data a#o"t 3;L
operations in an event log ta#le'
( data#ase trigger can help the 3( to control 3;L operations'
8/10/2019 PL SQL Packages and Triggers
20/41
8/10/2019 PL SQL Packages and Triggers
21/41
,o" m"st #e granted the appropriate privilege #! the 3( to s"ccessf"ll! create a
data#ase trigger'
The privilege 567(T7 T6I9976 allows !o" to create a trigger in !o"r schema'
(nd 567(T7 (N, T6I9976 allows !o" to create a trigger in another schema'
( "ser or application does not re2"ire privileges on a data#ase trigger or on an!
ta#les accessed #! a trigger'
The privileges of the schema that -owns- the trigger are implicitl! granted for the
d"ration of the exec"tion of the trigger'
( trigger can &re #efore or after a 3;L operation'
( 7.%67 trigger is &red #efore the triggering 3;L statement is exec"ted'
So) it can #e "sed to determine whether to a#ort the triggering statement'
It can also #e "sed to derive val"es #efore an INS76T or UP3(T7 statement is
exec"ted'
(n (.T76 trigger is &red after the triggering 3;L statement has exec"ted'
It can #e "sed to enforce integrit! constraints or log 3;L activit!'
( trigger can &re on INS76T) UP3(T7) or 37L7T7 statements'
(nd a trigger can &re at 6%0 level or at ST(T7;7NT level'
In all) there are twelve possi#le triggering events for each ta#le'
8/10/2019 PL SQL Packages and Triggers
22/41
So) !o" can create a maxim"m of twelve triggers on an! ta#le'
In releases of %racle prior to C'1) !o" cannot create more than one trigger t!pe per
ta#le'
4ere is the #asic algorithm that %racle "ses when &ring stored data#ase triggers'
( data#ase trigger is a failsafe mechanism to protect data integrit!'
( trigger is not a s"#stit"tion for proper ta#le de&nition or ade2"ate sec"rit! and
data validation techni2"es'
6eferential integrit! constraints sho"ld contin"e to #e incl"ded in the 567(T7 T(L7statement'
In fact) a trigger cannot override constraints or change data in a constraining ta#le
referenced in the triggering statement'
( ta#le is constrained if it de&nes a foreign ke! relationship with another ta#le'
8/10/2019 PL SQL Packages and Triggers
23/41
Sec"rit! and data validation sho"ld #e implemented in the frontend application
#efore #ase ta#le data manip"lation is attempted'
(n exception raised in a trigger sho"ld #e handled at trigger level #! coding an
exception section in the trigger #od!'
(n "nhandled exception will ca"se a complete roll#ack of the triggering statement'
eca"se data#ase triggers ca"se a performance overhead) the! sho"ld #e "sed
with discretion'
It is the responsi#ilit! of the 3( to develop a comprehensive strateg! for the
enforcement of data integrit! r"les'
(nd it is the responsi#ilit! of application developers to ens"re that programs are
-triggeraware-'
( data#ase trigger has some limitations'
( data#ase trigger cannot iss"e a S7L75T statement to a ta#le that is m"tating'
( #ase ta#le that is in the process of changing is said to #e m"tating'
.or example) a trigger that is in the process of &ring cannot read from the ta#le it is
&ring on'
(ltho"gh !o" can "se SQLDPl"s 3;L statements in a trigger) !o" cannot "se 33L or
35L statements'
.or example) statements s"ch as 567(T7 T(L7 and 96(NT S7L75T are nots"pported in a data#ase trigger'
(nd !o" cannot "se %racle=s transaction processing options* 5%;;IT) 6%LL(58)
and S(A7P%INT'
%racle s"pports a ST(T7;7NT data#ase trigger that &res once when a triggering
3;L statement is exec"ted'
( ST(T7;7NT trigger can #e speci&ed to &re #efore or after an INS76T) UP3(T7) or
37L7T7 statement'
4ere is an example 7.%67 INS76T ST(T7;7NT trigger'
8/10/2019 PL SQL Packages and Triggers
24/41
ST(T7;7NT is the defa"lt trigger t!pe'
( 567(T7 T6I9976 statement is "sed to create and name the ST(T7;7NT trigger'
The trigger will &re when the event -7.%67 INS76T %N st"dent- occ"rs'
( trigger restriction cannot #e coded in a ST(T7;7NT trigger'
The I. statement in the example trigger #od! tests whether %racle=s internal date
S,S3(T7 is greater than =J(N1K='
(nd if it is) the trigger is forced to fail and the triggering 3;L statement is a#orted'
The example trigger code is saved as a script and exec"ted at the SQL> prompt to
create and store the trigger in the data#ase'
4ere is the feed#ack generated when the example ST(T7;7NT trigger is ca"sed to
&re #! an INS76T statement on the st"dent ta#le'
4ere is an example of an (.T76 37L7T7 ST(T7;7NT trigger'
8/10/2019 PL SQL Packages and Triggers
25/41
8/10/2019 PL SQL Packages and Triggers
26/41
The ke!words .%6 7(54 6%0 specif! that the trigger &res for each row a@ected #!
the triggering event'
6emem#er) %racle creates a ST(T7;7NT trigger #! defa"lt if the ke!words .%6
7(54 6%0 are omitted'
The trigger #od! I. statement will insert a row in the de#ts:o"tstanding ta#le after
each row is deleted) provided the deleted loan:amo"nt is greater than ?ero'
Look again at the I. statement condition in the example 6%0 trigger'
This code "ses a notation called a correlation name'
It can #e "sed in a 6%0 trigger to reference a data element that is in the process of
m"tating'
In this case) the correlation name %L3 references data in a row that has $"st #een
logicall! deleted #! the triggering statement'
(nd loan:amo"nt references the #ase ta#le col"mn in the -deleted- row'
( correlation name is preceded #! a colon H*) "nless it is "sed in a trigger
restriction'
,o" can reference a deleted row in a trigger "sing the correlation name %L3
#eca"se the deletion has not !et #een committed to the data#ase'
Think a#o"t what happens at r"ntime'
This statement ca"ses all the rows in the st"dent ta#le to #e deleted'
8/10/2019 PL SQL Packages and Triggers
27/41
(nd after each row is deleted) the example trigger &res once'
If the val"e of *%L3'loan:amo"nt in the logicall! deleted row is greater than ?ero)
the trigger inserts a row in the de#ts:o"tstanding ta#le'
In an INS76T 6%0 trigger there is no old val"e) #"t !o" can "se the N70 correlation
name to refer to an inserted data element* *N70'col"mn:name
In an UP3(T7 6%0 trigger !o" can "se #oth the %L3 and N70 correlation names to
compare data val"es #efore and after the "pdate) for example I.
H*%L3'col"mn:name > *N70'col"mn:name T47N'''
In a 37L7T7 6%0 trigger) there is no new val"e) so onl! the %L3 correlation name
can #e "sed'
%racle does not s"pport correlation names in a ST(T7;7NT trigger'
%racle allows the defa"lt correlation names to #e renamed "sing this s!ntax in the6%0 trigger event* 67.767N5IN9 MN70 %L3O (S 67.767N57:N(;7
4ere is an example of a trigger that "ses referencing to -rename- the correlation
names %L3 and N70'
This techni2"e can #e "sed to avoid potential naming conicts'
( trigger restriction can #e "sed in a 6%0 trigger to determine whether to exec"te
the trigger #od! for each row a@ected #! the triggering statement'
4ere is the s!ntax for a 6%0 trigger restriction'
The trigger condition can #e an! valid #oolean PL/SQL expression) #"t cannot
incl"de a 2"er!'
Look now at this example (.T76 UP3(T7 6%0 trigger that "ses a restriction'
8/10/2019 PL SQL Packages and Triggers
28/41
( colon is not "sed with a correlation name in a 6%0 trigger restriction'
The trigger restriction is eval"ated for each row a@ected #! the 3;L statement'
If the restriction eval"ates to T6U7) the trigger #od! is exec"ted and a row isinserted in the refer:de#t:collector ta#le'
(nd if the restriction eval"ates to .(LS7) the next row is considered'
( ST(T7;7NT trigger does not s"pport restriction'
Look again at the example trigger event'
The loan:amo"nt col"mn in the st"dent ta#le has #een speci&ed in the trigger
event'
This means that the trigger will &re onl! if data in that col"mn is a@ected #! anUP3(T7 statement'
,o" can specif! more than one #ase ta#le col"mn in an UP3(T7 trigger event'
If !o" omit a col"mn reference) the trigger will &re when an! col"mn in the ta#le is
a@ected #! a triggering 3;L statement'
8/10/2019 PL SQL Packages and Triggers
29/41
%racle allows the "se of the following conditional predicates in a ST(T7;7NT or
6%0 trigger that has more than one triggering event*
+ I. INS76TIN9 T47N'''
+ I. UP3(TIN9 T47N'''
+ I. 37L7TIN9 T47N'''
This means that !o" can "se a di@erent set of trigger #od! statements for each
trigger event'
This is the #asic s!ntax "sed in a trigger #od! to identif! the triggering event and
exec"te the related statements'
Predicates can #e com#ined* I. HINS76TIN9 %6 UP3(TIN9 T47N'''
4ere is an example trigger that "ses conditional predicates to determine which set
of trigger #od! statements sho"ld #e exec"ted'
The trigger event speci&es that the trigger will &re #efore an INS76T) UP3(T7) or
37L7T7 statement is exec"ted on the st"dent ta#le'
8/10/2019 PL SQL Packages and Triggers
30/41
The conditional predicates are "sed to identif! the triggering event'
0hen the trigger is ca"sed to &re #! an INS76T or UP3(T7 statement) the &rst set of
statements are exec"ted'
The f"nction INIT5(P is "sed to ens"re that st"dent names are inserted into the
#ase ta#le in the correct case'
0hen the trigger is ca"sed to &re #! a 37L7T7 statement) a row is inserted in the
st"dent:archive ta#le'
,o" can also "se the conditional predicate to identif! whether a speci&c #ase ta#le
col"mn has #een a@ected #! a triggering 3;L statement'
3ata#ase performance can #e a@ected #! the extra processing overhead generated
#! a data#ase trigger'
Performance can #e a partic"lar pro#lem if a trigger is complex or if a large amo"nt
of data is loaded at once'
In fact) it ma! #e expedient to temporaril! disa#le a data#ase trigger #efore
carr!ing o"t #"lk loading of data'
The %racle "tilit! SQLDLoader can #e "sed to #"lkload data'
,o" can disa#le all triggers on a ta#le'
%r !o" can disa#le an individ"al trigger'
8/10/2019 PL SQL Packages and Triggers
31/41
If !o" decide to disa#le a #ase ta#le trigger for the d"ration of a data load) it is
important that another method is "sed to validate the data'
The 3( onl! sho"ld decide whether a trigger sho"ld #e disa#led'
Triggers on a ta#le can #e reena#led "sing the 7N(L7 ke!word'
(nd a trigger can also #e reena#led individ"all!'
,o" can delete a trigger from the data#ase "sing this s!ntax* 36%P T6I9976
trigger:name
0hen a trigger is created) the so"rce code is stored in the data#ase'
,o" can examine so"rce code and other trigger details #! 2"er!ing these defa"lt
views*
+ "ser:triggers
+ d#a:triggers
+ all:triggers
4ere is the data de&nition of the "ser:triggers view'
8/10/2019 PL SQL Packages and Triggers
32/41
This S7L75T statement retrieves information on the triggers in the active schema'
The 3( can examine details of an! trigger #! "sing the views d#a:triggers and
all:triggers'
UNIT R
,o" will now develop a sample data#ase trigger'
The trigger sho"ld insert a row in the grade:trans:log ta#le whenever a row is
deleted from the grade ta#le'
The p"rpose of this trigger is to maintain a transaction log'
The sample trigger sho"ld #e created on the grade ta#le'
The most appropriate trigger to "se here is an (.T76 37L7T7 6%0 trigger'
(n (.T76 trigger is generall! more eFcient than a 7.%67 trigger #eca"se a single
data#ase call is generated'
( 6%0 trigger is generall! more "sef"l than a ST(T7;7NT trigger #eca"se it can &re
once for each row a@ected #! the triggering statement'
8/10/2019 PL SQL Packages and Triggers
33/41
The &rst step in creating the sample trigger is to name the trigger grade:trigger'
Next) a trigger event sho"ld #e coded'
The trigger will &re when the event -after delete on grade- occ"rs'
The trigger sho"ld &re once after each row is deleted'
The ke!words .%6 7(54 6%0 specif! that the trigger is a 6%0 trigger that will &re
once after each row is deleted'
The next step in coding the sample trigger is the trigger #od!'
(n INS76T statement is coded to create a new row in the grade:trans:log ta#le after
each deletion from the grade ta#le'
5orrelation names sho"ld #e "sed in the A(LU7S cla"se of the INS76T statement to
reference each row as it is deleted from the grade ta#le'
The correlation name %L3 refers to the row that has $"st #een logicall! deleted #!
the triggering statement'
The trigger is now completed'
8/10/2019 PL SQL Packages and Triggers
34/41
The trigger code can #e saved as a script and exec"ted at the SQL> prompt to
create and store the trigger in the data#ase'
This SQL statement ca"ses fo"r rows to #e deleted from the grade ta#le'
The triggering statement deletes fo"r rows and ca"ses the trigger event -after
delete on grade-'
So the trigger is &red fo"r times) once for each row a@ected #! the triggering
statement'
( S7L75T statement on the grade:trans:log ta#le veri&es that the trigger &red
s"ccessf"ll!'
8/10/2019 PL SQL Packages and Triggers
35/41
8/10/2019 PL SQL Packages and Triggers
36/41
8/10/2019 PL SQL Packages and Triggers
37/41
8/10/2019 PL SQL Packages and Triggers
38/41
8/10/2019 PL SQL Packages and Triggers
39/41
8/10/2019 PL SQL Packages and Triggers
40/41
8/10/2019 PL SQL Packages and Triggers
41/41