3.1 - DB2 Programming Fundamentals.odp

34
7/23/2019 3.1 - DB2 Programming Fundamentals.odp http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 1/34 © 2010 IBM Corporation Information Management Information Management Ecosystem Partnerships IBM Canada Lab Summer/Fall 2010 DB2 ®  Programming Fundamentals

Transcript of 3.1 - DB2 Programming Fundamentals.odp

Page 1: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 1/34

© 2010 IBM Corporation

Information Management

Information Management Ecosystem PartnershipsIBM Canada Lab

Summer/Fall 2010

DB2®

 Programming Fundamentals

Page 2: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 2/34

2 © 2010 IBM Corporation

Information Management

 Agenda

■ Application Development Environment

■ Embedded SQL

■ Static SQL

■ Dynamic SQL

■ Routines !"pes Benefits #sage !ools for De$eloping %outines

■ Triggers

Page 3: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 3/34

& © 2010 IBM Corporation

Information Management

 Appli'ation De$elopment (n$ironment

■ Combination of ard!are and soft!are used to develop an

application

■ Te D"# Application Development Environment $ADE% iscomposed of several soft!are elements&

  operating s"stem  IBM® Data Ser$er Client

  Data)ase Appli'ation Programming Interfa'e *API+  programming language

  de$elopment tools

 App(,e'uta)le

RuntimeLibs

De$!ools

Prog-ang

DB API

DevLibs

Builds

I"M Data Server Client   .

  p  e  r  a   t   i  n  g   S  "  s

   t  e  m

Page 4: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 4/34

© 2010 IBM Corporation

Information Management

.perating S"stem

■ 'ou can develop D"# database applications on te follo!ing

operating systems&

 AI®

P#

-inu,®

Solaris

3indo4s®

.S

Page 5: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 5/34

5 © 2010 IBM Corporation

Information Management

IBM Data Ser$er Client

■ Te I"M Data Server Client provides

  Support to data)ase appli'ation de$elopment  DB2 administration tools  %untime 'onne'ti$it" to appli'ations

■ To configure te D"# application development( you mustave&

  installed a Data Ser$er Client  'ompleted )asi' 'onfiguration steps for t6e Data Ser$er

Client

Page 6: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 6/34

7 © 2010 IBM Corporation

Information Management

!"pes of IBM Data Ser$er Client

■ I"M Data Server Client is available in # pac)agings&

  I"M Data Server Client8 Complete pa'9age: in'ludes A-- de$elopment dri$ers and

administration tools

8 Supports data)ase administration and applicationdevelopment using an API su'6 as .DBC; C-I; <=(!; or

>DBC8 %e?uired for appli'ations using DB2CI API

  I"M Data Server Runtime Client8 If DB2 'ommand line pro'essor *C-P+ support and )asi'

'lient support for running and deploying applications isneeded i<e< no de$elopment li)raries

8 In'ludes all runtime li)raries

Page 7: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 7/34@ © 2010 IBM Corporation

Information Management

!"pes of IBM Data Ser$er Client and Dri$ers

■ I"M Data Server Drivers

  Smaller footprint t6an Data Ser$er Client  It 'an )e em)edded in appli'ations for redistri)ution

■ Types available& I"M Data Server Driver for *D"C and SQL*

8 for >a$a appli'ations onl"

 I"M Data Server Driver for +D"C and CLI8 for appli'ations using .DBC or C-I onl"

 I"M Data Server Driver ,ac)age8 for appli'ations using .DBC; C-I; <=(!; .-( DB; PP;%u)"; >DBC; or S->

8 if DB2 Command -ine Pro'essor Plus *C-PPlus+ support isneeded

Page 8: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 8/34 © 2010 IBM Corporation

Information Management

Data)ase Appli'ation Programming Interfa'e

