1.4 - Working With Databases and Database Objects - Odp
1.4 - Working With Databases and Database Objects - Odp
Information Management
Information Management
Agenda
Managing Instances
Cataloging
Information Management
Information Management
Instance
Database
1
Database
2
IB M D B 2 9
Information Management
M o re o n in s ta n c e s
T h e d e fa u lt in s t a n c e f o r a p a r t ic u la r s y s t e m is d e f in e d b y th e
D B 2 IN S T A N C E e n v ir o n m e n t v a r ia b le . I n itia lly f o r W in d o w s
d e f a u lt is D B 2 a n d f o r L in u x / U n ix it is d b 2 in s t 1 .
D B 2 9 In s ta n c e M a n a g e m e n t C o m m a n d s :
4
5
C om m and
P u rp o s e
d b 2 ic r t [In s ta n c e N a m e ]
C re a te s a n e w in s ta n c e
d b 2 id r o p [In s ta n c e N a m e ]
D e le te s ( d r o p s ) a n e x is tin g in s ta n c e
d b 2 ilis t
L is ts a ll in s ta n c e s th a t h a v e b e e n d e fin e d
d b 2 s ta rt
S ta rts th e D B 2 D a ta b a s e M a n a g e r b a c k g ro u n d
p r o c e s s e s f o r th e c u rr e n t in s ta n c e
d b 2 s to p
S to p s th e D B 2 D a ta b a s e M a n a g e r b a c k g ro u n d
p r o c e s s e s f o r th e c u rr e n t in s ta n c e
2010 IBM Corporation
Information Management
Instances
Command
Description
Example
db2start
db2start
db2stop
db2stop -f
db2icrt
Create an instance
db2idrop
Drop an instance
db2idrop f db2inst1
db2ilist
db2ilist
db2imigr
db2iupdt
Information Management
Example
Description
Example
Change a DB Setting
Result:
Information Management
Information Management
Information Management
TABLE SPACES
10
Information Management
11
Information Management
12
Information Management
Ideal for small, personal databases and databases that grow/shrink rapidly
Low maintenance and monitoring
CREATE TABLESPACE tbsp1 MANAGED BY SYSTEM
USING ('d:\acc_tbsp', 'e:\acc_tbsp', 'f:\acc_tbsp')
13
Information Management
Information Management
15
Information Management
Databases
16
Information Management
17
Information Management
DATABASE_PATH
INSTANCE_NAME
Directory with the name of the instance that controls the database.
NODExxxx
T0000000
T0000001
C0000000.TMP
T0000002
Database directory (name matches the database
token assigned to the database).
SQL0000x
DB2EVENT
SQLOGDIR
18
Information Management
Examples:
CREATE DATABASE TESTDB1
database-name
Database is
created on drive C:
Database is
created on drive E:
Automatic store is enabled and
storage paths are C: and D:
19
Information Management
20
Information Management
Configuration
Information (DB CFG)
Log Files
History Files
Must exist
Etc.
21
Can be deleted
Information Management
22
Information Management
Cataloging
23
Information Management
Manual configuration
Configuration Assistant (graphical)
Data Studio (graphical)
Command Line Processor (CLP)
24
Information Management
host_name or ip_addr
database_name
database_alias
node_name
authentication_type
Information Management
Tables
Schemas
Indexes
Aliases
User-Defined
Functions
26
Views
Sequences
Stored
Procedures
Triggers
Packages
Information Management
27
Information Management
Schemas
Benefits of a schema:
Tedious to search through entire database for objects with the
same name
The name of each object needs to be unique only within its schema
Access control
28
Information Management
Schemas
For example
CREATE SCHEMA PAYROLL
CREATE TABLE PAYROLL.STAFF
Table named STAFF is
assigned to the PAYROLL schema
29
Information Management
30
Comment
string
Information Management
Tables
31
DEPARTMENT Table
DEPTID
DEPTNAME
COSTCENTER
A000
ADMINISTRATION
10250
B001
PLANNING
10820
C001
ACCOUNTING
20450
D001
E001
R&D
50120
E002
MANUGACTURING
50220
E003
OPERATIONS
50230
Field
(Column
)
Information Management
Base Tables
Userdefined tables designed to hold persistent user data
table-name
data-type
Result Tables
DB2 Database Managerdefined tables populated with rows retrieved
from one or more base tables in response to a query
32
Information Management
Views
33
Information Management
Defines the
view
T1
EMPID
T2
NAME
INSTID
INSTID
NAME
001
JAGGER, MICK
H01
H01
HARMONICA
002
RICHARDS, KEITH
G01
G01
GUITAR
003
WOOD, RONNIE
G01
D01
DRUMS
004
WATTS, CHARLIE
D01
B01
BASS GUITAR
005
WYMAN, BILL
B01
K01
KEYBOARD
006
JONES, BRIAN
G01
S01
SAXAPHONE
myview
34
column-name(s)
EMPID
NAME
INSTNAME
001
JAGGER, MICK
002
003
WOOD, RONNIE
GUITAR
004
WATTS, CHARLIE
DRUMS
005
WYMAN, BILL
BASS GUITAR
006
JONES, BRIAN
GUITAR
HARMONICA
Information Management
Indexes
DEPTID Index
35
DEPTID
ROW
A000
B001
C001
D001
11
E001
E002
E003
F001
F002
F003
G010
10
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Row 9
Row 10
Row 11
DEPTNAME
COSTCENTER
F001
ADMINISTRATION
10250
B001
PLANNING
10820
E001
ACCOUNTING
20450
E003
HUMAN RESOURCES
30200
A000
R&D
50120
E002
MANUGACTURING
50220
F003
OPERATIONS
50230
C001
MARKETING
42100
F002
SALES
42200
G010
D001
LEGAL
60680
Information Management
Indexes Importance
36
Information Management
Indexes Example
EMPNO
INTEGER
FNAME
CHAR(20)
LNAME
CHAR(30)
TITLE
CHAR(10)
DEPARTMENT
CHAR(20)
SALARY
DECIMAL(6,2)
..and you wanted to create an index such that the index key
consists of the column named EMPNO and all employee numbers
entered will be guaranteed to be unique..
37
Information Management
38
Information Management
Example of INDEX
39
Information Management
Aliases
40
Information Management
Aliases
Example
..you wanted to create an alias that references a table named
EMPLOYEES and you wanted to assign it the name EMPINFO..
CREATE ALIAS empinfo FOR employees
Names the alias. The name must not
identify a table, view, nickname, or alias
that exists in the current database.
41
Information Management
SEQUENCE
42
Information Management
Sequences
43
Information Management
Sequences
44
Information Management
Example of SEQUENCE
CREATE SEQUENCE my_seq START WITH 5 INCREMENT BY 5 CACHE 5
After User1 & User2 are finished, User3 executes the following statement in
Connection3:
SELECT NEXT VALUE FOR my_seq FROM sysibm.sysdummy1
Answer: 55
45
Information Management
Example of SEQUENCE
CREATE SEQUENCE my_sequence CACHE 10 ORDER
ANSWER: 3
46
Information Management
Sequence Example
Consider the following scenario. We have a table, table_1, which has only one column
storing an integer value. The following statements are executed successfully in the orders
shown:
CREATE SEQUENCE my_seq START WITH 1 INCREMENT BY 1 CACHE 5
After a db2 terminate and reconnection to the database, the following statement was
issued:
INSERT INTO table_1 VALUES (NEXT VALUE FOR my_seq);
SELECT * FROM table_1
Information Management
Triggers
48
Using triggers places the logic that enforces business rules inside
the database.
Ex.
Information Management
Example of TRIGGER
CREATE TABLE tab1 (col1 INT);
CREATE TABLE tab2 (col1 INT);
CREATE TRIGGER trig1 AFTER UPDATE ON tab1
REFERENCING NEW AS new1
FOR EACH ROW MODE DB2SQL
INSERT INTO tab2 VALUES(new1.col1);
Output:0
49
Information Management
Triggers Example
EMPNO
INTEGER
FNAME
CHAR(20)
LNAME
CHAR(30)
TITLE
CHAR(10)
DEPARTMENT
CHAR(20)
SALARY
DECIMAL(6,2)
50
Information Management
Names the
trigger
51
Information Management
User-defined Functions
52
Information Management
Stored Procedures
53
Information Management
54
IB M D B 2 9
Information Management
C a llin g a S to r e d P r o c e d u r e
C R E A T E P R O C E D U R E g e t_ s a le s
(IN q u o ta IN T E G E R , O U T re tc o d e C H A R (5 ))
D Y N A M IC R E S U L T S E T S 1 L A N G U A G E S Q L
B E G IN
D E C L A R E s q ls ta te C H A R ( 5 ) ;
D E C L A R E s a le s _ r e s u lt s C U R S O R W IT H R E T U R N F O R
S E L E C T s a le s _ p e r s o n , S U M (s a le s ) A S to ta l_ s a le s
F R O M s a le s G R O U P B Y s a le s _ p e r s o n
H A V IN G S U M ( s a le s ) > q u o t a ;
D E C L A R E E X IT H A N D L E R F O R S Q L E X C E P T IO N
S E T r e tc o d e = s q ls ta te ; O P E N s a le s _ re s u lts ;
S E T r e tc o d e = s q ls ta te ;
EN D
C A L L g e t _ s a le s ( 2 5 , ? )
43
55
IB M D B 2 9
Information Management
D a ta b a s e O b je c ts P a c k a g e s
A p a c k a g e is a n o b je c t th a t c o n ta in s in fo rm a tio n n e e d e d to p r o c e s s S Q L
s t a t e m e n t s a s s o c ia t e d w it h a s o u r c e c o d e f ile o f a n a p p lic a t io n p r o g r a m .
D u r in g th e p r e - c o m p ile p r o c e s s ,
- a s o u r c e c o d e f ile c o n t a in in g e m b e d d e d S Q L s t a t e m e n t s is c o n v e r t e d
in t o a s o u r c e c o d e f ile t h a t is m a d e u p e n tir e ly o f h ig h - le v e l p r o g r a m m in g
la n g u a g e s ta te m e n ts .
- A t t h e s a m e tim e , a c o r r e s p o n d in g p a c k a g e t h a t c o n t a in s t h e a c c e s s
p la n s t h a t w ill b e u s e d t o p r o c e s s e a c h S Q L s t a t e m e n t e m b e d d e d in t h e
s o u r c e c o d e f ile is a ls o p r o d u c e d .
- T h is p a c k a g e m u s t r e s id e in a D B 2 d a ta b a s e th a t c o n ta in s th e d a ta
o b je c ts r e f e r e n c e d b y th e p a c k a g e b e f o r e th e c o r r e s p o n d in g a p p lic a tio n
c a n b e e x e c u te d a g a in s t th a t d a ta b a s e .
T h e p r o c e s s o f c r e a tin g a n d s to r in g a p a c k a g e in a D B 2 d a ta b a s e is k n o w n
a s b i n d in g , a n d b y d e f a u lt , p a c k a g e s a r e a u t o m a tic a lly b o u n d t o a
d a t a b a s e d u r in g t h e p r e c o m p ile p r o c e s s .
4 56
4
Questions?
Summer/Fall 2010
E-mail: [email protected]
Subject: DB2 Academic Workshop
Information Management