This document provides an overview of the DB2 10.1 Basic Database Administration Workshop for Linux, Unix and Windows. It introduces the instructor, Iqbal Goralwalla, who has extensive experience developing and working with DB2. The document discusses DB2 editions and key features, tools replaced in DB2 10 like Control Center, the new IBM Data Studio tool, and the DB2 instance and process models.
Boost Fertility New Invention Ups Success Rates.pdf
DBA Basics guide
1. The Information Management Specialists
DB2 10.1 Basic Database
Administration Workshop
for Linux, Unix and Windows
– CL2X3GB
Iqbal Goralwalla
2. The Information Management Specialists
Iqbal Goralwalla (iqbal@triton.co.uk)
– About Me
• IBM Gold Consultant
• IBM Champion for Information Management
• Head of DB2 on Midrange (LUW) at Triton Consulting
• Experience of DB2 LUW since DB2 Common Server (V2)
• IBM Certified Advanced Database Administrator
• Worked at the IBM Toronto Software Lab developing
DB2
Worked on V5, V6, and V8
Owner of 2 IBM patents on V8
60. Moving Data in DB2 UDB for LUW
Utilities
DB2 provides three utilities for mass data
movement
• EXPORT
• IMPORT
• LOAD
LOAD executed at the table level
IMPORT/EXPORT may use views, joins
etc (in certain circumstances)
61. Moving Data in DB2 UDB for LUW
File Formats
Determine how data is physically stored
in external files
Five different file formats supported by
data movement utilities
• ASC (non-delimited ASCII files)
• DEL (delimited ASCII files)
• WSF (Work Sheet Format files)
• IXF (Integrated Exchange Format files)
• CURSOR (V8.1)
62. Moving Data in DB2 UDB for LUW
Delimited ASCII Files (DEL)
Used extensively in RDBMS
Makes use of delimiters
• Row delimiter
• Column delimiter
• Character
100,”Joe”,”Joe Street”
200,”Foo”,”Foo Street”
300,”Moo”,”Moo Street”
63. Moving Data in DB2 UDB for LUW
Non-Delimited ASCII Files (ASC)
Fixed-length ASCII files
Row delimiter
No column or character delimiters
All column values are of fixed length
• Variable length character columns are
padded with blanks
100JoeJoe Street
200FooFoo Street
300MooMoo Street
64. Moving Data in DB2 UDB for LUW
Integrated Exchange Format Files (IXF)
Consist of unbroken sequence of
variable length records
• Numeric values stored as packed decimal
or binary
• Character values stored as ASCII
Cannot be edited using a text editor
IXF files contain structural information
• Can be used to rebuild database objects
65. Moving Data in DB2 UDB for LUW
Worksheet Format Files (WSF)
Used to extract or import data by Lotus
1-2-3 and Symphony products
Not used to move data from one DB2
table to another
Cannot be edited using a text editor
66. Moving Data in DB2 UDB for LUW
Data Movement Utilities and File Formats
Format LOAD IMPORT EXPORT
ASC Yes Yes No
DEL Yes Yes Yes
WSF No Yes Yes
IXF Yes Yes Yes
67. Moving Data in DB2 UDB for LUW
Export
Used to extract data from tables and write
into an external file
Data can be extracted in different file
formats
• IXF
• DEL
• WSF
Files can then be used by the DB2 Load or
Import utilities or other external products
68. Moving Data in DB2 UDB for LUW
Export
EXPORT uses SQL syntax to select data
from the database
SQL can be very versatile and may
• reference views and aliases
• include joins
• filter rows using where clause
• use columnar functions
• use group by and order by clauses
69. Moving Data in DB2 UDB for LUW
Export – minimum requirements
2. Path and file name
3. File type (IXF, DEL,
or WSF)
1. SELECT statement
of del
select * from f1team
f1team.delexport to
70. Moving Data in DB2 UDB for LUW
Export – example
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
export to f1team.del
of del
select * from f1team
1,”Ferrari”,1,”Maranello”,”Italy”
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
71. Moving Data in DB2 UDB for LUW
Export – optional requirements
Message file name to capture all error and
warning messages
New column names when exporting to IXF
or WSF file formats
File type modifier for additional formatting of
DEL and WSF files
File names and paths for exporting LOB
columns
72. Moving Data in DB2 UDB for LUW
Export
Must have SYSADM, or DBADM, or
CONTROL or SELECT on table(s)
Default date format for DEL and WSF files is
yyyymmdd. Can be changed to ISO
representation yyyy-mm-dd by specifying
DATEISO
Default delimiter for DEL format is double
quote (‘’). To override, use CHARDEL
Use tools like Visual Explain to evaluate
performance of Select statement
73. Moving Data in DB2 UDB for LUW
Export – Derived Columns
2 ways to force column rename for IXF
and WSF files:
1. Use the AS clause in SELECT
EXPORT … SELECT GROSS_PAY – TAXES
AS NET_PAY … FROM …
2. Use METHOD N option
EXPORT … METHOD N (‘NET_PAY’,…)
SELECT GROSS_PAY – TAXES, …
FROM …
74. Moving Data in DB2 UDB for LUW
Export – Large Objects
Can include 2GB of LOB data in the target
file
Store each LOB value in it’s own file
EXPORT TO mydata.del of DEL LOBS TO
E:datalobs1, E:datalobs2 LOBFILE mypics …
MODIFIED BY LOBSINFILE SELECT * FROM
mydata
E:datalobs1
mypics.001
E:datalobs1
mypics.002
E:datalobs2
mypics.323
75. Moving Data in DB2 UDB for LUW
Import
Used to move data from an external file into
a table or a view
Data can be imported from various file
formats
• IXF
• DEL
• ASC
• WSF
76. Moving Data in DB2 UDB for LUW
The IMPORT utility uses the SQL
processor to bulk load data
Faster than application programs for
large insert volumes
Triggers are fired and constraints
validated
Import
77. Moving Data in DB2 UDB for LUW
Import – minimum requirements
2. Path and file name
3. File type (IXF, DEL,
ASC, or WSF)
1. Import type
of del
insert into
f1team.del
4. Name or alias of
table or view where
data is to be imported
f1team
import from
78. Moving Data in DB2 UDB for LUW
Import – optional requirements
Message file name to capture all error and
warning messages
Number or rows to insert before committing
changes to table
Number of records to skip from file before
beginning import
Names of table or view columns into which
data will be inserted
79. Moving Data in DB2 UDB for LUW
Import – Insert Mode (1)
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
import from f1team.del
of del
insert into f1team
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
80. Moving Data in DB2 UDB for LUW
Import – Insert Mode (2)
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
import from f1team.del
of del
insert into f1team
(hq_city,country,team_id,name,principal)
”Maranello”,”Italy”, 1,”Ferrari”,1
”Woking”,”Britain”,2,”McLaren”,2
”Didcot”,”Britain”, 3,”Williams”,3
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
81. Moving Data in DB2 UDB for LUW
Import – Insert_Update Mode
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
import from f1team.del
of del
insert_update into f1team
1,”Ferrari”,1,”Rome”,”Italy”
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Rome Italy
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
82. Moving Data in DB2 UDB for LUW
Import – Replace Mode
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
import from f1team.del
of del
replace into f1team
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
Note: Replace mode is not
valid if primary key of F1TEAM
is referenced by a foreign key
in another table
83. Moving Data in DB2 UDB for LUW
Import – Replace_Create Mode (1)
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
1 Ferrari 1 Maranello Italy
import from f1team.del
of ixf
replace_create into
f1team
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
Note: Replace_Create mode is not
valid if primary key of F1TEAM
is referenced by a foreign key
in another table
Note: Only valid for IXF format
84. Moving Data in DB2 UDB for LUW
Import – Replace_Create Mode (2)
import from f1team.del
of ixf
replace_create into
f1team
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
Note: Replace_Create mode is not
valid if primary key of F1TEAM
is referenced by a foreign key
in another table
Note: Only valid for IXF format
85. Moving Data in DB2 UDB for LUW
Import – Create Mode
import from f1team.del
of ixf
create into
f1team
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
2 McLaren 2 Woking Britain
3 Williams 3 Didcot Britain
Note: Only valid for IXF format
86. Moving Data in DB2 UDB for LUW
Importing into a specific tablespace
A target tablespace can be specified
using the CREATE option
IMPORT FROM tabddl.ixf OF IXF
CREATE INTO newtab
IN mytbsp
INDEX IN myindextbsp
LONG IN mylongtbsp
All three tablespaces must be DMS if
INDEX or LONG options are used
87. Moving Data in DB2 UDB for LUW
Import – Usage Considerations
Commit frequency can be tuned
IMPORT … COMMITCOUNT 100 …
A failed import can be restarted
IMPORT … RESTARTCOUNT 200 …
Large objects can be imported into a table
from lob files created by the Export utility
IMPORT FROM mydata.del of DEL
LOBS FROM E:datalobs1, E:datalobs2
MODIFIED BY LOBSINFILE … INTO mydata …
88. Moving Data in DB2 UDB for LUW
Import – Method L
Used to import data from ASC files
Start and end position of each column need
to be specified
F1TEAM
TEAM_ID NAME PRINCIPAL HQ_CITY COUNTRY
Char(3) Varchar(20) Char(3) Varchar(20) Varchar(20)
import from f1team.asc
of asc
method L (1 3, 4 23, 24 26, 27 46, 47 66)
insert into f1team
89. Moving Data in DB2 UDB for LUW
Import – Method P
Column numbers used to select columns
from data file
File type should be DEL or IXF
import from f1team.del
of del
method P (1,2,5)
insert into f1team
1,”Ferrari”,1,”Maranello”,”Italy”
2,”McLaren”,2,”Woking”,”Britain”
3,”Williams”,3,”Didcot”,”Britain”
f1team.del F1TEAM
TEAM_ID NAME COUNTRY
90. Moving Data in DB2 UDB for LUW
Creating an identical table with
Export and Import
Export zero rows from the existing table
into an IXF file
EXPORT TO tabddl.ixf OF IXF
SELECT *
FROM tab
WHERE 1 < 0;
IMPORT FROM tabddl.ixf OF IXF
REPLACE_CREATE INTO newtab;
Import the IXF file into a new table with
the REPLACE_CREATE option
91. Moving Data in DB2 UDB for LUW
Load
Bypasses SQL processing to improve
performance
Pre-formats data pages and populates the
table one extent at a time
Does not fire triggers, invoke constraints or
check referential integrity
Utility can collect statistics and take a
backup during LOAD processing
Requires SYSADM or DBADM or LOAD
authorities
92. Moving Data in DB2 UDB for LUW
Load – minimum requirements
2. Path and file name
3. File type (IXF, DEL,
ASC, or CURSOR)
1. Load type
of del
insert into
f1team.del
4. Name of table where
data is to be loaded
f1team
load from
93. Moving Data in DB2 UDB for LUW
Load – usage considerations
Inserting new data
LOAD FROM mydata.ixf OF IXF …
INSERT INTO mytable …
Replacing data
LOAD FROM mydata.ixf OF IXF …
REPLACE INTO mytable …
Terminating a Load operation
LOAD FROM mydata.ixf OF IXF …
TERMINATE INTO mytable …
94. Moving Data in DB2 UDB for LUW
Load – usage considerations
Generating consistency points
LOAD FROM … SAVECOUNT 200 …
Restarting a failed Load
LOAD FROM mydata.ixf OF IXF …
RESTART INTO mytable …
Forcing Load to fail on warning
LOAD FROM … WARNINGCOUNT 1 …
Specifying a file for rejected rows (only valid
for DEL and ASC file types)
LOAD FROM … OF DEL …
MODIFIED BY DUMPFILE=C:mydump.del
95. Moving Data in DB2 UDB for LUW
LOAD from CURSOR
You can now LOAD from a SELECT
• New file type – CURSOR
• Supports arbitrary SELECT statements – single tables,
joins, nicknames, etc.
• CLP: Need to declare cursor, and cursor name provided
as the input file name to LOAD
DECLARE mycursor CURSOR FOR select *
from t1
LOAD FROM mycursor OF CURSOR
INSERT INTO t2 ALLOW READ ACCESS
96. Moving Data in DB2 UDB for LUW
LOAD from CURSOR – Example
Table t2 in database DB2
DECLARE mycursor CURSOR database DB2
user user1 using pwd1 FOR select * from t2
LOAD FROM mycursor OF CURSOR
INSERT INTO t1 ALLOW READ ACCESS
105. DB2 Administration for LUW – Part 2
Backup of Tablespaces – Usage
Considerations
Backup of tablespaces should be done
together if they contain:
• Tables which have data, indexes, and LOBs
spilt across DMS tablespaces
• Tables related by referential constraints
• Summary and underlying table in different
tablespaces
• Tables related by triggers
108. DB2 Administration for LUW – Part 2
Backup – enhancements – V8.2
Logs in backup images
• Logs can now be included in the online
backup
• Supports all types of online backups such as
database, table space, incremental, and
compressed
• All logs that are needed to restore the backup
and roll forward to the time corresponding to
the end of the backup are placed in the
backup image
116. DB2 Administration for LUW – Part 2
Restore Example 1
Basic restore requires path and time
117. DB2 Administration for LUW – Part 2
Restore Example 2
RESTORE DATABASE FIDB
FROM ‘C:UBackupsF1DB’
TAKEN AT 20020726152238
REPLACE EXISTING;
118. DB2 Administration for LUW – Part 2
Restore Example 3
RESTORE DATABASE FIDB
FROM ‘C:UBackupsF1DB’
TAKEN AT 20020726152238
REPLACE EXISTING
WITHOUT ROLLING FORWARD;
Note: The WITHOUT ROLLING FORWARD
option can NOT be specified if the restore is
taking place from an online backed up
database or from a tablespace level backup
119. DB2 Administration for LUW – Part 2
Restore Example 4
RESTORE DATABASE FIDB
TABLESPACE (userspace1) ONLINE
FROM ‘C:UBackupsF1DB’
TAKEN AT 20020726152238
REPLACE EXISTING;
Note: ONLINE option can only be used for
tablespace or history file restores
120. DB2 Administration for LUW – Part 2
Restore Example 5
RESTORE DATABASE FIDB
HISTORY FILE ONLINE
FROM ‘C:UBackupsF1DB’
TAKEN AT 20020726152238
REPLACE EXISTING;
Note: ONLINE option can only be used for
tablespace or history file restores
121. DB2 Administration for LUW – Part 2
Restore Example 6
How would you restore the database if there was a
crash after the backup taken on Thursday in each
case?
122. DB2 Administration for LUW – Part 2
Redirected Restore
Restore fails if current containers missing
from backup
May want to restore on new system which
may not have necessary containers defined
Redirected Restore allows adding,
changing, or removing of tablespace
containers during a restore
Better to take backup of tablespace
immediately after new containers are added
to the tablespace
123. DB2 Administration for LUW – Part 2
Redirected Restore Example
RESTORE DATABASE FIDB
FROM ‘C:UBackupsF1DB’
TAKEN AT 20020726152238
INTO NEWDB
REDIRECT
WITHOUT ROLLING FORWARD;
124. DB2 Administration for LUW – Part 2
Redirected Restore – defining new
containers
Since containers cannot be shared between
databases, the RESTORE command will return a
SQL1277N error stating that “storage must be
defined” for the new containers
Use LIST TABLESPACES to check state of
containers
Define storage for containers using the SET
TABLESPACE CONTAINERS command
Complete the redirected restore using RESTORE
DATABASE MYDB CONTINUE
125. DB2 Administration for LUW – Part 2
Restore Enhancements – Automatic
Storage
It is now possible to choose the location of the
database path during a restore
It is also possible to redefine storage paths
associated with a database Excellent!
• RESTORE DATABASE TEST1
• RESTORE DATABASE TEST2 TO X:
• RESTORE DATABASE TEST3 DBPATH ON D:
• RESTORE DATABASE TEST3 ON /path1, /path2,
/path3
• RESTORE DATABASE TEST4 ON E:newpath1,
F:newpath2 DBPATH ON D:
126. DB2 Administration for LUW – Part 2
Roll Forward Example 1
ROLLFORWARD DATABASE FIDB
TO END OF LOGS
OVERFLOW LOG PATH (C:LOGS);
127. DB2 Administration for LUW – Part 2
Roll Forward Example 2
ROLLFORWARD DATABASE FIDB
TO 2002-07-26-15.22.38.000000
AND STOP;
128. DB2 Administration for LUW – Part 2
Roll Forward Example 3
ROLLFORWARD DATABASE FIDB
TO END OF LOGS AND COMPLETE
TABLESPACE (USERSPACE1) ONLINE;
129. DB2 Administration for LUW – Part 2
Roll Forward Query Status
Roll forward status
• Working
• Pending
• In progress
• No roll forward pending
Next log file to be read
Log files processed
Last committed transaction
131. DB2 Administration for LUW – Part 2
StandbyActive
Client Reroute
Log
pages
Clients
HADR HADR
Active Connection Active Connection
db2 update alternate server for
database mydb using hostname
sbhost port sbport
Hostname sbhost and port sbport
automatically stored on client
HADR – Overview
db2 TAKEOVER HADR ON
DATABASE mydb
153. The Information Management Specialists
DB2 References
• Getting to know the CLP
http://www.ibm.com/developerworks/data/library/
techarticle/dm-0503melnyk/
• Data Studio – V3.1.1
www.ibm.com/developerworks/downloads/im/data/
154. The Information Management Specialists
DB2 References
• Best Practices
www.ibm.com/developerworks/data/bestpractices/
• DB2 Certification
www.ibm.com/certify
http://www.ibm.com/developerworks/views/data/librar
yview.jsp?sort_order=1&sort_by=Title&series_title_by=d
b2+10.1+fundamentals+certification+exam+610+prep
http://www.channeldb2.com/video/db2-tech-talk-part-
one-certification-prep-for-db2-10-
http://www.channeldb2.com/video/db2-tech-talk-part-
two-certification-prep-for-db2-10-for-linux-un
155. The Information Management Specialists
Redirected Restore – Generate Script
• db2 restore db test from /home/backups taken at
20121122090733 redirect generate script
red_restore.sql
• Modify red_restore.sql. You can modify:
Restore options
Automatic storage paths
Container layout and paths
• Run the modified redirected restore script. For
example: db2 –tvf red_restore.sql