■ To configure te ADE for te A,Is tat you !ill use( you must

ave& installed a Data Ser$er Client installed t6e API dri$er*s+

■ A,Is available for use include&

 AD.<=(! DB2 C-I and .DBC DB2CI *'ounterpart to .ra'les .CI+ (m)edded S- >DBC and S->

 .-( DB Perl PP %u)"/%u)" on %ails P"t6on

Page 9: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 9/34E © 2010 IBM Corporation

Information Management

Programming -anguages

C and C

C.B.- and Fortran %(

%u)"/%u)" on %ails

Perl and PP

>a$a P"t6on

SupportedProgramming-anguages

CG; HB <=(! and ot6er <=(! languages

Page 10: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 10/3410 © 2010 IBM Corporation

Information Management

Introdu'tion to (m)edded S-

■ Embedded SQL 

  Appli'ations are 'oded )" em)edding S- statements 4it6in t6e appli'ation sour'e 'ode

■ Caracteristics of Embedded SQL  (m)edded S- data)ase appli'ations 'onne't to

data)ases and e,e'ute em)edded S- statements<  (m)edded S- statements are em)edded 4it6in a 6ostprogramming language 'ode<

  (m)edded S- statements 'an )e e,e'uted stati'all" ord"nami'all"<

  ou 'an de$elop em)edded S- appli'ations for DB2 in t6efollo4ing 6ost programming languages:8 C; C; C.B.-; F.%!%A=; and %(

Page 11: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 11/34

11 © 2010 IBM Corporation

Information Management

Building (m)edded S- Appli'ations

■ Since source code as Embedded SQL code( te ost

language compiler cannot process it-  A pre'ompilation p6ase is ne'essar" to repla'e t6e

(m)edded S- 4it6 $alid 6ost language s"nta,<

■ ,rior to application compilation and lin)ing

  Prepare t6e sour'e files 'ontaining em)edded S-statements using t6e DB2 pre'ompiler < .utputs:8 Modified source file

8 "ind file  'ontains a''ess plans for stati' S- statementsin t6e appli'ation 'ode

  Bind t6e statements in t6e appli'ation to t6e targetdata)ase<

  .n'e pre'ompiled and )ound t6e em)edded S-appli'ation is read" to )e 'ompiled and lin9ed using t6e 6ostlanguagespe'ifi' de$elopment tools<

Page 12: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 12/34

12 © 2010 IBM Corporation

Information Management

Building (m)edded S- Appli'ations Steps

.- Create source files !it embedded SQL

#- Connect to a database( ten precompile eac source file toconvert embedded SQL source statements

/- Compile te modified source files $and oter files !itoutSQL statements% using te ost language compiler $Eg& C

compiler%

0- Lin) te ob1ect files !it te D"# and ost languagelibraries to produce an e2ecutable program-

 Compiling and lin)ing $steps / and 0% create tere3uired ob1ect modules

4- "ind te bind file( if tis !as not already done atprecompile time( or if a different database is going to beaccessed

5- Run te application

Page 13: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 13/34

1& © 2010 IBM Corporation

Information Management

Building (m)edded S- Appli'ations Steps

if (strlen(user) != 0) {

  EXEC SQL CONNECT :user IDENTIFIED BY :" #$

if (strlen(user) != 0) {  %li&C = SQLC'nne%t("*%+

  (SQLC,& )"*,li-s+  SQL.NTS+  (SQLC,& )user+  SQL.NTS+  (SQLC,& )"+  SQL.NTS)#

$

Page 14: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 14/34

1 © 2010 IBM Corporation

Information Management

P%(P and BI=D

■ ,RE, $,REC+M,ILE%

 %eads "our sour'e 'ode; parses and 'on$erts t6eem)edded S- statements to DB2 runtime ser$i'es API'alls

 4rites t6e output to a ne4 modified sour'e file !6e pre'ompiler produ'es a''ess plans for t6e S-

statements 46i'6 are stored toget6er as a pa'9age 4it6int6e data)ase

■ "I6D  done )" default during pre'ompilation *t6e P%(P 'ommand+

 if deferred t6en t6e BINDFILE option needs to )e spe'ified at PREP time in order for a )ind file to )e generated

Page 15: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 15/34

15 © 2010 IBM Corporation

Information Management

Stati' and D"nami' S-

■ Tere are t!o different types of SQL statements&

 stati'all" e,e'uted S- d"nami'all" e,e'uted S-

■ Statically e2ecuted SQL statements

  S"nta, is full" 9no4n at pre'ompile time

8 names for t6e 'olumns and ta)les referen'ed in a statement

must )e full" 9no4n at pre'ompile time8 Stati' J S- statement doesnt '6ange

  S- statements are 'ompiled *a''ess plan is 'reated+ )efore t6eappli'ation is )uilt<

  Stati'all" e,e'uted S- is )est used on data)ases 46ose

