8/12/2019 Ppt Db25 Oracle 03
1/98
PL/SQLOracle Day 3
8/12/2019 Ppt Db25 Oracle 03
2/98
2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
O()ecti*es
o e+plain PL/SQL s (progra-s nits
. Proce! res
. nctions
. Pac ages
. Data(ase riggers
8/12/2019 Ppt Db25 Oracle 03
3/98
3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
O*er*ie1 of S (progra-s
s (progra-. Is a na-e! PL/SQL (loc that can accept para-eters an! (e
in*o e! fro- a calling en*iron-ent. Is of t1o types
4 proce! re that perfor-s an action 4 f nction that co-p tes a *al e
. Is (ase! on stan!ar! PL/SQL (loc str ct re
. Pro*i!es -o! larity, re sa(ility, e+tensi(ility,an! -aintaina(ility
. Pro*i!es easy -aintenance, i-pro*e! !ata sec rity an!integrity, i-pro*e! perfor-ance, an! i-pro*e! co!e clarity
8/12/2019 Ppt Db25 Oracle 03
4/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
$loc Str ct re for nony-o sPL/SQL $loc s
D"CL #" 6optional7Declare PL/SQL o()ects to (e se!1ithin this (loc
$"8I& 6-an!atory7Define the e+ec ta(le state-ents
"9C"P IO& 6optional7Define the actions that ta e place if an error or e+ception arises
"&D: 6-an!atory7
8/12/2019 Ppt Db25 Oracle 03
5/98
5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
$loc Str ct re for PL/SQL S (progra-s
IS ; SDeclaration section
$"8I&"+ec ta(le section
"9C"P IO&"+ception section
"&D:
Subprogram Specification
Subprogram Body
8/12/2019 Ppt Db25 Oracle 03
6/98
8/12/2019 Ppt Db25 Oracle 03
7/98
>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
$enefits of S (progra-s
. "asy -aintenance
. I-pro*e! !ata sec rity an! integrity
. I-pro*e! perfor-ance
. I-pro*e! co!e clarity
8/12/2019 Ppt Db25 Oracle 03
8/98
?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
@hat Is a Proce! reA
. proce! re is a type of s (progra- that perfor-s an action'
. proce! re can (e store! in the !ata(ase, as a sche-a o()ect, for repeate!e+ec tion'
8/12/2019 Ppt Db25 Oracle 03
9/98
BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Synta+ for Creating Proce! res
C!"A#" $%! !"PLAC"& P!%C"'(!" procedurename ) parameter1 $mode & datatype1, parameter2 $mode & datatype2, . . . *+S ASPL/SQL B ock-
. #he !"PLAC" option indicates that if the procedure e.ists, itwi be dropped and rep aced with the new version created bythe statement
. PL/SQL b ock starts with either B"0+1 or the dec aration ofoca variab es and ends with either "1' or "1'
procedurename.
8/12/2019 Ppt Db25 Oracle 03
10/98
0Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
De*eloping Proce! res
2"ditor
Code to create
procedure
filename.sql
SQL3P us
4 Load and e.ecute file.sql
Source code
Compi e
P code
(se S5%6 "!!%!Sto view
compi ation errorsProcedure
created
".ecute 7
%rac e
8/12/2019 Ppt Db25 Oracle 03
11/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
or-al %ers s ct al Para-eters
. or-al para-eters *aria(les !eclare! in the para-eter list of
a s (progra- specification"+a-ple
C#" " P#OC"D #" inEsalary6pEi! & F$"#, pEa-o nt& F$"#7
'''"&D incEsalary:
. ct al para-eters *aria(les or e+pressions reference! in thepara-eter list of a s (progra- call
"+a-ple
inEsalary6*Ei!, 20007
8/12/2019 Ppt Db25 Oracle 03
12/98
2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Proce! ral Para-eter Fo!es
Ca ingenvironment
Procedure
)'"CLA!"*
B"0+1
"8C"P#+%1
"1'-
+1 parameter
%(# parameter
+1 %(# parameter
8/12/2019 Ppt Db25 Oracle 03
13/98
3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Creating Proce! res 1ith Para-eters
Can be assigned adefau t va ue
Actua parameter can bea itera , e.pression,constant, or initia i9ed
variab e
+nitia i9ed variab e(ninitia i9edvariab e
:orma parameter acts asa constant
Passed intosubprogram-returned to ca ingenvironment
!eturned toca ingenvironment
;a ue is passed intosubprogram
'efau t mode
+1 %(#%(#+1
8/12/2019 Ppt Db25 Oracle 03
14/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
I& Para-eters "+a-ple
p=id2>?
C!"A#" %! !"PLAC" P!%C"'(!" inc=sa ary )p=id +1 emp emp=id@# P"*
+SB"0+1 (P'A#" emp S"# sa ary sa ary 3 2 2 65"!" emp=id p=id-"1' inc=sa ary-
/
8/12/2019 Ppt Db25 Oracle 03
15/98
5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
O Para-eters "+a-ple
Ca ing environment QUERY_EMP procedure
p_id
p_name
p_salary
p_incentives
2DE
!am
DD
7
8/12/2019 Ppt Db25 Oracle 03
16/98
8/12/2019 Ppt Db25 Oracle 03
17/98
>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
%ie1ing O Para-eters
. Load and run the emp=Fuery sF script fi e to create theQ("! 2 procedure
. 'ec are host variab es, e.ecute the Q("! 2 procedure, andprint the va ue of the g oba 0=1A
8/12/2019 Ppt Db25 Oracle 03
18/98
?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
%ie1ing I& O Para-eters
;A!+ABL" g=phone ;A!C5A!4)2G*B"0+1
Hg=phone H IE2EDDG4? 7GDJ-"1'-
/P!+1# g=phone"8"C(#" Phone=proc )Hg=phone*P!+1# g=phone
8/12/2019 Ppt Db25 Oracle 03
19/98
BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Fetho!s for Passing Para-eters
. Positional List act al para-eters in the sa-e or!er asfor-al para-eters'
. &a-e! List act al para-eters in ar(itrary or!er (yassociating each 1ith its correspon!ing for-al para-eter'
. Co-(ination List so-e of the act al para-eters as
positional an! so-e as na-e!'
8/12/2019 Ppt Db25 Oracle 03
20/98
20Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
D" L Option for Para-eters
C!"A#" %! !"PLAC" P!%C"'(!" add=department )p=name +1 dep dep=name@# P" '":A(L# IabcJ, p= oc +1 dep oc=id@# P" '":A(L# 2E *+SB"0+1 +1S"!# +1#% dep)dep=id, dep=name, oc=id* ;AL("S )seF2 1"8#;AL, p=name, p= oc*-"1' add=department-
/
8/12/2019 Ppt Db25 Oracle 03
21/98
2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
"+a-ples of Passing Para-eters
B"0+1 add=department- add=department )J#!10J, 7G *- add=department ) p= oc K 7 , p=name KJ"'(J*- add=department ) p= oc K 74 * -"1'-
/
S"L"C# dep=id, dep=name, oc=id:!%< dep-
8/12/2019 Ppt Db25 Oracle 03
22/98
22Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" %! !"PLAC" P!%C"'(!" emp = eave )p=id +1 emp emp=id@# P"*+S P!%C"'(!" og=e.ec2 +S B"0+1 +1S"!# +1#% og=tab e2 )user=id, og=date* ;AL("S )(S"!, S S'A#"*- "1' og=e.ec2-B"0+1 '"L"#" :!%< emp 65"!" emp=id p=id- og=e.ec2-"1' emp= eave-
/
'ec aring Subprograms
8/12/2019 Ppt Db25 Oracle 03
23/98
23Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
In*o ing a Proce! re fro- an nony-o s PL/SQL$loc
'"CLA!" v=id 1(
8/12/2019 Ppt Db25 Oracle 03
24/98
2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
In*o ing a Proce! re fro- nother Proce! re
C!"A#" %! !"PLAC" P!%C"'(!" emp=process+S C(!S%! c2 +S S"L"C# emp=id :!%< emp -
B"0+1 :%! i +1 c2L%%P
inc=sa ary)emp=rec emp oyee=id*- "1' L%%P-
C%
8/12/2019 Ppt Db25 Oracle 03
25/98
25Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
#e-o*ing Proce! res
Synta+
"+a-ple
'!%P P!%C"'(!" procedurename
'!%P P!%C"'(!" inc=sa ary-
Drop a proce! re store! in the !ata(ase'
8/12/2019 Ppt Db25 Oracle 03
26/98
2
8/12/2019 Ppt Db25 Oracle 03
27/98
2>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" $%! !"PLAC"& :(1C#+%1 functionname ) parameter1 $mode & datatype1, parameter2 $mode & datatype2, . . . *
!"#(!1 datatype+S ASPL/SQL B ock-
#he PL/SQL b ock must have at east one !"#(!1 statement
Synta+ for Creating nctions
8/12/2019 Ppt Db25 Oracle 03
28/98
2?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Creating a nction
+nvoke 7
2"ditor
Code to create
function
i SQL3P us
4 Load and e.ecute filename.sql
Source code
Compi e
P code :unctioncreated
%rac e
8/12/2019 Ppt Db25 Oracle 03
29/98
2BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
"+ec ting nctions
. In*o e a f nction as part of a PL/SQL e+pression'
. Create a *aria(le to hol! the ret rne! *al e'
. "+ec te the f nction' he *aria(le 1ill (e pop late! (y the *al e ret rne!thro gh a RETURN state-ent'
8/12/2019 Ppt Db25 Oracle 03
30/98
30Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
!*antages of ser=Define! nctions in SQL"+pressions
. "+ten! SQL 1here acti*ities are too co-ple+, too a1 1ar!,or na*aila(le 1ith SQL
. Can increase efficiency 1hen se! in the WHERE cla se tofilter !ata, as oppose! to filtering the !ata in the application
. Can -anip late character strings
8/12/2019 Ppt Db25 Oracle 03
31/98
3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" %! !"PLAC" :(1C#+%1 ta.ca c)p=va +1 1(
8/12/2019 Ppt Db25 Oracle 03
32/98
32Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Locations to Call ser=Define! nctions
. Select list of a SELECT co--an!
. Con!ition of the WHERE an! HAVING cla ses CONNECT BY , START WITH , ORDER BY, an! GROUP BY
cla ses VALUES cla se of the INSERT co--an!
SET cla se of the UPDATE co--an!
8/12/2019 Ppt Db25 Oracle 03
33/98
33Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
#estrictions on Calling nctions fro- SQL"+pressions GG''
o (e calla(le fro- SQL e+pressions, a ser=!efine!f nction - st. $e a store! f nction. ccept only IN para-eters. ccept only *ali! SQL !ata types, not PL/SQL specific types,
as para-eters. #et rn !ata types that are *ali! SQL !ata types, not PL/SQL
specific types
8/12/2019 Ppt Db25 Oracle 03
34/98
3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
#estrictions on Calling nctions fro- SQL"+pressions
. nctions calle! fro- SQL e+pressions cannot contain DFLstate-ents'
. nctions calle! fro- UPDATE/DELETE state-ents on a ta(le cannot contain DFL on the sa-e ta(le '
. nctions calle! fro- an UPDATE or a DELETE state-ent on a
ta(le cannot H ery the sa-e ta(le'. nctions calle! fro- SQL state-ents cannot contain
state-ents that en! the transactions'. Calls to s (progra-s that (rea the pre*io s restriction are
not allo1e! in the f nction'
8/12/2019 Ppt Db25 Oracle 03
35/98
35Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
#estrictions on Calling fro- SQL
C!"A#" %! !"PLAC" :(1C#+%1 f2 )p=sa ary 1(
8/12/2019 Ppt Db25 Oracle 03
36/98
3
8/12/2019 Ppt Db25 Oracle 03
37/98
3>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
o1 Proce! res an! nctions Differ
Procedure
)'"CLA!"*
B"0+1
"8C"P#+%1
"1'-
+1 parameter
%(# parameter
+1 %(# parameter
Ca ingenvironment
Ca ingenvironment
:unction
)'"CLA!"*
B"0+1
"8C"P#+%1
"1'-
+1 parameter
8/12/2019 Ppt Db25 Oracle 03
38/98
3?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Procedures
".ecute as a PL/SQLstatement
'o not contain !"#(!1
c ause in the header
Can return none, one, ormany va ues
Can contain a !"#(!1statement
:unctions
+nvoke as part of ane.pression
8/12/2019 Ppt Db25 Oracle 03
39/98
3BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
$enefits of Store!Proce! res an! nctions
. I-pro*e! perfor-ance
. "asy -aintenance
. I-pro*e! !ata sec rity an! integrity
. I-pro*e! co!e clarity
8/12/2019 Ppt Db25 Oracle 03
40/98
0Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
List ll Proce! res an! nctions
S"L"C# obNect=name, obNect=type:!%< user=obNects65"!" obNect=type in )JP!%C"'(!"J,J:(1C#+%1J*%!'"! B obNect=name-
8/12/2019 Ppt Db25 Oracle 03
41/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Co umn
1A
8/12/2019 Ppt Db25 Oracle 03
42/98
2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
List the Co!e of Proce! resan! nctions
S"L"C# te.t:!%< user=source65"!" name IP2J%!'"! B ine-
8/12/2019 Ppt Db25 Oracle 03
43/98
3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
List Co-pilation "rrors (y singS O@ "##O#S
S5%6 "!!%!S P!%C"'(!" p2
8/12/2019 Ppt Db25 Oracle 03
44/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
De( gging PL/SQL Progra- nits
. he DBMS_OUTPUT pac age 4 cc - lates infor-ation into a ( ffer 4 llo1s retrie*al of the infor-ation fro- the ( ffer
. tono-o s proce! re calls 6for e+a-ple, 1riting the o tp tto a log ta(le7
. Soft1are that ses DBMS_DEBUG 4 Proce! re $ il!er 4 hir!=party !e( gging soft1are
8/12/2019 Ppt Db25 Oracle 03
45/98
5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
O*er*ie1 of Pac ages
Pac ages. 8ro p logically relate! PL/SQL types, ite-s, an!
s (progra-s. Consist of t1o parts
4 Specification
4 $o!y. Cannot (e in*o e!, para-eteriJe!, or neste!. llo1 the Oracle ser*er to rea! - ltiple o()ects into -e-ory
at once
8/12/2019 Ppt Db25 Oracle 03
46/98
8/12/2019 Ppt Db25 Oracle 03
47/98
>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
De*eloping a Pac age
. Sa*ing the te+t of the CREATE PACKAGE state-ent in t1o!ifferent SQL files facilitates later -o!ifications to thepac age'
. pac age specification can e+ist 1itho t a pac age (o!y,( t a pac age (o!y cannot e+ist 1itho t a pac age
specification'
8/12/2019 Ppt Db25 Oracle 03
48/98
?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" $%! !"PLAC"& PAC A0" package_name+S AS public type and item declarations subprogram specifications
"1' package_name;
Creating the Pac age Specification
. he #"PL C" option !rops an! recreates the pac agespecification'
. %aria(les !eclare! in the pac age specification are initialiJe! to& LL (y !efa lt'
. ll the constr cts !eclare! in a pac age specification are *isi(le tosers 1ho are grante! pri*ileges on the pac age'
Synta.H
8/12/2019 Ppt Db25 Oracle 03
49/98
BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
COMM_P"C#"$E package
$_COM
Packagespecification
2
Declaring P (lic Constr cts
RE%ET_COM proceduredec aration
4
8/12/2019 Ppt Db25 Oracle 03
50/98
50Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Creating a Pac age Specification "+a-ple
C!"A#" %! !"PLAC" PAC A0" com=pack +S g=com 1(
8/12/2019 Ppt Db25 Oracle 03
51/98
5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Creating the Pac age $o!y
Synta+
C!"A#" $%! !"PLAC"& PAC A0" B%' package_name+S AS private type and item declarations subprogram bodies
"1' package_name;
. #he !"PLAC" option drops and recreates the package body
. +dentifiers defined on y in the package body are privateconstructs #hese are not visib e outside the package body
.A private constructs must be dec ared before they are usedin the pub ic constructs
8/12/2019 Ppt Db25 Oracle 03
52/98
52Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
RE%ET_COM procedure dec aration
&"'ID"TE_COM function definition
Packagespecification
Packagebody
2
7
4RE%ET_COM procedure definition
COMM_P"C#"$E package
P (lic an! Pri*ate Constr cts
$_COM
4
$ l
8/12/2019 Ppt Db25 Oracle 03
53/98
53Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Creating a Pac age $o!y "+a-ple
C!"A#" %! !"PLAC" PAC A0" B%' com=pack+S :(1C#+%1 va idate=comm )p=com +1 1(
8/12/2019 Ppt Db25 Oracle 03
54/98
5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
P!%C"'(!" reset=com )p=com +1 1(
8/12/2019 Ppt Db25 Oracle 03
55/98
55Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
In*o ing Pac age Constr cts
"+a-ple In*o e a f nction fro- a proce! re 1ithinthe sa-e pac age'
CRE"TE OR REP'"CE P"C#"$E (ODY c)m_pac* I%+ + +
PROCEDURE reset_c)m ,p_c)m IN NUM(ER- I% (E$IN IF validate_c)m,p_c)m- T.EN /_c)m 01 p_c)m! E'%E
R"I%E_"PP'IC"TION_ERROR,2343335 6C)mmissi)n Invalid 7-! END IF! END reset_c)m!END c)m_pac*!
# *i P
8/12/2019 Ppt Db25 Oracle 03
56/98
5
8/12/2019 Ppt Db25 Oracle 03
57/98
5>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
8 i!elines for De*eloping Pac ages
. Constr ct pac ages for general se'
. Define the pac age specification (efore the (o!y'
. he pac age specification sho l! contain only thoseconstr cts that yo 1ant to (e p (lic'
. Place ite-s in the !eclaration part of the pac age (o!y 1henyo - st -aintain the- thro gho ta session or across transactions'
. Changes to the pac age specification reH irereco-pilation of each referencing s (progra-'
. he pac age specification sho l! contain as fe1 constr ctsas possi(le'
!* t f P
8/12/2019 Ppt Db25 Oracle 03
58/98
5?Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
!*antages of Pac ages
. Fo! larity "ncaps late relate! constr cts'
. "asier application !esign Co!e an! co-pile specificationan! (o!y separately'
. i!ing infor-ation 4 Only the !eclarations in the pac age specification are
*isi(le an! accessi(le to applications'
4 Pri*ate constr cts in the pac age (o!y are hi!!en an!inaccessi(le'
4 ll co!ing is hi!!en in the pac age (o!y'
!* t f P
8/12/2019 Ppt Db25 Oracle 03
59/98
5BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
!*antages of Pac ages
. !!e! f nctionality Persistency of *aria(lesan! c rsors
. $etter perfor-ance 4 he entire pac age is loa!e! into -e-ory 1hen the
pac age is first reference!' 4 here is only one copy in -e-ory for all sers'
4 he !epen!ency hierarchy is si-plifie!'. O*erloa!ing F ltiple s (progra-s of the
sa-e na-e
O* l !i g
8/12/2019 Ppt Db25 Oracle 03
60/98
8/12/2019 Ppt Db25 Oracle 03
61/98
8/12/2019 Ppt Db25 Oracle 03
62/98
8/12/2019 Ppt Db25 Oracle 03
63/98
8/12/2019 Ppt Db25 Oracle 03
64/98
8/12/2019 Ppt Db25 Oracle 03
65/98
8/12/2019 Ppt Db25 Oracle 03
66/98
8/12/2019 Ppt Db25 Oracle 03
67/98
Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
sing &ati*e Dyna-ic SQL
Dyna-ic SQL. Is a SQL state-ent that contains *aria(les that can change
! ring r nti-e. Is a SQL state-ent 1ith placehol!ers an! is store! as a
character string. "na(les general=p rpose co!e to (e 1ritten
. "na(les !ata=!efinition, !ata=control, or session=controlstate-ents to (e 1ritten an! e+ec te! fro- PL/SQL
. Is 1ritten sing either DBMS_SQL or nati*e !yna-ic SQL
"+ec tion lo1
8/12/2019 Ppt Db25 Oracle 03
68/98
8/12/2019 Ppt Db25 Oracle 03
69/98
8/12/2019 Ppt Db25 Oracle 03
70/98
>0Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
sing the D$FSEDDL Pac age
he D$FSEDDL Pac age. Pro*i!es access to so-e SQL DDL state-ents fro- store!
proce! res. Incl !es so-e proce! res
4 L "#ECOFPIL" 6o()ectEtype, o1ner, o()ectEna-e7
4 & LK "EO$M"C 6o()ectEtype, o1ner, na-e, -etho!7
&ote his pac age r ns 1ith the pri*ileges of callingser, rather than the pac age o1ner SKS'
'B
8/12/2019 Ppt Db25 Oracle 03
71/98
>Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
he DBMS_OUTPUT pac age ena(les yo to o tp t
-essages fro- PL/SQL (loc s' *aila(leproce! res
incl !e
PUT
NEW_LINE
PUT_LINE
GET_LINE
GET_LINES
ENABLE/DISABLE
(sing the 'B
8/12/2019 Ppt Db25 Oracle 03
72/98
>2Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
trigger
. Is a PL/SQL (loc or a PL/SQL proce! re associate! 1ith ata(le, *ie1, sche-a, or the !ata(ase
. "+ec tes i-plicitly 1hene*er a partic lar e*ent ta es place
. Can (e either
4 pplication trigger ires 1hene*er an e*ent occ rs 1ith apartic lar application
4 Data(ase trigger ires 1hene*er a !ata e*ent 6s ch as DFL7 orsyste- e*ent 6s ch as logon or sh t!o1n7 occ rs on a sche-a or!ata(ase
'atabase #riggers
0 id i f ' i i # i
8/12/2019 Ppt Db25 Oracle 03
73/98
>3Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
.Design triggers to
4Perfor- relate! actions
4CentraliJe glo(al operations
.Do not !esign triggers
4@here f nctionality is alrea!y ( ilt into the Oracle
ser*er 4 hat ! plicate other triggers
.Create store! proce! res an! in*o e the- in a trigger, if thePL/SQL co!e is *ery lengthy'
. he e+cessi*e se of triggers can res lt in co-ple+inter!epen!encies, 1hich -ay (e !iffic lt to -aintain in largeapplications'
0uide ines for 'esigning #riggers
Data(ase rigger "+a-ple
8/12/2019 Ppt Db25 Oracle 03
74/98
>Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
App ication
+1S"!# +1#% "
8/12/2019 Ppt Db25 Oracle 03
75/98
>5Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
triggering state-ent contains. rigger ti-ing
4 or ta(le $" O#", "#
4 or *ie1 I&S " D O
. riggering e*ent I&S"# , PD ", or D"L" "
. a(le na-e On ta(le, *ie1
. rigger type #o1 or state-ent
.@ "& cla se #estricting con!ition
. rigger (o!y PL/SQL (loc
Creating '
8/12/2019 Ppt Db25 Oracle 03
76/98
>
8/12/2019 Ppt Db25 Oracle 03
77/98
>>Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
DFL rigger Co ponents
riggering ser e*ent @hich DFL state-ent ca sesthe trigger to e+ec teA Ko can se any of thefollo1ing. I&S"#. PD ". D"L" "
DFL rigger Co-ponents
8/12/2019 Ppt Db25 Oracle 03
78/98
>?Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
DFL rigger Co ponents
rigger type Sho l! the trigger (o!y e+ec te for eachro1 the state-ent affects or only onceA. State-ent he trigger (o!y e+ec tes once for the triggering
e*ent' his is the !efa lt' state-ent trigger fires once, e*enif no ro1s are affecte! at all'
. #o1 he trigger (o!y e+ec tes once for each ro1 affecte! (ythe triggering e*ent' ro1 trigger is not e+ec te! if thetriggering e*ent affects no ro1s'
DFL rigger Co-ponents
8/12/2019 Ppt Db25 Oracle 03
79/98
>BCopyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
DFL rigger Co ponents
rigger (o!y @hat action sho l! the trigger perfor-Ahe trigger (o!y is a PL/SQL (loc or a call to aproce! re'
iring SeH ence
8/12/2019 Ppt Db25 Oracle 03
80/98
?0Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
g
#riggering action
B":%!"statement trigger
B":%!" row trigger A:#"! row trigger
A:#"! statement trigger
'
8/12/2019 Ppt Db25 Oracle 03
81/98
?Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
(P'A#" emp S"# sa ary sa ary 3 2 4G 65"!" dep=id D -
g
(se the fo owing firing seFuence for a trigger on a tab e, whenmany rows are manipu atedH
B":%!" statement trigger B":%!" statement trigger
B":%!" row trigger B":%!" row trigger A:#"! row trigger A:#"! row trigger
B":%!" row trigger B":%!" row trigger A:#"! row trigger A:#"! row trigger
A:#"! statement trigger A:#"! statement trigger
S f C i
8/12/2019 Ppt Db25 Oracle 03
82/98
?2Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" $%! !"PLAC"& #!+00"! trigger_name timing
event1 %! event2 %!
%1 tablenameTrigger body
1oteH #rigger names must be uniFue with respect to othertriggers in the same schema
Synta.H
Synta. for Creating'
8/12/2019 Ppt Db25 Oracle 03
83/98
?3Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" %! !"PLAC" #!+00"! trig2 B":%!" +1S"!# %1 emp B"0+1 +: )#%=C5A!)S S'A#",J' J* +1 )JSA#J,JS(1J** %!
)#%=C5A!)S S'A#",J554 J*1%# B"#6""1 J EJ A1' J2>J* #5"1 !A+S"=APPL+CA#+%1="!!%! )M4 222,J ou are nota owed to operate beyond officehours J*- "1' +:-"1'-
/
".amp eH
Creating '
8/12/2019 Ppt Db25 Oracle 03
84/98
?Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" $%! !"PLAC"& #!+00"! trigger_name timing
event1 %! event2 %! %1 tablename
$!":"!"1C+10 %L' AS old | 1"6 AS ne &:%! "AC5 !%6 65"1 ) condition *trigger_body
Synta.H
Creating a '
8/12/2019 Ppt Db25 Oracle 03
85/98
?5Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" %! !"PLAC" #!+00"! prevent=sa B":%!" +1S"!# %! (P'A#" %: sa ary %1 emp :%! "AC5 !%6 B"0+1 +: 1%# )H1"6 Nob=id +1 )I"'(CA#%!J, I
8/12/2019 Ppt Db25 Oracle 03
86/98
?
8/12/2019 Ppt Db25 Oracle 03
87/98
?>Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
+1S"!# +1#% emp)emp=id, name, Nob=id, sa ary, *
;AL("S )EEE, J#empJ, IC%1S(L#A1#J, 4 , *-
(P'A#" emp
S"# sa ary 4 , name JSukumarJ 65"!" emp=id EEE-
S"L"C# user=name, :!%< audit=emp=tab
(sing %L and 1 6 Qua ifiersH".amp e (sing Audit="mp=#ab e
!estricting a !ow #rigger
8/12/2019 Ppt Db25 Oracle 03
88/98
??Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" %! !"PLAC" #!+00"! get=commission B":%!" +1S"!# %! (P'A#" %: sa ary %1 emp :%! "AC5 !%6 65"1 )1"6 Nob=id I"'(CA#%!J*B"0+1 +: +1S"!#+10 #5"1 H1"6 commission H - "LS+: H%L' commission +S 1(LL #5"1 H1"6 commission H - "LS"
H1"6 commission H H%L' commission 24- "1' +:-"1'-
/
!estricting a !ow #rigger
I&S " D O riggers
8/12/2019 Ppt Db25 Oracle 03
89/98
?BCopyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
App ication
+1S"!# +1#% my=view -
< =;+"6
+1S#"A' %: #rigger
+1S"!#
#ABL"2
(P'A#"#ABL"4
Creating an +1S#"A' %: #rigger
8/12/2019 Ppt Db25 Oracle 03
90/98
B0Copyright 2005, Infosysechnologies Lt! "#/CO#P/C#S/D$25/003%ersion &o' 2'0
C!"A#" $%! !"PLAC"& #!+00"! trigger_name +1S#"A' %: event1 %! event2 %! %1 vie _name
$!":"!"1C+10 %L' AS old | 1"6 AS ne &$:%! "AC5 !%6&trigger_body
Synta.H
Creating an 1S# A %: #rigger
Creating an I&S " D O rigger
8/12/2019 Ppt Db25 Oracle 03
91/98
BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
g gg
+1S"!# +1#% emp=view)emp=id, *;AL("S)222,I!A
8/12/2019 Ppt Db25 Oracle 03
92/98
B2Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
#riggers
'efined with C!"A#"#!+00"!
'ata dictionary containssource code in(S"!=#!+00"!S
+mp icit y invoked
C%
8/12/2019 Ppt Db25 Oracle 03
93/98
B3Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
'!%P #!+00"! triggername -
'!%P #!+00"! trig2-
!% ! ! y .
o re-o*e a trigger fro- the !ata(ase, se the DROP
TRIGGER synta+
".amp eH
A triggers on a tab e are dropped when the
tab e is dropped
$enefits of Data(ase riggers
8/12/2019 Ppt Db25 Oracle 03
94/98
BCopyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
. I-pro*e! !ata sec rity 4 Pro*i!e enhance! an! co-ple+ sec rity chec s 4 Pro*i!e enhance! an! co-ple+ a !iting
. I-pro*e! !ata integrity 4 "nforce !yna-ic !ata integrity constraints
4 "nforce co-ple+ referential integrity constraints 4 "ns re that relate! operations are perfor-e! together
i-plicitly
%ie1ing rigger Infor-ation
8/12/2019 Ppt Db25 Oracle 03
95/98
B5Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
Ko can *ie1 the follo1ing trigger infor-ation. S"#EO$M"C S !ata !ictionary *ie1 o()ect infor-ation. S"#E #I88"#S !ata !ictionary *ie1 the te+t of the
trigger . S"#E"##O#S !ata !ictionary *ie1 PL/SQL synta+ errors
6co-pilation errors7 of the trigger
Listing the Code of #riggers
8/12/2019 Ppt Db25 Oracle 03
96/98
B
8/12/2019 Ppt Db25 Oracle 03
97/98
B>Copyright 2005, Infosysechnologies Lt!
"#/CO#P/C#S/D$25/003%ersion &o' 2'0
@e ha*e e+plaine! the follo1ing PL/SQL s (progra-s nits
. Proce! res
. nctions
. Pac ages
. Data(ase riggers
8/12/2019 Ppt Db25 Oracle 03
98/98
Thank You!