Beruflich Dokumente
Kultur Dokumente
Oracle Day 3
O()ecti*es
o e+plain PL/SQL s,(progra-s ,nits . . . . Proce!,res /,nctions Pac0ages Data(ase riggers
O*er*ie1 of S,(progra-s
2 s,(progra-3 . Is a na-e! PL/SQL (loc0 that can accept para-eters an! (e in*o0e! fro- a calling en*iron-ent . Is of t1o types3 4 2 proce!,re that perfor-s an action 4 2 f,nction that co-p,tes a *al,e . Is (ase! on stan!ar! PL/SQL (loc0 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
Subprogram Body
PL/SQL S,(progra-s
===== === === === ===== === === === +++ +++ +++ +++ +++ ===== === === +++ === ===== === === === ===== === === === ===== === === +++ === +++ +++ ===== === === +++ === +++ +++ ===== === === === ===== ===+++ === +++=== +++ ===== ===+++ === +++=== +++ +++ +++ +++ +++ +++ +++ Subprogram A, which contains the repeated code
===== === === === ===== === === === A === === ===== === ===== === === === ===== === === === A === === ===== === ===== === === === A === === ===== ===
<
$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
>
@hat Is a Proce!,reA
. . 2 proce!,re is a type of s,(progra- that perfor-s an action' 2 proce!,re can (e store! in the !ata(ase, as a sche-a o()ect, for repeate! e+ec,tion'
. .
#he !"PLAC" option indicates that if the procedure e.ists, it wi be dropped and rep aced with the new version created by the statement/ PL/SQL b ock starts with either B"0+1 or the dec aration of oca variab es and ends with either "1' or "1' procedurename.
De*eloping Proce!,res
"ditor Code to create procedure SQL3P us 4 Load and e.ecute file.sql 2 filename.sql
CC
C2
:orma parameter acts as (ninitia i9ed a constant variab e Actua parameter can be a itera , e.pression, constant, or initia i9ed variab e Can be assigned a defau t va ue <ust be a variab e
C3
C!"A#" %! !"PLAC" P!%C"'(!" inc=sa ary )p=id +1 emp /emp=id@#AP"* +S B"0+1 (P'A#" emp S"# sa ary B sa ary 3 2/2C 65"!" emp=id B p=id"1' inc=sa ary/
C5
OD Para-eters3 "+a-ple
QUERY_EMP procedure
p_id
C5
OD Para-eters3 "+a-ple
C!"A#" %! !"PLAC" P!%C"'(!" Fuery2 )p=id +1 emp /emp=id@#AP", p=name %(# emp /name@#AP", p=sa ary %(# emp /sa ary@#AP", p=comm %(# emp /commission@#AP"* +S B"0+1 S"L"C# name, sa ary, commission +1#% p=name, p=sa ary, p=comm :!%< emp 65"!" emp=id B p=id"1' Fuery2/
C<
%ie1ing OD Para-eters
. .
Load and run the emp=Fuery/sF script fi e to create the Q("!A2 procedure/ 'ec are host variab es, e.ecute the Q("!A2 procedure, and print the va ue of the g oba 0=1A<" variab e/
C>
C?
CB
C!"A#" %! !"PLAC" P!%C"'(!" add=department )p=name +1 dep/dep=name@#AP" '":A(L# IabcJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 2ECC* +S B"0+1 +1S"!# +1#% dep)dep=id, dep=name, oc=id* ;AL("S )seF2/1"8#;AL, p=name, p= oc*"1' add=department/
20
2C
22
23
C!"A#" %! !"PLAC" P!%C"'(!" emp=process +S C(!S%! c2 +S S"L"C# emp=id :!%< emp B"0+1 :%! i +1 c2 L%%P inc=sa ary)emp=rec/emp oyee=id*"1' L%%PC%<<+#"1' emp=process/
25
#e-o*ing Proce!,res
Drop a proce!,re store! in the !ata(ase'
25
Store! /,nctions
. . .
2 f,nction is a na-e! PL/SQL (loc0 that ret,rns a *al,e' 2 f,nction can (e store! in the !ata(ase as a sche-a o()ect for repeate! e+ec,tion' 2 f,nction is calle! as part of an e+pression'
2<
C!"A#" $%! !"PLAC"& :(1C#+%1 functionname )parameter1 $mode& datatype1, parameter2 $mode& datatype2, . . .* !"#(!1 datatype +S,AS PL/SQL B ock-
2>
Creating a /,nction
"ditor Code to create function iSQL3P us 4 Load and e.ecute filename.sql 2
:unction created 7
2? "#/CO#P/C#S/D$25/003 %ersion &o' 2'0
"+ec,ting /,nctions
. . . In*o0e 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'
2B
30
3C
32
33
35
35
!emoving :unctions
Drop a store! f,nction'
Synta.H
DROP FUNCTION FUNCTION ffunction_name DROP !
dropping a function and recreating it/ Privi eges granted on the function remain the same when this synta. is used/
3<
Procedure
:unction
Ca ing environment
Ca ing environment
+1 parameter
3>
3?
3B
S"L"C# obNect=name, obNect=type :!%< user=obNects 65"!" obNect=type in )JP!%C"'(!"J,J:(1C#+%1J* %!'"! BA obNect=name-
50
Co umn 'escription 1ame of the obNect #ype of obNect, for e.amp e, P!%C"'(!", :(1C#+%1, PACQA0", PACQA0" B%'A Line number of the source code #e.t of the source code ine
L+1" #"8#
5C
52
53
he DBMS_OUTPUT pac0age3
4 2cc,-,lates infor-ation into a (,ffer 4 2llo1s retrie*al of the infor-ation fro- the (,ffer
. .
2,tono-o,s proce!,re calls 6for e+a-ple, 1riting the o,tp,t to a log ta(le7 Soft1are that ,ses DBMS_DEBUG
4 4 Proce!,re $,il!er hir!=party !e(,gging soft1are
55
O*er*ie1 of Pac0ages
Pac0ages3 . 8ro,p logically relate! PL/SQL types, ite-s, an! s,(progra-s . Consist of t1o parts3 4 Specification 4 $o!y . Cannot (e in*o0e!, para-eteriJe!, or neste! . 2llo1 the Oracle ser*er to rea! -,ltiple o()ects into -e-ory at once
55
Co-ponents of a Pac0age
Pub ic variab e Procedure A dec aration Pub ic procedure
Package specification
Private variab e Package body Procedure B definition Procedure A definition Private procedure
5<
De*eloping a Pac0age
Sa*ing the te+t of the CREATE PACKAGE state-ent in t1o !ifferent SQL files facilitates later -o!ifications to the pac0age' 2 pac0age specification can e+ist 1itho,t a pac0age (o!y, (,t a pac0age (o!y cannot e+ist 1itho,t a pac0age specification'
5>
C!"A#" $%! !"PLAC"& PACQA0" package_name +S,AS public type and item declarations subprogram specifications "1' package_name; . . . he #"PL2C" option !rops an! recreates the pac0age specification' %aria(les !eclare! in the pac0age specification are initialiJe! to &DLL (y !efa,lt' 2ll the constr,cts !eclare! in a pac0age specification are *isi(le to ,sers 1ho are grante! pri*ileges on the pac0age'
5?
COMM_P"C#"$E package
$_COM Package specification
5B
C!"A#" %! !"PLAC" PACQA0" com=pack +S g=com 1(<B"! HB C/2GP!%C"'(!" reset=com )p=com +1 1(<B"!*"1' com=pack/
. .
0=C%< is a g oba variab e and is initia i9ed to C/2G !"S"#=C%< is a pub ic procedure that is imp emented in the package body/
50
. . .
#he !"PLAC" option drops and recreates the package body/ +dentifiers defined on y in the package body are private constructs/ #hese are not visib e outside the package body/ A private constructs must be dec ared before they are used in the pub ic constructs/
5C
Package specification
2 4
Package body
52
C!"A#" %! !"PLAC" PACQA0" B%'A com=pack +S :(1C#+%1 va idate=comm )p=com +1 1(<B"!* !"#(!1 B%%L"A1 +S v=ma.=com 1(<B"!B"0+1 S"L"C# <A8)commission* +1#% v=ma.=com :!%< emp +: p=com K v=ma.=com #5"1 !"#(!1):ALS"*"LS" !"#(!1)#!("*"1' +:"1' va idate=com///
53
P!%C"'(!" reset=com )p=com +1 1(<B"!* +S B"0+1 +: va idate=com)p=com* #5"1 g=comHBp=com- MMreset g oba variab e "LS" !A+S"=APPL+CA#+%1="!!%!)M4C444,ICommission +nva idJ*"1' +:"1' reset=com"1' com=pack/
55
#e-o*ing Pac0ages
#o remove the package specification and the body, use the fo owing synta.H '!%P PACQA0" package_name;
#o remove the package body, use the fo owing synta.H '!%P PACQA0" B%'A package_name;
5<
. .
5>
2!*antages of Pac0ages
. . . Fo!,larity3 "ncaps,late relate! constr,cts' "asier application !esign3 Co!e an! co-pile specification an! (o!y separately' Ii!ing infor-ation3 4 Only the !eclarations in the pac0age specification are *isi(le an! accessi(le to applications' 4 Pri*ate constr,cts in the pac0age (o!y are hi!!en an! inaccessi(le' 4 2ll co!ing is hi!!en in the pac0age (o!y'
5?
2!*antages of Pac0ages
. . 2!!e! f,nctionality3 Persistency of *aria(les an! c,rsors $etter perfor-ance3 4 he entire pac0age is loa!e! into -e-ory 1hen the pac0age 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!ing3 F,ltiple s,(progra-s of the sa-e na-e
5B
O*erloa!ing
. . . "na(les yo, to ,se the sa-e na-e for !ifferent s,(progra-s insi!e a PL/SQL (loc0, a s,(progra-, or a pac0age #eH,ires the for-al para-eters of the s,(progra-s to !iffer in n,-(er, or!er, or !ata type fa-ily "na(les yo, to (,il! -ore fle+i(ility (eca,se a ,ser or application is not restricte! (y the specific !ata type or n,-(er of for-al para-eters &ote3 Only local or pac0age! s,(progra-s can (e o*erloa!e!' Ko, cannot o*erloa! stan!=alone s,(progra-s'
<0
C!"A#" %! !"PLAC" PACQA0" over=package +S P!%C"'(!" add=department )p=deptno +1 dep/dep=id@#AP", p=name +1 dep/dep=name@#AP" '":A(L# IABCJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 222*P!%C"'(!" add=department )p=name +1 dep/dep=name@#AP" '":A(L# IABCJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 222*"1' over=package/
<C
Ko, -,st !eclare i!entifiers (efore referencing the-' C!"A#" %! !"PLAC" PACQA0" B%'A forward=package +S P!%C"'(!" bonus)/ / /* +S B"0+1 ca c=rating)/ / /*MMi ega reference "1'P!%C"'(!" rating)/ / /* +S B"0+1 /// "1'"1' forward=package/
Copyright 2005, Infosys echnologies Lt! <2 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0
&ote3 Calls to s,(progra-s that (rea0 the a(o*e restrictions are not allo1e!'
<5
<5
<<
<>
"+ec,tion /lo1
SQL state-ents go thro,gh *ario,s stages3 . Parse . $in! . "+ec,te . /etch &ote3 So-e stages -ay (e s0ippe!'
<?
<B
he D$FSEDDL Pac0age3 . Pro*i!es access to so-e SQL DDL state-ents fro- store! proce!,res . Incl,!es so-e proce!,res3 4 2L "#ECOFPIL" 6o()ectEtype, o1ner, o()ectEna-e7 4 2&2LKL"EO$M"C 6o()ectEtype, o1ner, na-e, -etho!7 'B<S=''L/AL#"!=C%<P+L")JP!%C"'(!"J,JA=(S"!J,JQ("!A2J* &ote3 his pac0age r,ns 1ith the pri*ileges of calling ,ser, rather than the pac0age o1ner SKS' 'B<S=''L/A1ALAR"=%BS"C#)J#ABL"J,JA=(S"!J,JS%BSJ,JC%<P(#"J*
>0
>C
'atabase #riggers
2 trigger3 .Is a PL/SQL (loc0 or a PL/SQL proce!,re associate! 1ith a ta(le, *ie1, sche-a, or the !ata(ase ."+ec,tes i-plicitly 1hene*er a partic,lar e*ent ta0es place .Can (e either3
42pplication trigger3 /ires 1hene*er an e*ent occ,rs 1ith a partic,lar application 4Data(ase trigger3 /ires 1hene*er a !ata e*ent 6s,ch as DFL7 or syste- e*ent 6s,ch as logon or sh,t!o1n7 occ,rs on a sche-a or !ata(ase
>2
>3
%"'_C.EC# trigger
"<PL tab e
>5
>5
rigger ti-ing3 @hen sho,l! the trigger fireA .$"/O#"3 "+ec,te the trigger (o!y (efore the triggering DFL e*ent on a ta(le' .2/ "#3 "+ec,te the trigger (o!y after the triggering DFL e*ent on a ta(le' .I&S "2D O/3 "+ec,te the trigger (o!y instea! of the triggering state-ent' his is ,se! for *ie1s that are not other1ise -o!ifia(le'
><
>>
>?
>B
/iring SeH,ence
(se the fo owing firing seFuence for a trigger on a tab e, when a sing e row is manipu atedH '<L statement +1S"!# +1#% dep )dep=id, dep=name, oc=id* ;AL("S )ECC, I!"S"A!C5J, GLCC*-
B":%!" statement trigger #riggering action B":%!" row trigger A:#"! row trigger A:#"! statement trigger
Copyright 2005, Infosys echnologies Lt! ?0 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0
/iring SeH,ence
(se the fo owing firing seFuence for a trigger on a tab e, when many rows are manipu atedH (P'A#" emp S"# sa ary B sa ary 3 2/4G 65"!" dep=id B DC-
B":%!" statement trigger B":%!" row trigger A:#"! row trigger /// B":%!" row trigger A:#"! row trigger /// A:#"! statement trigger
Copyright 2005, Infosys echnologies Lt! ?C "#/CO#P/C#S/D$25/003 %ersion &o' 2'0
?2
?3
?5
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<A1A0"!J** A1' H1"6/sa ary K GGCCC #5"1 !A+S"=APPL+CA#+%1="!!%! )M4C444,I Cannot earn this amountJ*"1' +:"1'/
?5
?<
?>
??
<A=;+"6
(P'A#" #ABL"4
?B
B0
BC
B2
".amp eH '!%P #!+00"! trig2A triggers on a tab e are dropped when the tab e is dropped/
B3
I-pro*e! !ata sec,rity3 4 Pro*i!e enhance! an! co-ple+ sec,rity chec0s 4 Pro*i!e enhance! an! co-ple+ a,!iting I-pro*e! !ata integrity3 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
B5
Ko, can *ie1 the follo1ing trigger infor-ation3 . DS"#EO$M"C S !ata !ictionary *ie13 o()ect infor-ation . DS"#E #I88"#S !ata !ictionary *ie13 the te+t of the trigger . DS"#E"##O#S !ata !ictionary *ie13 PL/SQL synta+ errors 6co-pilation errors7 of the trigger
B5
B<
S,--ary
@e ha*e e+plaine! the follo1ing PL/SQL s,(progra-s ,nits . . . . Proce!,res /,nctions Pac0ages Data(ase riggers
B>
Thank You!
Copyright 2005, Infosys echnologies Lt! B? "#/CO#P/C#S/D$25/003 %ersion &o' 2'0