statisti's do not '6ange a great deal<8 Sin'e t6e a''ess plan is 'reated at 'ompilation time<

 EXEC SQL UPDATE staff

  SET salary = salary + 10000

  WHERE i != "10 AND #$t = %&'

Page 16: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 16/34

17 © 2010 IBM Corporation

Information Management

Stati' and D"nami' S-

■ Dynamically e2ecuted SQL statements

 are built and e,e'uted )" an appli'ation at runtime

■ A Scenario !ere Dynamic SQL !ould be used& an intera'ti$e appli'ation t6at prompts t6e end user for 9e"

parts of an S- statement

8 (g: Sear'6 for emplo"ees )ased on t6eir name; or t6e lastname; or )ot6<

 str($y)*st,arSt-tDy./

UPDATE staff SET salary = salary 121 WHERE #$t = 34'

  EXEC SQL PREPARE St-tDy. FR56 7*st,arSt-tDy.'  EXEC SQL EXECUTE St-tDy. USIN8 7#$t'

S- statement is 'reatedat e,e'ution time

S- statements is d"nami'all"

prepared and e,e'uted

S- statement is 'reatedat e,e'ution time

Page 17: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 17/34

1@ © 2010 IBM Corporation

Information Management

%outines

■ Routines are database ob1ects&

  'an en'apsulate programming and data)ase logi' t6at 'an)e in$o9ed li9e a programming su)routine from a $ariet" ofS- interfa'es

■ Tere are many useful applications and benefits of using

routines !itin a database or database application- Egs&  (,tending )uiltin S- fun'tion support  (n'apsulate appli'ation logi' t6at 'an )e in$o9ed from an

S- interfa'e  Impro$e appli'ation performan'e )" redu'ing net4or9 traffi'

  Allo4 for faster; more effi'ient S- e,e'ution  Allo4 t6e interopera)ilit" of logi' implemented in differentprogramming languages

  A''ess to features t6at e,ist onl" on t6e ser$er   (nfor'ement of )usiness rules

Page 18: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 18/34

1 © 2010 IBM Corporation

Information Management

!"pes of %outines

■ Definition of a routine can be

  S"stemdefined: )uiltinK pro$ided 4it6 t6e produ't  #serdefined: 'reated )" users

■ Te supported functional types of routines are&  Fun'tions

  Pro'edures *also 'alled stored pro'edures+  Met6ods

■ Te supported routine implementations are&  Builtin routines

8 (g: S#M*+; C.#=!*+ are )uiltin fun'tions

  Sour'ed routines  S- routines

8 Composed of S- and S- P- *Pro'edural -anguage+

  (,ternal routines8 De$eloped outside t6e DB2 data)ase using a programming

language *(g: >a$a; C; C; et'+

Page 19: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 19/34

1E © 2010 IBM Corporation

Information Management

S"stemdefined and #serdefined %outines

■ System7defined routines

  Pro$ided 4it6 t6e produ't  Immediatel" read"touse   %e?uire t6e ne'essar" pri$ileges to in$o9e t6ese routines

■ 8ser7defined routines

  Created )" t6e user   (,tend t6e S- language )e"ond t6e support 46i'6 is'urrentl" a$aila)le

  Implemented in a $ariet" of 4a"s in'luding:8 sour'ing )uiltin routines

8 using S- statements onl"8 using S- 4it6 anot6er programming language

Page 20: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 20/34

20 © 2010 IBM Corporation

Information Management

#ser Defined Fun'tions

■ 8ser9Defined :unctions $8D:s% are special ob1ects tat are

used to e2tend and enance te support provided by tebuilt9in functions available !it D"#-

■ 8nli)e D"#;s built9in functions( user9defined functions canta)e advantage of system calls and D"#;s administrative

A,Is-  SQL 8D:s < coded using SQL ,L  E2ternal 8D:s < coded using a programming language

■ :unctions al!ays return a value&

  SQL Scalar( Table( or Ro!

■ 8ser7defined functions are created $or registered% bye2ecuting te C&E,TE F/NCTION SQL statement-

Page 21: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 21/34

21 © 2010 IBM Corporation

Information Management

#ser Defined Fun'tions (,ample

■ 8D: returning a table as result

■ Te C&E,TE F/NCTION statement defines a table function

tat returns te employees in a specified departmentnumber-

CREATE FUNCTI5N DETE1LOYEES )DEPTN5 ,ARCHAR)"44

