0% found this document useful (0 votes)
12 views10 pages

Adobe Scan 05-Mar-2024

This document discusses Oracle packages. It explains that a package provides an extra layer of abstraction to a module. A package has a specification defining its public objects and a body containing the actual code. Packages allow restricting access, following object-oriented principles, and improve performance through one-time loading into memory.

Uploaded by

ajay2karthick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Adobe Scan 05-Mar-2024

This document discusses Oracle packages. It explains that a package provides an extra layer of abstraction to a module. A package has a specification defining its public objects and a body containing the actual code. Packages allow restricting access, following object-oriented principles, and improve performance through one-time loading into memory.

Uploaded by

ajay2karthick
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

jiP

n, '1..J'SQ l Named Block~ f>TO(edUnt, fun


Ch.ap. 14 ctio n, Packase, and lrlgg~;

Pa ck u~ ii.Pe modular in na ture, and Ora cle


has many built-in packaget. (f "
~memucr.
L... ~
D BM S OUTP UT is I bui lt-i n package. You also know that a packag 10
-
CJllicd STA~'114.RD cont.aim definit ,.,
ions of many operators use d tn · 0 ~
racl e. Th er t
u-c m.a.nv ben~6ts to using a package.
~ \)bjects m a pa ct~ :an be dec
lared as public objects, which can be
eoo :d from ou ~o r as p,; ..•atcobj ref er-
ects. which are known only to the pac
restrict ac;cc$S to a pa ct: ~ to it5 speci.6 kag e. Yo u can
cati.on only and hide the act ual progra
aspect. A package ~ " S some
rules oi object-oriented programm ing mming
gnunmcrs some ot;c.x:t-oriented capabil , and it giv es pro~
ities. A pack.age compiles suc cessfu
v."it.bout a ~ - if the specification lly even
compiles. When an object in the packag
enc ed far ~ finl time. the en tire e is refer-
package is loaded into memory. All
axnts arc &\'aw.bk from that ~in t on. package ele-
because the entire package stays in me
This one-time loadiag improves perform mo rv.
ance and is very useful when the functio
proc:cdures i.n it an, ~ freq ns and
uently. The package also follows top-do
wn design .
Str um ... of a hc ka ge
t
A pad age pro"ides
:!:~ an extra layer to a module. A module
~:ck.age has a ~6 ca tio n and a
body. A modul
has a hea der and a bod v
e 's hea der specifies th-~
age specific a u : :~ ~ t~U d~
how to call that ~°? ule . Similarly, the
c, ca pac k-
erent modules within a package.
l ,.Kk119e Sp ect tia tio n

A pa d
age spe ci,'filC-dtJoo
.
does not contain any cod b .

public:~~:
about the dcm enu of the pac . .
du.r es, ded aratioM of global or
kage It . e, ~! It doe s con tain inf orm atio n
defirut1ons ~f fun ctio ns and pro
clared rn a PUSQL block 's dcclara ce-
tjo .es, aThnd anything else that can be de-
~c t
ion° f 1r
a pac&.a~ are called pobli b, a section . e obJ'e t • h
Th c s m t e spe .
co ~ects. e genera l syntax is. cification
0£AT'E {Oil M/tt..ACEJ PACKA GE
15
~u~1...
1g- ,,a m.
I ~ v1rt.i,1• •nd ty/M
I ttxc..ptJo,, ~t io ns 1 (U<J•r• ti.ons
I f-
f '-cul'J()f' l'Pedf#
·-~,
I IW'I
utlo ns J
V
~ ,
,...,,.
IQI° " ,p.d flut Jon
1I 'j ~., ~
<...,
/
For example,
ENO I
I ~ •P«fflat1on1 I
J)O(t•- ); ,
t.,
, }-i
~ 2:l
P4.CXAGEbb tMf fl ,
IS to ~I ~~ CONSTAt:" fHTEGU
:• 12;
l2S

p!ayer_on _dl Eief PTION;


