1.4 - working with databases and database objects.odp
TRANSCRIPT
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 1/57
© 2010 IBM Corporation
Information Management
Information Management Ecosystem PartnershipsIBM Canada Lab
Summer/Fall 2010
Working with Databases an Database
!b"e#ts in DB2$
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 2/57
2 © 2010 IBM Corporation
In%ormation Management
&gena
■ Servers, Instances, and Databases
■ Managing Instances
■ Creating a DB2 Database
■ Cataloging
■ Database (Data) Objects
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 3/57
' © 2010 IBM Corporation
In%ormation Management
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 4/57
( © 2010 IBM Corporation
In%ormation Management
Ser)ers* Instan#es* an Databases
■ ..uon installation, t!e DB2 Database Manager (rogram files) are coied to t!e server , and aninstance of t!e DB2 Database Manager is created.
■ ..instances are resonsible for managing s"stemresources and databases t!at fall under t!eir control.
■ ..databases are resonsible for managing t!estorage, modification, and retrieval of data.
■ I#S$%#C& is a logical database serverenvironment.
DB2 vie's t!e 'orld as a !ierarc!" of objects
Database1
Database2
Instan#e
Ser)er
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 5/57
+ © 2010 IBM Corporation
In%ormation ManagementI
B M D B 2 ,
M o r e o n i n s t a n c e s
- h e e % a u l t i n s t a n # e % o r a p a r t i # u l a r s . s t e m i s e % i n e b . t h e
D B 2 I # S $ % # C & e n v i r o n m e n t v a r i a b l e I n i t i a l l . % o r W i n o w s
e % a u l t i s 0 D B 2 a n % o r i n u 3 / 4 n i 3 i t i s 0 b 2 i n s t 1
D B 2 , I n s t a n # e M a n a g e m e n t C o m m a n s 5
u r o s eC o m m a n d
S t o p s t h e D B 2 D a t a b a s e M a n a g e r b a # k g r o u n
p r o # e s s e s % o r t h e # u r r e n t i n s t a n # e
b 2 s t o p
S t a r t s t h e D B 2 D a t a b a s e M a n a g e r b a # k g r o u n
p r o # e s s e s % o r t h e # u r r e n t i n s t a n # e
b 2 s t a r t
i s t s a l l i n s t a n # e s t h a t h a ) e b e e n e % i n e b 2 i l i s t
D e l e t e s 6 r o p s 7 a n e 3 i s t i n g i n s t a n # e b 2 i r o p 8 I n s t a n c e N a m e 9
C r e a t e s a n e w i n s t a n # e b 2 i # r t 8 I n s t a n c e N a m e 9
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 6/57
: © 2010 IBM Corporation
In%ormation Management
Instan#es
■ Stand*alone DB2 environment
■ Can !ave multile instances er dataserver
■ %ll instances s!are t!e same e+ecutablebinar" files
■
&ac! instance !as its o'n configuration■ Different soft'are level for an instance
Command Descrition &+amle
db2start Start t!e default instancedb2start
db2sto Sto t!e current instance db2stop -f
db2icrt Create an instance db2icrt –u db2fenc1 db2inst1
db2idro Dro an instance db2idrop –f db2inst1
db2ilist ist all instances db2ilist
db2imigr Migrate an instance after ugrading DB2 db2imigr –u db2fenc1 db2inst1
db2iudt -date an instance after installation of a fi+ ac db2iupdt –u db2fenc1 db2inst1
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 7/57; © 2010 IBM Corporation
In%ormation Management
DB an DBM #on%igurations
Descrition &+amle
/ie' Database Manager Settings db2 get dbm cfg show detail
C!ange a Database Manager Setting db2 update dbm cfg using <parameter> <value>
Descrition &+amle
/ie' Database Settings db2 get db cfg for <database>
db2 connect to <database>
db2 get db cfg show detail
C!ange a DB Setting db2 update db cfg using logprimary 10
UP!"# $ %&' U()*' +,'#"!)* #%,.#/ U(##)" ,*
%!++ (/(P,%'#"$%,*&)'34
(#+#%" $%,*&)'"/P#5 +,'#"!)*5 U(##)" &,6 (#((),*$%,*&)'
<etrie)ing original )alue 6on isk7an upate )alue 6in memor.7
=uer.ing the resulting global
-emporar. table 6DB>C!?FI@7
Change )alue o% logretain Ausere+it b #on%ig parameters
<esult5
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 8/57 © 2010 IBM Corporation
In%ormation Management
Database Storage Moel
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 9/57, © 2010 IBM Corporation
In%ormation Management
DB2 Storage Model
■ O0IC% S$O1%0& MOD& -&BS are mae up o% C!4M?S an <!WS -&BS are pla#e into table spa#es -&B SE&C #an ha)e more then one table
● 3SIC% S$O1%0& MOD&● C!?-&I?< is a ph.si#al storage e)i#e● Can be a e)i#e * ire#tor. or a %ile● Container is assigne to table spa#e✔
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 10/5710 © 2010 IBM Corporation
In%ormation Management
-&B SE&CS
■ $%B& S%C&S is a logical sace bet'een data and storage
devices. Consists o% more then one #ontainers -ables* regular ata* ine3es* an long ata resies in table
spa#es
●
S45 C1&%$& 6 %$&1 6 D1O $%B&S%C&S● $o disla" tablesaces info5
● IS- -&BSE&CS 8 S!W D-&I9●
●
Details about containers can be obtained 'it! a follo'ingcommand.● IS- -&BSE&C C!?-&I?<S %or tablespa#e>i
8S!W D-&I9
●
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 11/57
11 © 2010 IBM Corporation
In%ormation Management
DB2 Storage55 -able Spa#es !)er)iew
■ ogical objects in bet'een logical
table and !"sical containers
■ %llo's assignment of t!e locationof data to articular logical devicesor ortions t!ereof
■ %ll tables, inde+es, and ot!er dataare stored in a table sace
■ Can be associated to a secificbuffer ool Containers can be files,
directories or ra' devices
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 12/57
12 © 2010 IBM Corporation
In%ormation Management
$%B&S%C& S$O1%0& M%#%0&M&#$
■ S"stem 7 Managed Sace (SMS)5
!SGs FI SHS-M M&?&@< allo#ates an manages thespa#e
● Database Managed Sace (DMS)5●
ere* the Database manager #ontrols the storage spa#e●
● %utomatic Storage 'it! DMS5● Databases are enable %or automati# storage ha)e a set o%
one or more storage paths asso#iate with them● & table spa#e #an be e%ine as manage b. automati#
storageJ an its #ontainers assigne an allo#ate b. DB2base on those storage paths
● Hou #annot enable/isable storage on#e it has been e%ine%or atabase
● De%ault t.pe in DB2 ,
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 13/57
1' © 2010 IBM Corporation
In%ormation Management
DB2 Storage55 -able Spa#e Management
■ S"stem Managed Saces (SMS) Data store in %iles representing ata ob"e#ts Spa#e is allo#ate on eman
&##ess to ata #ontrolle using stanar I/! %un#tions o% the !SIeal %or small* personal atabases an atabases that grow/shrink rapil.
● ow maintenan#e an monitoring
■ Database Managed Saces (DMS) Data store in %iles or on raw e)i#es
Storage spa#e preKallo#ate in %ile s.stem* t.pi#all. #ontiguousph.si#all.Ieal %or per%orman#eKsensiti)e appli#ations
● In#rease maintenan#e an monitoring
%#!"# "!$+#(P!%# tbsp1 6!*!'# $/ (/("#6U()*' 37d89acctbsp75 7e89acctbsp75 7f89acctbsp74
%#!"# "!$+#(P!%# tbsp2P!'#():# ; 6!*!'# $/ !"!$!(#
U()*' 3&)+# 7 =storage=dms17 10 64 !U",#():# /#(
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 14/57
1( © 2010 IBM Corporation
In%ormation Management
DB2 Storage55 -able Spa#e Management
■ %utomatic Storage $able Sace
DBM #reates an e3tens #ontainers as neee up thelimits impose b. the storage paths asso#iate with theatabase
&utomati#all. hanles resiLing table spa#es Creates a DMS table spa#e %or regular/large table spa#es
Creates a SMS table spa#e %or user or s.stem temporar.table spa#es
%#!"# !"!$!(# mydb !U",6!")% (",!'# /#(
%,**#%" ", mydb
%#!"# "!$+#(P!%# tbsp1 6!*!'# $/ !U",6!")% (",!'#
#e' DB 8 $BSare !andled b" automatic storage
b" D&9%-$
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 15/57
1+ © 2010 IBM Corporation
In%ormation Management
DB2 Storage55 Bu%%er Eools
■ %rea of main memor" used to cac!e table
and inde+ data■ &ac! database must !ave at least one
buffer ool B. e%ault IBMDF&4-BE is use
Bu%%er pools #an be #reate* roppe or altere
SHSC&-B4FF<E!!S #atalog )iew
a##esses the in%ormation %or the bu%%er poolse%ine in the atabase
■ &ver" table sace associates a secificbuffer ool of t!e same age si:e
Mat#h bu%%er pool siLe with purpose o% tableto in#rease hit ratio
■ Self*$uning Memor" Manager (S$MM)available
%#!"# $U&&#P,,+ bp? P!'#():# %#!"# "!$+#(P!%# tbsp1 P!'#():# $U&&#P,,+ bp?
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 16/57
1: © 2010 IBM Corporation
In%ormation Management
Databases
■ ;!at maes u a DB2 database<
& DB2 atabase is mae up o% a #olle#tion o% ob"e#ts
& atabase #ontains the %ollowing ob"e#ts5
-ables* )iews* ine3es* s#hemas
o#ks* triggers* store pro#eures* pa#kages
Bu%%er pools* log %iles* table spa#es
■ ;!ic! tools can !el "ou create DB2 databases<
First Steps
Control Center 6@4I wiLar7
Comman ine Ero#essor 6CE7
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 17/57
1; © 2010 IBM Corporation
In%ormation Management
Creating a DB2 Database First Steps
■ %s art of t!e DB2installation rocess, t!e9irst Stes anel isdisla"ed allo'ing t!euser to generate a
number of a samledatabases to 'or 'it!
■ $o launc! t!e first stesinterface issue db2fs
from a DB2 commandline
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 18/57
1 © 2010 IBM Corporation
In%ormation Management
-.pi#al Dire#tor. ierar#h. -ree
DATABASE_PAT
INSTANCE_NAME
N!DE""""
DATABASE_NAME
-0000000
-0000001
C0000000-ME
-0000002
S=00003
DB2N?-
S=!@DI<
o#ation spe#i%ie when the atabase was #reate !< the)alue o% the dftdbat! DBM #on%iguration parameter
Dire#tories #ontaining %ile or subKire#tor.#ontainers %or the S3SC%$S%C&* $&MS%C&=*an -S&1S%C&= table spa#es
Dire#tor. with the name o% the instan#e that #ontrols the atabase
Dire#tor. with the name o% the noe number assigne to thispartition 6alwa.s #OD&>>>> i% atabase is nonpartitione7
Dire#tor. with the name that was assigne to theatabase
Database ire#tor. 6name mat#hes the atabasetoken assigne to the atabase7
Dire#tor. %or e)ent monitor ata
Dire#tor. %or transa#tion log %iles
Files neee %or atabase re#o)er. anbookkeeping tasks*
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 19/57
1, © 2010 IBM Corporation
In%ormation Management
Creating a DB2 Database Comman ine
■ $!e C1&%$& D%$%B%S& command initiali:es a ne' database
>>-%#!"#--@-!"!$!(#-@--database-name----------------------->
CREATE DATABASE TESTDB1
CREATE DATABASE TESTDB2 ON C:
CREATE DATABASE TESTDB3
AUTOMATIC STORAGE YESON C:,D: DBPATH ON E:
atabaseKname
Database is#reate on ri)e C5
&utomati# store is enable anstorage paths are C5 an D5
Database is#reate on ri)e 5
3amples5
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 20/57
20 © 2010 IBM Corporation
In%ormation Management
;it! Database, DB2 automaticall" creates■ $!e t!ree table saces are created5
S3SC%$S%C&5 -his is where DB2 s.stem #atalog is kept
that tra#ks all o% the metadata asso#iate with DB2 ob"e#ts $&MS%C&=* & temporar. work area where DB2 #an
pla#e intermediate results -S&1S%C&=K a pla#e where all user ob"e#ts 6tables*
ine3es7 resies b. default
● Con%iguration in%ormation 6DB CF@7
● og %iles* istor. %iles et#
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 21/57
21 © 2010 IBM Corporation
In%ormation Management
With atabases* DB2 automati#all. #reates
Catalog table sacesS3SC%$S%C&
(= re?uired)
● Catalog tables 'it! metadata
● Must e+ist
ConfigurationInformation (DB C90)● og 9iles
● istor" 9iles
● &tc.
-ser $able Saces
-S&1S%C&=
(= or more re?uired)
● Default user table sace
● Can be deleted
● %ll user defined tables
S"stem temorar"table sace
$&MS%C&=
(= re?uired)
● S"stem temorar" sace
● ;or area for oerations,
for e+amle5 join, sorts
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 22/57
22 © 2010 IBM Corporation
In%ormation Management
DB2 Data Ser)er Clients
IBM Data Server Drivers
ight weight eplo.ment solution %or ISNs
Must be installe manuall.
● IBM Data Server Driver for @DBC and S4@ Oa)a store pro#eures an userK
e%ine %un#tions
ODBC* S=O
● IBM Data Server Driver for ODBC and CI !DBC &EI* or CI &EI
● IBM Data Server Driver acage
!DBC* CI* ?-* ! DB* EE*<ub.* ODBC* or S=O
IBM Data Server 1untime Client
ODBC* &D!?-* ! DB* !DBC* CI* EE* an<ub.
as CE but @4I tools are not in#lue
Support D&E e3ploitation* -CE/IE an ?ameEipe* #ataloging
IBM Data Server Client
&ll the %un#tionalit. o% IBM Data Ser)er <untimeClient
Elus %un#tionalit. %or atabase aministration*appli#ation e)elopment* an #lient/ser)er#on%iguration
Capabilities in#lue @4I tools su#h as#on%iguration assistant* #ontrol #enter* )isualstuio tools
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 23/57
2' © 2010 IBM Corporation
In%ormation Management
Cataloging
■ DB2 !as multile directories t!at are used to access
databases
-he s.stem atabase ire#tor. #ontains a list anpointer to where all the known atabases #an be %oun(list db director")
-he noe ire#tor. #ontains in%ormation relating to howan where remote s.stems or instan#es #an be %oun( list node director")
-he Database Conne#tion Ser)i#es 6DCS7 Dire#tor. #ontains in%ormation relating to how an whereatabases on D<D& s.stems #an be %oun( list dcs director")
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 24/57
2( © 2010 IBM Corporation
In%ormation Management
Wa.s o% Cataloging a Database at a Client
■ %utomated configuration using discover"
Searc! discover" #lient sear#hes ser)er on the network
Ano'n discover" one parti#ular ser)er is Puerie %orin%ormation
■ %utomated configuration using access rofiles
Server rofiles #ontains in%ormation on instan#es anatabases
Client rofiles use to upli#ate in%ormation %rom one #lient toanother
■ Manual configuration Con%iguration &ssistant 6graphi#al7
Data Stuio 6graphi#al7
Comman ine Ero#essor 6CE7
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 25/57
2+ © 2010 IBM Corporation
In%ormation Management
Cataloguing55 Setting 4p Communi#ations
■ $o use a remote database5
1 Catalog the remote s.stem 6noe72 Catalog the atabase within the remote noe
■ tools available5 Con%iguration &ssistant 6graphi#al7 Data Stuio 6graphi#al7
Comman ine Ero#essor 6CE7
authenti#ation>t.pe
catalog database sample as mysample at node db2node authentication server
atabase>aliasatabase>name noe>name
catalog tcpip node db2node remote mysystem serverdb2tcp2
alias host>name or ip>ar
ser)i#e>nameor port>number
db2 list AdatabaseBnodeCdirectory
#an be use to %in the locall"catalogued DB or noe
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 26/57
2: © 2010 IBM Corporation
In%ormation Management
Database 6Data7 !b"e#ts
■ Database objects, also no'n as data objects, are used to
control !o' all user data (and some s"stem data) is storedand organi:ed 'it!in a DB2 database
-ser*Defined9unctions
Storedrocedures
acages
$riggersSe?uences%liasesInde+es
/ie's$ablesSc!emas
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 27/57
2; © 2010 IBM Corporation
In%ormation Management
Database Object 5 SC&M%
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 28/57
2 © 2010 IBM Corporation
In%ormation Management
S#hemas
■ Sc!emas (uni?ue identifiers) are objects t!at are used to
logicall" classif" and grou ot!er objects in t!e database.
■ Sc!emas !ave rivileges associated 'it! t!em t!at allo' t!esc!ema o'ner to control '!ic! users can create, alter, anddro objects 'it!in t!em.
■ Benefits of a sc!ema5
-eious to sear#h through entire atabase %or ob"e#ts with thesame name
-he name o% ea#h ob"e#t nees to be uniPue onl. within its s#hema
&##ess #ontrol
When an ob"e#t is #reate without spe#i%.ing a s#hema* ob"e#t willbe asso#iate with an impli#it s#hema using the authorization ID
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 29/57
2, © 2010 IBM Corporation
In%ormation Management
S#hemas
■ Most database objects !ave a t'o*art object name
-he first part being the s#hema name 6or Puali%ier7
-he second is the ob"e#t name
■ ;!en an object is created, "ou can assign it to a secific sc!ema
SCHEMA_NAME.OBJECT_NAME
■ 9or e+amle
CREATE SCHEMA P!/,++
CREATE TABLE P!/,++("!&&
-able name S-&FF isassigne to the E&H<! s#hema
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 30/57
'0 © 2010 IBM Corporation
In%ormation Management
S#hemas 3ample 55 4sing the #omman line
■ %#!"# (%D#6! payroll !U"D,):!"),* user1E
■ %,66#*" ,* (%D#6! payroll )( 7schema for payrollapplication7E
Creates a s#hema %or anini)iual user with the
authoriLation ID Q4S<1Q
S#hemaname
S#hema to#omment on
Commentstring
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 31/57
'1 © 2010 IBM Corporation
In%ormation Management
-ables
■ % relational database resents data as a collection of tables
■ % table consists of data logicall" arranged in columns andro's (records)
ea#h #olumn #ontains )alues o% the same ata t.pe
ea#h row #ontains a set o% )alues %or ea#h #olumn a)ailable
■ $!e storage reresentationof a ro' is called a record
■ t!e storage reresentationof a column is called a field
■ eac! intersection of a ro'and column is called a value
D&$ID D&$#%M& COS$C&#$&1
A000 &DMI?IS-<&-I!? 102+0
B001 E&??I?@ 1020
C001 &CC!4?-I?@ 20(+0
D001 4M&? <S!4<CS '0200
E001 < A D +0120
E002 M&?4@&C-4<I?@ +0220
E003 !E<&-I!?S +02'0
<e#or6<ow7
DE&<-M?- -able
Fiel6Column7
Nalue
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 32/57
'2 © 2010 IBM Corporation
In%ormation Management
-ables ' Main -.pes o% -ables
■ Base $ables
4serRe%ine tables esigne to hol persistent user ata
CREATE TABLE dep!"#e$"
%dep"&d CHAR%'(,
dep"$#e )ARCHAR%30(,
*+"*e$"e! INTEGER(-
■ 1esult $ables DB2 Database ManagerRe%ine tables populate with rows retrie)e
%rom one or more base tables in response to a Puer.
■ Declared $emorar" $ables
4serRe%ine tables use to hol nonpersistent ata temporaril.* on behal%o% a single appli#ation
3pli#itl. #reate b. an appli#ation when the. are neee an impli#itl.estro.e when the appli#ation that #reate them terminates its lastatabase #onne#tion
Create with #%+!# '+,$!+ "#6P,!/ "!$+# statement
tableKname#olumnKname5
names a #olumn o%the table ataKt.pe
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 33/57
'' © 2010 IBM Corporation
In%ormation Management
Niews
■ /ie's can be seen as virtual tables derived from one or more
tables or vie's Create to limit a##ess to sensiti)e ata or group together ata
%rom i%%erent tables in a single ob"e#t
Niews o not #ontain real ata
!nl. the )iew e%inition itsel% is a#tuall. store in the atabase
Can be eletable* upatable* insertable* an reaKonl.
When #hanges are mae to ata through a )iew* the ata is#hange in unerl.ing table itsel%
Can be use inter#hangeabl. with tables when retrie)ing ata
● C#EATE $IE% dept_&ie' AS SELECT ( )#!M department
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 34/57
'( © 2010 IBM Corporation
In%ormation Management
Niews 3ample 55 Simple )iew re%eren#ing two base tables
&MID #%M& I#S$ID
001 O&@@<* MIC H01
002 <IC&<DS* I- G01
003 W!!D* <!??I G01
00' W&--S* C&<I D01
00 WHM&?* BI B01
00/ O!?S* B<I&? G01
I#S$ID #%M&
H01 &<M!?IC&
G01 @4I-&<
D01 D<4MS
B01 B&SS @4I-&<
01 HB!&<D
S01 S&T&E!?
&MID #%M& I#S$#%M&
001 O&@@<* MIC HARMONICA
002 <IC&<DS* I- GUITAR
003 W!!D* <!??I GUITAR
00' W&--S* C&<I DRUMS
00 WHM&?* BI BASS GUITAR
00/ O!?S* B<I&? GUITAR
CREATE )IE myview%e#p&d, $#e, &$"$#e( AS
SELECT T1.e#p&d, T1.$#e, T2.$#eROM T1, T2
HERE T1.&$"&dT2.&$"&d
)iewKname #olumnKname6s7
Ienti%ies the )iewe%inition
De%ines the)iew
-1 -2
m"vie'
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 35/57
'+ © 2010 IBM Corporation
In%ormation Management
Ine3es
■ %n inde+ is an object t!at contains an ordered set of ointers
t!at refer to ro's in a base table. $!e" are based uon oneor more columns but stored as a searate entit".
D&$ID 1O;
A000 +
B001 2
C001
D001 11
E001 '
E002 :
E003 (F001 1
F002 ,
F00' ;
@010 10
DE&<-M?- -able
D&$ID D&$#%M& COS$C&#$&1
001 &DMI?IS-<&-I!? 102+0
B001 E&??I?@ 1020
E001 &CC!4?-I?@ 20(+0
E003 4M&? <S!4<CS '0200
A000 < A D +0120
E002 M&?4@&C-4<I?@ +0220
003 !E<&-I!?S +02'0
C001 M&<-I?@ (2100
F002 S&S (2200@010 C4S-!M< S4EE!<- (2'00
D001 @& :0:0
DE-ID Ine3 <ow 1
<ow 2<ow '
<ow (
<ow +
<ow :
<ow ;
<ow
<ow ,<ow 10
<ow 11
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 36/57
': © 2010 IBM Corporation
In%ormation Management
Ine3es Importan#e
■ ..rovide a fast, efficient met!od for locating secific ro's of
data in ver" large tables.
■ ..rovide a logical ordering of t!e ro's of a table
● ..can be used to enforce t!e uni?ueness of records stored ina table.
■ ..can force a table to use clustering storage, '!ic! causest!e ro's of a table to be !"sicall" arranged according to t!eordering of t!eir inde+ column values.
■
C1&%$& -#I4-& I#D& emnoind+ O#emlo"ees(emno)
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 37/57
'; © 2010 IBM Corporation
In%ormation Management
Ine3es 3ample
■ Suose "ou !ad t!e follo'ing &MO3&&S base table..
..and "ou 'anted to create an inde+ suc! t!at t!e inde+ e"
consists of t!e column named &M#O and all emlo"ee numbersentered 'ill be guaranteed to be uni?ue..
Column#ame ...
Data $"e ...
EMPNO I?-@<
NAME C&<6207
LNAME C&<6'07
TITLE C&<6107DEPARTMENT C&<6207
SALARY DCIM&6:*27
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 38/57
' © 2010 IBM Corporation
In%ormation Management
Ine3es 3ample 6#ontinue7
CREATE U*)FU# INDE4 e#p$+_&$d5ON e#p6+7ee%e#p$+(
U*)FU# pre)ents the table %rom
#ontaining two or more rows with thesame )alue o% the ine3 ke.
tableKname ienti%ies a table onwhi#h an ine3 is to be #reate
#olumnKname ienti%ies a #olumnthat is to be part o% the ine3 ke.
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 39/57
', © 2010 IBM Corporation
In%ormation Management
3ample o% I?DT
■ C<&- -&B arts 6 part>no I?- 6,7 ?!- ?4*
part>name N&<C&<62(7* pert>remain I?- 6,77
■
Creating an I?DT on #olumn part>no
C1&%$& -#I4-& I#D& id+artno O# arts(artno)
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 40/57
(0 © 2010 IBM Corporation
In%ormation Management
&liases
■ %n alias is an alternate name for an object suc! as a table or
vie'.
■ ike other ob"e#ts* an alias #an be #reate* roppe* an ha)e#omments asso#iate with it
■ &liases are publi#l. re%eren#e names* so no spe#ial authorit. orpri)ilege is rePuire to use them
owe)er* a##ess to the table or )iew that an alias re%ers to stillrePuires appropriate authoriLation
■ &liases #an be #reate b. e3e#uting the %#!"# !+)!( S=
statement
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 41/57
(1 © 2010 IBM Corporation
In%ormation Management
&liases
■ &+amle
.ou wante to #reate an alias that re%eren#es a table nameME!HS an .ou wante to assign it the name MEI?F!
%#!"# !+)!( e#p&$8+ &, e#p6+7ee
■ ;!" use an alias instead of t!e actual object name<
So that S= statements #an be #onstru#te su#h that the. areinepenent o% the Puali%ie names that ienti%. the base tables or)iews the. re%eren#e
?ames the alias -he name must notienti%. a table* )iew* ni#kname* or alias
that e3ists in the #urrent atabase
Ienti%ies the table*)iew* ni#kname* or
alias %or whi#h aliasK
name is e%ine
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 42/57
(2 © 2010 IBM Corporation
In%ormation Management
S&4-&#C&
■ Se?uence is an ob"e#t that use to generate ata )alues
automati#all.■ De%ault in#rement )alue is 1
■ E<NI!4S N&4 e3pression returns the most re#ent generategenerate )alue %or sePuen#e
■ ?T- N&4 e3pression returns the ne3t sePuen#e )alue
■ MA*$AL+E of a se,-ence can be e,-a. to MIN$AL+E/
■
■
CREATE SE!E"CE em#$id START %IT& '(( I"CREME"T B) '(CAC&E *
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 43/57
(' © 2010 IBM Corporation
In%ormation Management
SePuen#es
■ % se?uence is an object t!at is used to generate data values
automaticall". -nlie an identit" column, a se?uence is nottied to an" secific column or an" secific table.
&n ientit. #olumn pro)ies a wa. %or DB2 to automati#all.generate a uniPue numeri# )alue %or ea#h row that is ae to thetable
■ Se?uences !ave t!e follo'ing c!aracteristics5
Nalues generate #an be an. e3a#t numeri# ata t.pe that has as#ale o% Lero 6SM&I?-* BI@I?-* I?-@<* or DCIM&7
Conse#uti)e )alues #an i%%er b. an. spe#i%ie in#rement )alue
Counter )alues are re#o)erable Counter )alues are re#onstru#te%rom logs when re#o)er. is rePuire
Nalues generate #an be #a#he to impro)e per%orman#e
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 44/57
(( © 2010 IBM Corporation
In%ormation Management
SePuen#es
■ Se?uences can generate values in one of t!ree 'a"s5
B. in#rementing or e#rementing b. a spe#i%ie amount* withoutbouns
B. in#rementing or e#rementing b. a spe#i%ie amount to auserRe%ine limit an stopping
B. in#rementing or e#rementing b. a spe#i%ie amount to a
userRe%ine limit* an then #.#ling ba#k to the beginning anstarting again
■ &+amle5 if "ou 'anted to create a se?uence t!at generatesnumbers, starting 'it! t!e number =>> and incrementing
eac! subse?uent number roduced b" =>■ %#!"# (#FU#*%# e#p_&d ("!" G)"D 100 )*%#6#*" $/ 10
?ames the sePuen#e
Spe#i%ies the %irst
)alue %or the sePuen#e
Spe#i%ies the inter)albetween #onse#uti)e
)alues o% the sePuen#e
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 45/57
(+ © 2010 IBM Corporation
In%ormation Management
&+amle of S&4-&#C&
C<&- S=4?C m.>seP S-&<- WI- + I?C<M?- BH + C&C +
● 4ser1 su##ess%ull. e3e#utes the %ollowing statements in Conne#tion15
/%-&S #&$ /%-& 9O1 m"se? I#$O 5con=!var
/%-&S #&$ /%-& 9O1 m"se? I#$O 5con=!var
4ser2 su##ess%ull. e3e#utes the %ollowing statement in Conne#tion25
/%-&S #&$ /%-& 9O1 m"se? I#$O 5con2!var
● &%ter 4ser1 A 4ser2 are %inishe* 4ser' e3e#utes the %ollowing statement in
Conne#tion'5
S&&C$ #&$ /%-& 9O1 m"se? 91OM s"sibm.s"sdumm"=
What will be output i% abo)e Puer. is e3e#ute5
Ans+er, **
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 46/57
(: © 2010 IBM Corporation
In%ormation Management
&+amle of S&4-&#C&
C1&%$& S&4-&#C& m"se?uence C%C& => O1D&1
■ $!e follo'ing statements are successfull" e+ecuted in se?uencet!roug! searate database connections5
■
CO##&C$IO#= * /%-&S #&$ /%-& 9O1 m"se?uence I#$O 5con=!var
CO##&C$IO#2 * /%-&S #&$ /%-& 9O1 m"se?uence I#$O 5con2!var
CO##&C$IO#= * /%-&S #&$ /%-& 9O1 m"se?uence I#$O 5con=!var
■
■ 4uestion 5 ;!at is t!e current value of t!e 5con=!var !ost
variable<■ %#S;&15
■
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 47/57
(; © 2010 IBM Corporation
In%ormation Management
SePuen#e 3ample
Consider t!e follo'ing scenario. ;e !ave a table, table=, '!ic! !as onl" one columnstoring an integer value. $!e follo'ing statements are e+ecuted successfull" in t!e orderss!o'n5
C1&%$& S&4-&#C& m"se? S$%1$ ;I$ = I#C1&M&#$ B3 = C%C& E
I#S&1$ I#$O table= /%-&S (#&$ /%-& 9O1 m"se?)F
I#S&1$ I#$O table= /%-&S (#&$ /%-& 9O1 m"se?)F
%fter a db2 terminate and reconnection to t!e database, t!e follo'ing statement 'asissued5
I#S&1$ I#$O table= /%-&S (#&$ /%-& 9O1 m"se?)F
S&&C$ G 91OM table=
;!at value 'ill be t!e last value returned b" t!e ?uer"<
%ns5 H
I % ti M t
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 48/57
( © 2010 IBM Corporation
In%ormation Management
-riggers
■ % trigger defines a set of actions t!at are erformed in
resonse to an insert, udate, or delete oeration on asecified table.
■ ike #onstraints* triggers are o%ten use to en%or#e ata integrit.an business rules
■ 4nlike #onstraints* triggers #an also be use to upate othertables* automati#all. generate or trans%orm )alues %or inserte orupate rows* an in)oke %un#tions to per%orm tasks su#h asissuing errors or alerts
■ 4sing triggers pla#es the logi# that en%or#es business rules insiethe atabase
■ &+. C1&%$& $1I00&1 a"raise B&9O1& -D%$& O#emlo"ees 9O1 &%C 1O;S&$ ne'.salar" salar" G =.=
I % ti M t
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 49/57
(, © 2010 IBM Corporation
In%ormation Management
&+amle of $1I00&1
C1&%$& $%B& tab= (col= I#$)F
C1&%$& $%B& tab2 (col= I#$)F
C1&%$& $1I00&1 trig= %9$&1 -D%$& O# tab=
1&9&1&#CI#0 #&; %S ne'=
9O1 &%C 1O; MOD& DB2S4
I#S&1$ I#$O tab2 /%-&S(ne'=.col=)F
■
■ I#S&1$ I#$O tab= /%-&S(2),()F
■
;!at 'ill be t!e outut 5
■ S&&C$ count(G) 91OM tab2
Outut5>
■
I % ti M t
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 50/57
+0 © 2010 IBM Corporation
In%ormation Management
-riggers 3ample
■ Suose "ou !ad t!e follo'ing &MO3&&S base table..
..and "ou 'anted to create a trigger for &MO3&&S t!at 'illcause t!e value for t!e column named &M#O to be incrementedeac! time a ro' is added to t!e table
Column #ame ... Data $"e ...
EMPNO I?-@<
NAME C&<6207
LNAME C&<6'07
TITLE C&<6107
DEPARTMENT C&<6207
SALARY DCIM&6:*27
In%ormation Management
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 51/57
+1 © 2010 IBM Corporation
In%ormation Management
-riggers 3ample #ontG
%#!"# ")''# e#p$+_&$*
!&"# )*(#" ,* e#p6+7ee&, #!%D ,G UP!"# e#p$+ (#" e#p$+ e#p$+ 9 1
?ames thetrigger
Spe#i%ies the a#tion to be per%ormewhen a trigger is a#ti)ate
-he a#tion is to be
applie on#e for each ro' a%%e#te b. the trigger
-he a#tion is to be applie after the #hanges #ause b. the
a#tual upate o% the sub"e#t table
In%ormation Management
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 52/57
+2 © 2010 IBM Corporation
In%ormation Management
4serKe%ine Fun#tions
■ -serJdefined functions (-D9s) are secial objects t!at are
used to e+tend and en!ance t!e suort rovided b" t!ebuiltJin functions available 'it! DB2.
■ 4nlike DB2Gs builtRin %un#tions* userRe%ine %un#tions #an takea)antage o% s.stem #alls an DB2Gs aministrati)e &EIs
■ 4serKe%ine %un#tions are #reate 6or registere7 b. e3e#utingthe %#!"# &U*%"),* S= statement
■ S= S#alar* -able* or <ow Constru#te using onl. S=statements an #an return a )alue* row or table
■ 3ternal S#alar/-able Written using a highKle)el programminglanguage su#h as C* CUU* or Oa)a an returns a single )alue ortable
In%ormation Management
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 53/57
+' © 2010 IBM Corporation
In%ormation Management
Store Ero#eures
■ %n S4 stored rocedure is an ordinar" rogram comosed
entirel" of S4 statements t!at can be called b" analication.
■ Store pro#eures #an be #alle lo#all. or remotel.
■ &n e3ternal store pro#eure is a store pro#eure that is written
using a highKle)el programming language
3ternal store pro#eures #an be more power%ul than S= storepro#eures be#ause the. #an take a)antage o% s.stem #alls anaministrati)e &EIs along with S= statements
In%ormation Management
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 54/57
+( © 2010 IBM Corporation
In%ormation Management
Store Ero#eures &)antages / Bene%its
■ 1educes net'or traffic
■ %ccess to features t!at e+ist onl" on t!e server
■ &nforcement of business rules
■ % remote stored rocedure rovides t!e most advantages5
It splits the appli#ation logi# an en#ourages an e)en istributiono% the #omputational workloa
It pro)ies an eas. wa. to #all a remote program
In%ormation ManagementI
B M D B 2 ,
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 55/57
++ © 2010 IBM Corporation
In%ormation Management
C a l l i n g a S t o r e d r o c e d u r e
C < & - E < ! C D 4 < g e t > s a l e s
6 I ? P u o t a I ? - @ < * ! 4 - r e t # o e C & < 6 + 7 7D H ? & M I C < S 4 - S - S 1 & ? @ 4 & @ S =
B @ I ?
D C & < s P l s t a t e C & < 6 + 7 V
D C & < s a l e s > r e s u l t s C 4 < S ! < W I - < - 4 < ? F ! <S & & C $ s a l e s e r s o n , S - M ( s a l e s ) % S t o t a l s a l e s9 1 O M s a l e s 0 1 O - B 3 s a l e s e r s o n
% / I # 0 S - M ( s a l e s ) K ? u o t a FD C & < T I - & ? D < F ! < S = T C E - I ! ?
S - r e t # o e s P l s t a t e V ! E ? s a l e s > r e s u l t s VS - r e t # o e s P l s t a t e V
? D
C % g e t s a l e s ( 2 E , < )
In%ormation Management
I B M D B 2 ,
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 56/57
+: © 2010 IBM Corporation
In%ormation Management
D a t a b a s e O b j e c t s 7 a c . a g e s
& a c . a g e i s a n o b " e # t t h a t # o n t a i n s i n % o r m a t i o n n e e e t o p r o # e s s S =
s t a t e m e n t s a s s o # i a t e w i t h a s o u r # e # o e % i l e o % a n a p p l i # a t i o n p r o g r a m D u r i n g t h e r e * c o m i l e r o c e s s *
- a s o u r # e # o e % i l e # o n t a i n i n g e m b e e S = s t a t e m e n t s i s # o n ) e r t e
i n t o a s o u r # e # o e % i l e t h a t i s m a e u p e n t i r e l . o % h i g h K l e ) e l p r o g r a m m i n g
l a n g u a g e s t a t e m e n t s
- & t t h e s a m e t i m e * a # o r r e s p o n i n g p a # k a g e t h a t # o n t a i n s t h e a # # e s s
p l a n s t h a t w i l l b e u s e t o p r o # e s s e a # h S = s t a t e m e n t e m b e e i n t h e
s o u r # e # o e % i l e i s a l s o r o d u c e d
- - h i s p a # k a g e m u s t r e s i e i n a D B 2 a t a b a s e t h a t # o n t a i n s t h e a t a
o b " e # t s r e % e r e n # e b . t h e p a # k a g e b e % o r e t h e # o r r e s p o n i n g a p p l i # a t i o n# a n b e e 3 e # u t e a g a i n s t t h a t a t a b a s e
- h e p r o # e s s o % # r e a t i n g a n s t o r i n g a p a # k a g e i n a D B 2 a t a b a s e i s k n o w n
a s b i n d i n g * a n b . e % a u l t * p a # k a g e s a r e a u t o m a t i # a l l . b o u n t o a
a t a b a s e u r i n g t h e p r e # o m p i l e p r o # e s s
Information Management Ecos stem Partnerships
7/23/2019 1.4 - Working with Databases and Database Objects.odp
http://slidepdf.com/reader/full/14-working-with-databases-and-database-objectsodp 57/57
Information Management
Information Management Ecosystem PartnershipsIBM Canada Lab
Summer/Fall 2010Questions?
E-mail: [email protected]: “DB2 Academic Wo!sho"#