RETURNS T,BLE (E1NO C,&(2)+L,STN,1E 3,&C,&(45)+FI&STN,1E 3,&C,&(46))

LAN8UA8E SQL

READS SQL DATA

N5 EXTERNAL ACTI5N

DETER6INISTIC

RETURN

  SELECT E6PN5/ LASTNA6E/ FIRSTN6E FR56 E6PL59EE

WHERE E6PL59EE2W5R:DEPT = DEPTE6PL59EES2DEPTN5

Page 22: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 22/34

22 © 2010 IBM Corporation

Information Management

#ser Defined Fun'tions :: (,e'uting

■ :unctions can be invo)ed from inside a SQL statement

■ E2ample& a SELECT statement tat ma)es use of te

DETE1LOYEES functionSELECT E6PN5/ LASTNA6E/ FIRSTNA6E FR56

T,BLE(DETE1LOYEES(7,007)) AS D

■ Te 8ser7Defined Table :unction is invo)ed by referencingte function in te :R+M clause of an SQL statement !ereit can process a set of input values-

■ Te reference to te table function must be preceded by teTA"LE clause and be contained in brac)ets-

Page 23: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 23/34

2& © 2010 IBM Corporation

Information Management

Stored Pro'edures

■ It is an ordinary program composed entirely of SQL

statements and SQL ,L code tat can be called by anapplication-

■ Stored procedures $S,% can be called locally or remotely-

  Locally& from anoter stored procedure or trigger 

  Remotely& from an application

■ An e2ternal stored procedure is a stored procedure tat is!ritten using a ig7level programming language

 (,ternal stored pro'edures 'an )e more po4erful t6an S- stored

pro'edures )e'ause t6e" 'an ta9e ad$antage of s"stem 'alls andadministrati$e APIs along 4it6 S- statements<

 !6e dra4)a'9 is t6at sin'e t6e" are e,ternal to t6e DB2 engine;t6e" are usuall" not as effi'ient as S- Stored Pro'edures<

Page 24: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 24/34

2 © 2010 IBM Corporation

Information Management

Stored Pro'edures S- P- Support

■ Te SQL ,rocedural Language $SQL ,L% is a language

e2tension of SQL  'onsists of statements and language elements  used to implement pro'edural logi' in S- statements

8 Conditional *IF+; loops *F.%+; e,'eption 6andling; et'

SQL procedures !it SQL ,L  allo4s "ou to effe'ti$el" program in S-  'omplete set of S- P- statements 'an )e used in S-

pro'edures

Page 25: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 25/34

25 © 2010 IBM Corporation

Information Management

Stored Pro'edures P-/S- Support

■ ,L=SQL $,rocedural Language=Structured Query Language%

statements&  'an )e 'ompiled and e,e'uted using DB2 interfa'es   redu'es t6e 'omple,it" of ena)ling e,isting P-/S- 

solutions to 4or9 4it6 t6e DB2 data ser$er 

Te supported interfaces include&  DB2 'ommand line pro'essor *C-P+  DB2 C-PPlus  IBM® Data Studio  IBM .ptim De$elopment Studio

■ ,L=SQL statement e2ecution is not enabled from teseinterfaces by default- ,L=SQL statement e2ecution supportmust be enabled on te D"# data server-

Page 26: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 26/34

27 © 2010 IBM Corporation

Information Management

Stored Pro'edures Creating and In$o9ing

■ Stored ,rocedures

 'reated )" e,e'uting t6e CREATE PR5CEDURE statement in$o9ed )" e,e'uting t6e CALL statement 4it6 a referen'e to

a pro'edure 'an ta9e input; output; and inputoutput parameters; e,e'ute

a 4ide $ariet" of S- statements; and return multiple result

sets to t6e 'aller 

■ ,rocedures can be invo)ed from any!ere tat te CALLstatement is supported including&

 'lient appli'ations

 (,ternal routines *pro'edure; #DF; or met6od+ S- routines *pro'edure; #DF; or met6od+ !riggers *)efore triggers; after triggers; or instead of triggers+ D"nami' 'ompound statements Command line pro'essor *C-P+

Page 27: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 27/34

2@ © 2010 IBM Corporation

Information Management

Stored Pro'edures (,ample

■ An e2ample C&E,TE &OCED/&E statement for te

DET.1EDI,N procedure signature is as follo!s&

CREATE PR5CEDURE DEPT;6EDIAN

)IN #$tN<-#r S6ALLINT/ 5UT -#ia.Salary D5UBLE4