fiJNC(ION team_av,u a9e{poh,u IN NUMOER, play1n IMNUMB~ )
RETURN NUMBER;
mo bb_team;
The package specification fo r the coursejnfo pack.age in Figure 14-lO contain~
the specification of a procedure called FIND_TITLE and functions HAS..PRE REO
and FIND_PR EREQ. The COURSE JNFO package contairus three modules in all.
i

SQL> CREATE OR REPLACE PACKAGE COURSE_IHfO


2 AS
3 PROCEDURE FIND_TlTlE
4 (1_10 IN COURSE.COURSEID% TYPf,
5 O_TITLE OUT COURSE.TTTtE%TYPE);
6 FUNCTION HAS_PREREQ
7 (1_10 IN COURSE.COURS£10%TYPE)
8 RETURN BOOLEAN;
9 FUNCTION FIND_PREREQ
10 (1_10 IN COURSE.COURSE1D% TYPE)
11 RETURN VARCHARl;
12 END COURSE_IN FO;
13 /

Package created.

SQL>

Figure 14-10 Pad.ag~ spccifi~lion.

Package Body

A package body contains actual programming code for the modules described in
the specificatio n section. It also contains code: for the modules not described in the
specificatio n section.The module code in the body \\ithout a description in the spec-
ification is called a printe module. or a hidden module. and it is not visible outside
the body of the package.
The general syntax of a pu~kngt! hody is
PACKA GE BODY ,,.dcagen.am.
IS
( v•rl•ble .and typt ct.<'4r1tio1u J
{ ru/'lor. sptdfic•tlom •nd SEL.EC'T r
I hHd•r ,11d body ol functions J
J

l h...,., and body of proc-.clures J

\
{ BEGIN
I

,,
--+-ae

.
ion, Package, and lir19%
LJSQL Named Blocks: Pmctdure, Funct ,
P
O,IP· 1•
[ EXCEP110N
.xmptlon handltrJ J SQL- r

END { peck,g•n•me J; '


3
f,

nu
field is to a record, so an ob ject is to a package. Wh enkyou rcf· ore . 1 '·
As. a package you must qualify Jt . wit. h thc name of that pac age usm g d() t ri
8 b'Ject, the compilativn ',
·
tiJect lI1f you do not use dot notation to reference an d
' o f
111 7
l h .
do not nve to use ot notation or tha t pc1c r
1ttOO, a pac kag e, you a
.1w·t1 .hin the bo dy of ·
u . def inite1y have to use dot notati.on to ref ere nee an obJect frc1 m
ge's objects, but you
notbcr package. For example, .'
IF bb_team.total_players < 10 THEN
·2

·or exa mple, .. • .1

EXCEPTION
N
WHEN bb_te1m.pl1y1 r_on_dl THE
kage.
yer s an d pla yer _on _dl are mo dules/objects in the bb_team pac
where total_pla y:
ere is a set of rul es tha t you mu st follow in writing a pac kag e's bod
Th
ti on
iab les , con sta nts , exc eption s, and so on declared in the spe cifica
• Th e var
must no t be dec lar ed aga in in the
package body.
er of cur so r and mo dul e definitions in the specification mu st
• The nu mb
module headers in the body.
ma tch the nu mb er of cur sor and bo dy.
y ele me nt dec lar ed in the spe cification can be refere nce d in the
• An
ely, are
ure s 14- 10 and 14- 11, pac kag e specification and body, res pec tiv
In Fig cti on in the
for the cou rse _in fo pac kag e. Toe calls to a procedure and a fun
shown ly.
cou ru_ inf o pac kag e arc shown
in Figures 14-12 and 14-13, respective
LE of the
Fig ure 14- 12, a cal l is ma de to the procedure FlN D_TIT
In V_COU RSEID
ure 14-11. The procedure is passed
CO UR SE_IN FO package in fig , the pro ced ure throws an except
ion and
am ete r. If Co urs eld is inv alid
as an IN par urs eld is valid, the
n han dle s tha t exr ept ion wit h an app rop ria te message. If Co
the titl e.
ou r par am ete r V_T ITL E is assigntoedthecoufunrsection HA S_ PR ER EQ of the COURSE_
In Fig ure 14-13, a call is ma de
h om : par am ete r, V _C OU RS EID . If course does no t have a prereq-
IN FO package wit st, an ~xcep-
fun ctio n dis pla ys the app rop riate message. If course does not exi
uisite, the n ret urr..,;
thr ow n in the fun ctio n bod y. and the message is displayed .1be fun ctio
tion is
ctio n ret um sT RU E if the prerequisite exists. IfTRUF -.
FALSE in both cases. Th e fun
Q, is called with the V_C OU R~ di0
r
~ fun ctio n, FlN DY RE RE
ret urn ed, ano the and title-. n~ r" 14
enated prerequisite ID
ramete r. The function returns concat
atio ns mentioned abovt.
shows ou tput fro ni all thr ee situ rr •. -. l"•
mand to run a PEl'-h. 1ge \
Yo u cao ah a use the EXECUTE com
EXECUTE pacbgename.procedurtn•
~
32 7
Pa cka ges

INF O AS
I SQ L> CRE ATE OR RF.PLAC
2
E PAC KA GE BO DY COU RSE
PRO CED UR E FIN D TITL E
- ,
IS I
T CO UR SE. TIT LE% TYP E)
3 (/_I D IN CO URS E.C OU
RSE ID% TYP E. 0 - TIT LE OU ,
4 BEG I N
ERE cou Rs e,o -= u o ;
I
s e INT O o_ TIT LE FRO M cou Rs E WH
SEL ECT nn
6 EXC EPT ION
7 WH EN CIT HER S THE N /
E(I_ ID II• not fou nd .');
8 DB MS,_ OU TPU T.P UT_ LIN
9 EN O FIN D_ TIT LE;
--- --- --- ---
--- --- --- --- --- --- ---
10 --- --- ---- --· ---- ---- ---- ----
; 1 FUN CTI ON HA S_P RER EQ OL EA N IS
(/_I D IN CO URS E.C OU
RS EID % TYP E) RET UR N BO
12
V _PR El=t EQ VA RCH AR 2(6
);
13
14 BEG IN
NE ') INT O V _ PRE REQ
15 SEL ECT NV L(PRER EQ, 'NO
FR OM CO UR SE WH ERE
CO UR SEI D:: I_ ID;
16
IF V _ PFt ERE Q = ' NO NE '
THE N
17 lte 1;
NE ('No pre req uis
18 DB MS _ OU TPU T. PUT_LI
19 RE TUR N FAL SE;
20 ELS E RE TUR N TRU E;
21 EN D IF;
22 EX CE PTI ON
D THE N
23 WH Efll NO _ DA TA_ FO UN t exi st') ;
E('C our se ; 'II 1_ 1D II' doe s no
24 DB MS _O U1' PU T.P UT_ UN
25 RE TU RN FAL SE;
;
26 EN D HA S__PR ER EQ
--- - - - - - - --- --- -- ·
- - - - - - - - - - - - - -- --- ------ ---- ---- ---- --
27
REQ
28 FU NC TIO N FIN D_ PRE HA R2 IS
IO¾ TYP E) RET UR N VA RC
29 (I_I D IN CO UR SE.CO UR SE
30 V_ ,C, VA RC HA R2 (6);
31 V_ TITLE VA RC HA R2 (25 );
32 V_PRE VA RC HA R2 (30 );
33 BE GIN ' l'JE ')
'NO NE '), NVL(P.T/TLE. ' NO
34 SE LEX T NV L(P .CO UR SE ID,
CO UR SE C. CO UR SE P
35 . IN1'O V_ ID, V _ TIT LE FR OM • 1_1 0;
RS EID AN D C.C OU RS EID
36 WH ER E C. PR ER EQ rr: P.C OU
V __PRE := V _ ID II '·- ' II V_ ITT
LE:
37
38 RE TU RN V _PR f;
39 EX CE PT ION
RN 'NO NE ';
I
40 WH EN OT HE RS TH EN RETU
I 4 1 EN O;
I
1 -42 EN D CO UR 5E_ iNF O;
I
/.'3 I
. Pl'O(tdurt, functJon, Package, and
32 1 Chip. 14 PlJSQL Named Blocks, Trigger

SQl> 1• MQnymous block c.,lls


OOC> proctdurt flND _m tf In pac • COURSE_INFO •t
k~
SQL> D!Cl.ARE
2 URSEIO% rv, e :,■ ,,,•.coURSEID';
V COURSEIO COURSE.CO
\
3 v:mu couRsE.TtTU%TVf'f;
4 BEGICN'""11RSE INfO,FIND_T
tTU(V_COURSEIO, V_TtTLE):
S ""' - EN
6 IF \I- TtTtE IS NOT NPVTULL THLINE(V
7 COURSEID It ': , II V- TITLE);
OBMS_OUTPUT. - -
I ENO IF;
9 ENO ;
10 I
Ent er val ue for p_courseld: 052
65
0 S26 5: Syr tem s Analysis
PL/SQL pro ced unt succass
fully completed.
SQL> I
Enter val ue for p_courseld: 051
00
as 100 not fou nd.
Pl/SQL pro ced ure successfully
complet ed.

f\p re 14- U CaU to pro ced ure


in the package of Figure 14-11.
TRI GGERS

A da tab ue trigger, known simply


as a trigger1 ~s ~ SQL block. It
database and is called automatically is sto r~d jn_the
when a triggenng event occurs. A
call a trigger ~ lici.!!l'.:,The
triggering event is based on a Da user can no t
-guage (DML) statement, such as ta Manipulation Lan·
INSERT, UPDATE, or DE LE TE
created to fire before or after the . A trigger can be
triggering event. For example, if you
ger to execute after you INSERT design a trig-
a new employee in the EMPLOY
trigger executes after the INSERT EE table, the
statement. The execution of a
kno wn as firing the trigger. 'The trigger is also
general syntax is
CREATE I OR REPUCE J TRIG GER trlg g.,- na~
BEFORE I An-ER I INSTEAD OF
trlg ger lng evM t ON t11blt11'.ti~w
/ FOR EACH RO W} wher
{ WH EN COM/hlon J you 1
DECJ.ARE

'
you r
fH<Jaratlon sta tem ent s funct
I
BEGIN ate a
Ex«utabl• statem.11 ts tab le.
EXCEPTION pl.a ce
Exc.ptlon•h1ndl/n9 st,t .~1 1t1
END,·
r Triggers

SQL> I* Anonymous block calls function HAS_PREREQ


DOC> and function FIND_PAEREQ In package COURSE INFO •
SQL> DECLARE - /
2 V_FLAG BOOLEAN;
3 V_COURSEID COURSE.COURSE10% TYPE:• '&P_COURSEIO'·
4 V_TITLE VARCHAR2(30); '
5 BEGIN
6 V_COURSEID := UPPER(V•.COURSEIO);
7 V_FLAG := COURSE_INFO.HAS_PREREQ(V_COURSEID);
8 IF V_FLAG = TRUE THEN
9 V_TITLE := COURSE_INFO.FIND_PREREQ(V_COURSflO);
10 OBMS_OUTPUT.PUT_LINE('Course: 'II V_COURSEIO);
11 DBMS _OUTPUT.PUT_LINE('Pre-Requlsfte ·'II V_COURSEID);
12 ENO IF;
13 ENO;
1, /
Ent.r value for p_courseld: CIS265
Course: CIS265
P~Requlslte · Cl5253
PL/SOL proced ure successfully completed.

SQL> /
Enter value for p_courseid: CIS253
No prerequisite
PL'SQL procedure successfully completed.

SOL> /
99
Enter value for p_courseid: 0S9
1 the Course: CIS999 does not exist ·
nnot
PUSQL procedure successfully completed,
Lan-
m be SQL>
trig- e of Figure 14-11 ,
:, the Flgure 14-13 Call to functions in the packag
also
LACE oiea.1'
. a new trigger and RE! tional, an•
are creaung d REPLACE 1sdopprv--- ...-dw <
where CREA TE means. you . • The key wor 8 ,1

g tngge_r. se REPL ACE art . If you cc~


you are replac ing an eX1stm
you should only use it to modify a tngger . If you ~he trigge r reph•C
~5 1 anothe \h
function, or packa ge exists with the same nam~:If it and associate
oc ri
11 '' \ n
4 0
to mo Y . t ·
i n one table, you
ate a trigge r for a table and then decide
. already eXlS s .
table, you will get an error. If a tngger
. i.c1e-pu
place it and associ ate it with anoth er table. ~or dert·ved c0Ju.t11flc~.h~.ks, 1
· va1ues , fonning v11li·d'tY1 "
A trigge r is very useful in gene.r.lltlng .
. . , 1·d entnes' per
t tack of table access. preven ung mva 1
n, Pac kag e, and li'9g
L Named Blocks: Procedure, Functio 1 ~
pL/SQ
Chap. 14
330
wever, involving creation ~f a
. There are some res tric tions, ho
maintaining secunty.
trigger: Contr ol Language. sta tem en t '
such <1~
. can no t use a na ns act ion
• A tngger
LL BA CK , or SA V~ PO IN T. ~ op eraflo_ns pe rfo rm ed by a
COMMIT, RO tngger operations get com mitted
om e par t of the tra nsa ctio n. Th e
. ger bec
tng .
or rol led back wit h the tra nsa ctw n.
by a trigger can not perform Tra
nsactior.
• A pro ced ure or fun cti on cal led
Contr ol Language statements.
in a trig ger can no t be dec lar ed wit h i,O NG or LO NG RAW da ta
• A variable
types.

BEFORE Tri gg en
of a DM L statement. The BE FO
RE
ger is fired bef ore exe cut ion
The BEFORE trig som e values in a new row, insert a cal-
ful wh en you wa nt to plu g into
trigger is use ha
col um n into a new row , or val ida te a value in the INSERT query wit
culated
lookup in ano ther table. tha t fire s before a new row is ins t!rt ed
an exa mp le o f a trig ger
Figure 14-14 is
new em plo yee is bei ng add ed to the EMPLOYEE table, you can use
into a table. If a E as the
ger to get the nex t em plo yee num ber from the sequence, use SY SDAT SE RT
a trig
Th e trigger in Figure 14-14 fires
before the IN
employee's hir e dat e, and so on . le nam e the trig-
ent . Th e nam ing con ven tio n use d in the exa mp le uses the tab ger use s
stat~m ert ," and the n the wo rd trigger. A trig
o we d by bi for "be for e ins
ger is for, folJ cur ren tly pro ces sed
udo rec ord cal led :NE W, which aUows you to access the type of
a pse
e of rec ord :NE W is tab len am e% TYPE. In thi s example ' the
row. Th e typ • • ord are reference d wit h
:NE W 1·s ~m pl oy ee oYo TY PE . Th e col um ns m th1s .NE W rec

yee ld) .
dot not ati on (e.g., :NE W. Em pJo
R
GGER EMPLO YEE_BI_TRIGGE
SQL> CREATE OR REPLACE TRI
EE
2 BEFORE INSERT ON EMPLOY
3 FOR EACH RO W
4 DEQ .AR E
YEEID% TYPE•
5 V_EMPID EMPLOYEE.EM PLO •
6 Um N
SEQ.NEXTVAl
7 SfUCT EMPLOYEE_EMPLOYEEIO_
8 INTO V_EMPfO FROM DUAL;
9 :HEW.EMPLOYEEIO ~ V_EMPIO;
10 :NEW.HIREDATE ;a: SYSOATE·
; '
11 ENO
!
12 I
I
I

Tl'lg ger create d.

SQL>
--- I
Figure 14-14 BEFO RE trig~t•~.
Triggers
331
Th . ,
e tnggcr emplort·«· b1 tr''8~<'r
rn .
prov ides vulues . of Employec ld and Hir •.
Date. so you need . . - - e
.
not rnclude thosi: va lu c.s INS· E f> 'r
many columns that co n h . . . .m yo~ r ' stateme nt. If you have
be shortene d considcrnh~ ~st'g;~d v~1l ucs via a tn gg_c r:y1>ur IN ~ERT stntc ment will
t3 ble ,vithout v I · ~ n •~urc l~ - l S. 11. row 15 lllSl.!rt ud m th e EMPLOY EE
iiven value '
- 1
•t/
:cs fur_Empl(,~·cd d anti Hire Du te co lumns. Tho~e columns arc
' ' ring ot the BEFO RE trigger o f F11,tur c 14 -14 .

SQL> INSERT mro EMPLOYEE


2 (LNAME. FNAME. POSITIONIO , SUPERVISOR, SALARY OEPTIO QUALID)
3 VALUES ' '
4 ('ZEE ', 'SONIA', 3, 543, 100000, 20, 2);
1 row c,...ted.

SQL> SET UNESJZE 200


SQL> SELECT • FROM EMPLOYEE WHERE LNAME ■ 'ZEE';

l EMPLOYEEIO LNAME FNAME POSITIONIO SUPERVISOR

5(6 ZEE SONIA 543

SQL>

t11urc 14-15 BEFORE trigg(r--row inserted .

AFTER Triggen

An AFTER trigger fires after a DML statement is executed. lt utilizes the built-in
Boolean functions INSERTING. UPDATING. and DELETING. If the triggering
event is one of the three DML statements, the: function related to the DML state-
ment ret urns TRUE and the other two return FALSE. For example. if the current
DML statement is INSERT, then INSERTIN G returns T RUE . but DELETl NG
and UPDATING return FALSE.
The example in Figure 14-16 uses an existing table named TRANSl··USTORY.
which keeps tr~ck of transactions perfom1ed on a table. It keeps track of updat es and
deletions,·tbe us.e r who performs them. and the dates on which they are performed .
The trigger is named employee_adu_trigger. where adt, stands for "after delete or up-
date •·Toe trigger u~es the transaction type based on the last OML statement. l.t also
plugs in the user name and today's date. TI1e information \S then inserted in th\:'
TR.ANSHISTORY table. Figure 14-17 shows rows inserted in the TRANSHtSTORY
table on use of DELETE and UPDATE statements by trigger
ror the ex.ample in Figure 14• 14. we used FOR EACH ROW. Such a mggcr i~
~ o ,, n 3S a row trigger. If it is based on INSERT. the trigter fires om:e for everv
newl~ mserted row. lf it is based on U PDAT E statemen t and the UPDATE anc(' \ '>
th-e ro,~ the trigger is ftred fi ve time~. once for each affeci ~d row In Figure 14-16.
we J1d not use a FOR EACH ROW. clause. Such a trigger is \. rt() W U as a sta tc me rn
mggu. \ stateme nt trigger 1~ fired only once {01 rhe :-.ta Lern.: t W!!:;pec uv~ of t!w
1

i.iiflb, of W'-\'~ affected b\ the DML s~at.:m1?11l.


J
PUSQ~ ttotntd e1ocl<S: pr
ocedur e, function, Package g g er
, and Tri

CIIEATT OR REPIJ.Cf
TRIGGER EMPLOYEE-"
~ITTR OELE'Tt Oft UP
DU_TIIIGGE R
OAT£ ON EMPLOYEE
3 OEC1.ARE
• \J _TAANSTYP£ vA
ftc.HAR1(6):
S BEGIN
6 If OEL£TING itiEH
7 v_TAA,NSTYP£ :• 10E
LE1't':
8 ELSIF UPOA'TmG TiiE
VJ"AANsTYPE :•N 'UPD
9 A'ft';
10 ENO I~
11 INSERT INTO TAAHSH
12 VALUES ('EMPLOY ISTORY
13 END: EE', V_'fAANSTYPE,
USER. SYSDA-)•
~ •• •
1• /
Trioger t,-ai.d.

SQL>

Fi p. rt 14-16 A
FTER trigger.

SQL> OB.ETE FR
OM EMPlOYEE
2 WHERE UP'ER(LNAM E) • 'VlQUEZ'·
1 row deleted
,
SQL> SELECT * ~O
M TRANSHISTOR"t.
TA
__SL
_ _ENAME
I EMPLOYEE
- --~-
TRANSTYPE I
USER_NAME
DELETE
I SQL> UPO
I
--- ~ _DAlt
05-0EC-03
2 ATE EMPLOYEE NSHAH
SET CO MISstON
3 WHERE MEMPLOYEE7oSALA
• 54RY
7· • 0.10
, rows upda
ted

SQL> SELECT • FR
OM TRANSHIST
I TABLENAME
- - - -~ - TRANSTYPE a_N
USER ~AME
TRAN_DATe
EMPLOYEE DELETE
EMPLOYEE NSHAH
UPOATE 0S-OEC-03
NSHAH
SQL> 0S-DEC-03

F1 gu tt 14-17 AF
j
TER t ng
. ger-m
· action.
_j
niggers

333
In Figure 14-17, you see the workings of the AFTERTRlGGE
A row is deleted from the EMPLOYEE table and the tn· . R of Figure l 4-16.
Is · ggcr mserts
TRAN SH TORY table. Then, a row is updated in the EMPLOYE a row tn .
the
trigger inserts another row in the TRANSHISTORY table. E table, and the ,
INSTEAD OF Trigge r
)
The BEFO RE and AFfER triggers are based on databa se tables From . .\ "
onward , 0 race l prov1'des another type of• tngger
.
called an INSTE· AD OFversion 81 '4
. h. b .
wh1c 1s not ~se d o~ a ta bl e b. ut 1s
. b d · tngger.
ase .on a view (covered in Chapter 9).The IN-
STEAD OF tngger 1s a row tnggcr. If a view is based on a SELECT query'that con-
tains set operators, group functions, GROU P BY and HAVING clauses, DISTINCT
function, join, and/or a ROWN lJM pseudocolumn, data manipu lation is not possi-
ble throug h it.
An INSTEAD OF trigger is used to modify a table that cannot be modified
throug h a view. This trigger fires " instead of' trigger ing DML stateme nts. such
as
DELE TE, UPDA TE, or INSER T.
In Figure 14-18, a complex view is created with a SELEC T query and an outer
join. Facultyld 235 , 333, and 444 are not used in the STUDENT table; in other
words, there is no "child" row in STUDENT table for those facult y members. Facul-
ty Id 235 and 333 are not used in the CRSSECTION table either. The DELET
E
statement to delete Faculty Id 235 in Figure 14-18 returned an error message . We
will accomplish deletion of row by creating an INSTEAD OF trigger.

I SQL> CREATE OR REPLACE VIEW STUDENT_FACULTY


2 AS
I 3 SELECT S.STUDENTIO, S.LAST, S.ARST, F.FACULTYID, F.NAME
4 FROM STUDENT S, FACULTY F
5 WHERE S.FACULTYID(+) • F.FACULTYID;

v..wertated.
SQL> DELETE fROM student_facutty WHERE Facultyld • 23S;
DEUTE FROM student_f acuity WHERE Facultyld • 235

EIUtOR at line 1: rv.<l tablll
ORA..01752: cannot delete from view without exactly on• kay-prete
I
SQL> - - - - - - - - - - - - - - - - - -
l
.
Ylcure 14-18 No data mampulauon t hrou gh coroplel , t(' W

,
. teJ on the •
JS CI<.: J
In Figure 14-19, an INSTE AD OF DELE TE tnggt!r , is issued to
STuDE NT...FA c u - DELE TE sta temt:r
1
. · LTY view. Now, when the . ~ r~d. and the facult-y
delete a fa cuJty membe r with the comple x view, the tngger 15 t 1

You might also like