■ 8sing te C,LL statement from te CL,

  spe'if" t6e pro'edure name and appropriate parameterarguments

> (all #$t;-#ia. )?1/ 34

Page 28: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 28/34

2 © 2010 IBM Corporation

Information Management

(,ternal %outines

■ E2ternal routines

 %outine logi' is implemented in a programming languageappli'ation t6at resides outside of t6e data)ase

■ 'ou can create e,ternal pro'edures

 e,ternal fun'tions e,ternal met6ods<

■ "enefits 6arness t6e full fun'tionalit" and performan'e of t6e '6osen

implementation programming language

 a''ess and manipulate entities outside of t6e data)ase

■ >en to use an E2ternal routine re?uire a smaller degree of intera'tion 4it6 t6e DB2

data)ase; )ut t6at must 'ontain a lot of logi' or $er" 'omple,logi'

Page 29: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 29/34

2E © 2010 IBM Corporation

Information Management

De$eloping %outines

■ ,rocedure for Developing Routines

1<36en t6ere is no s"stemdefined routine a$aila)le t6atpro$ides t6e fun'tionalit" t6at is re?uired

2<Determine 46at t"pe of routine to 'reate&<36at implementation to use

8 S- %outine

8 (,ternal %outine<Define t6e interfa'e for t6e routine5<De$elop t6e routine logi'7<(,e'ute S- to 'reate t6e routine@<!est t6e routine

<Deplo" it for general use

Page 30: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 30/34

&0 © 2010 IBM Corporation

Information Management

!ools for De$eloping %outines

■ ?rapical 8ser7Interface $?8I% tool( provided !it D"#&

 IBM® Data Studio8 eas"touse de$elopment en$ironment

8 simplif" t6e pro'ess of 'reating routines

8 de$elop stored pro'edures on one operating s"stem and)uild t6em on ot6er ser$er operating s"stems

■ Command Line Interface( provided !it D"#& DB2 Command -ine Pro'essor  *DB2 C-P+

Page 31: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 31/34

&1 © 2010 IBM Corporation

Information Management

!riggers

■ A trigger  defines a set of actions tat are performed in

response to an insert( update( or delete operation on aspecified table-

■ Li)e constraints( triggers are often used to enforce dataintegrity and business rules-

■ 8nli)e constraints( triggers can also be used to update otertables( automatically generate or transform values forinserted or updated ro!s( and invo)e functions to performtas)s suc as issuing errors or alerts-

■ 8sing triggers places te logic tat enforces business rules inside te database-

Page 32: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 32/34

&2 © 2010 IBM Corporation

Information Management

!riggers (,ample

■ Suppose you ad te follo!ing EM,L+'EES base table--

--and you !anted to create a trigger for EM,L+'EES tat !ill storeinformation about salary canges in a table called SALAR'@IST-

Column 6ame --- Data Type ---

E6PN5 I=!(L(%

FNA6E CA%*20+

LNA6E CA%*&0+

TITLE CA%*10+

DEPART6ENT CA%*20+

SALAR9 D(CIMA-*7;2+

Page 33: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 33/34

&& © 2010 IBM Corporation

Information Management

!riggers (,ample *Continued+

CREATE TRI88ER #-$.;i.(

AFTER UPDATE 5N #-$ly##sREFERENCIN8 NEW AS . 5LD AS

F5R EACH R5W

  INSERT INT5 salary;*ist 

,ALUES )2#-$./

2salary/CURRENT TI6ESTA6P4

=ames t6etrigger 

Spe'ifies t6e a'tion to )e performed46en a trigger is a'ti$ated

!6e a'tion is to )eapplied on'e for

each row  affe'ted)" t6e trigger 

!6e a'tion is to )e applied after  t6e '6anges 'aused )" t6e

a'tual update of t6e su)e't ta)le

Page 34: 3.1 - DB2 Programming Fundamentals.odp

7/23/2019 3.1 - DB2 Programming Fundamentals.odp

http://slidepdf.com/reader/full/31-db2-programming-fundamentalsodp 34/34

Information Management

Information Management Ecosystem PartnershipsIBM Canada Lab

Summer/Fall 2010Questions?

E-mail: [email protected]: “DB2 Academic Wo!sho"#