Académique Documents
Professionnel Documents
Culture Documents
cover
Front cover
DB2 9 Database
Administration Workshop for
Linux
(Course code CL2X2)
Student Exercises
ERC 10.3
Student Exercises
Trademarks
IBM is a registered trademark of International Business Machines Corporation.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX
CICS
AIX 5L
ClearCase
DB2 Connect
DRDA
InfoSphere
Lotus
OS/390
S/390
WebSphere
1-2-3
Express
iSeries
MQSeries
OS/400
Symphony
z/OS
Approach
DB2
Distributed Relational
Database Architecture
Informix
LotusScript
Optim
pSeries
Tivoli
zSeries
VMware and the VMware "boxes" logo and design, Virtual SMP and VMotion are
registered trademarks or trademarks (the "Marks") of VMware, Inc. in the United States
and/or other jurisdictions.
Intel and Pentium are trademarks or registered trademarks of Intel Corporation or its
subsidiaries in the United States and other countries.
Java and all Java-based trademarks and logos are trademarks of Sun Microsystems, Inc.
in the United States, other countries, or both.
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Microsoft, Windows, Windows NT and Windows Vista are trademarks of Microsoft
Corporation in the United States, other countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other
countries.
AMD, and combinations thereof, is a trademark of Advanced Micro Devices, Inc.
Other company, product, or service names may be trademarks or service marks of others.
Copyright International Business Machines Corporation 1999, 2010. All rights reserved.
This document may not be reproduced in whole or in part without the prior written permission of IBM.
Note to U.S. Government Users Documentation related to restricted rights Use, duplication or disclosure is subject to restrictions
set forth in GSA ADP Schedule Contract with IBM Corp.
V5.3
Student Exercises
TOC
Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Exercises description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Exercise 1. Starting your lab environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1
Exercise instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Starting the VMware image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Exercise 2. DB2 CLP usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exercise instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Section 1: Exploring the CLP usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Section 2: Exploring the CLPPlus usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Section 3: Looking at Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-1
2-2
2-2
2-3
2-6
Contents
iii
Student Exercises
iv
V5.3
Student Exercises
TOC
Contents
Student Exercises
vi
V5.4
Student Exercises
TMK
Trademarks
The reader should recognize that the following terms, which appear in the content of this
training document, are official trademarks of IBM or other companies:
IBM and the IBM logo are registered trademarks of International Business Machines
Corporation.
The following are trademarks of International Business Machines Corporation, registered in
many jurisdictions worldwide:
AIX
CICS
AIX 5L
ClearCase
DB2 Connect
DRDA
InfoSphere
Lotus
OS/390
S/390
WebSphere
1-2-3
Express
iSeries
MQSeries
OS/400
Symphony
z/OS
Approach
DB2
Distributed Relational
Database Architecture
Informix
LotusScript
Optim
pSeries
Tivoli
zSeries
Trademarks
vii
Student Exercises
viii
V4.0
Student Exercises
pref
Exercises description
The students will learn how to administer a Linux database server using either DB2's GUI
interface or a DB2 Command Window.
Table 0-1: Lab Guide Substitutions
Description
Your Value
Substitution in Lab Guide
Student User ID
inst20
Roots Password
dalvm3
Server hostname
ibmclass
Administrative Server
dasusr1
Admin Server Password
ibm2blue
Initial User Name
inst00
Initial User Password
ibm2blue
DO NOT CHANGE ANY OF YOUR PASSWORDS!
Each exercise in this course is divided into sections as described below.
Objectives This section outlines the learning points of the exercise.
Reference Materials List of documents which you may find useful in performing the
exercise.
Lab Exercise The step-by-step guide to the lab.
The exercise instructions include hints to help you perform the tasks requested or to
provide answers to the questions posed in the exercise. Where possible, both the Terminal
session and the GUI solutions are given.
Optional Exercises This section gives you additional exercises to perform relating to the
unit of discussion. It is strictly optional and should be performed when you have completed
the required exercises. The required exercises pertain to the most pertinent information
provided in the unit. This section may help round out the hands-on experience for a related
unit.
Exercises description
ix
Student Exercises
Lab environment
artno
inx
Pk
tsp02
bio
picture
CLOB
BLOB
tsp01
tsp03
TS
classification
Albums
itemno
Fk
price
type
Reorder values
(itemno,current timestamp)
upd
Stock
ate
Stock.qty <=5
inx
Trigger: reorder
tsp04
er
tsp05
af t
TS
Fk
Pk
cc
t yp
e
inx
artno
title
itemno
qty
trigger
cc
tsp06
TS
new
Reorder
TS
Concerts
itemno
timestamp
trigger
trigger
tsp07
artno
date
city
tsp04
TS
Copyright IBM Corporation 2010
V4.0
Student Exercises
Uempty
Introduction
You will use a VMware virtual machine (VM) image to run exercises
within. The DB2 VMware VM lab image you will use is a Linux image,
in a single user environment.
This image contains the DB2 server used for the CL2X and 3L2X
courses.
1-1
Student Exercises
Exercise instructions
Starting the VMware image
Note
If you are in the ILO class - 3L2X - then your instructor will inform you of how you can use
an eLab environment for your exercises.
Note
Steps 1 through 3 are not used for the eLab environment.
The VMware VM (virtual machine) image you will be using is a Virtual Machine image of an
operating system, run within the native Windows operating system of your computer.
__ 1. Logon to your student workstation with the username and password supplied by
your instructor.
________________________________________________________________
__ 2. Start a Linux VMware VM image on your computer.
__ a. On your local computer, find the VMware Player icon and start it.
__ b. The VMware Player window opens, which is a browser. Navigate on the browser
to the location of your VMware VM image (your instructor will inform you of the
location).
________________________________________________________________
__ c. Select the file with the .vmx extension, whose icon looks like this:
V4.0
Student Exercises
Uempty
Linux
SUSE: Right-click the inst00s Home icon in the upper left of the screen, and
select Browse Folder. This opens a window similar to Windows Explorer that you
can use to work with files in the various directories. Double-click the Filesystem
icon.
__ 6. Maximize your VMware VM image by clicking the Maximize icon on the upper right
of your image window.
__ 7. Leave your Virtual Machine image running while we continue the class.
End of exercise
1-3
Student Exercises
1-4
V4.0
Student Exercises
Uempty
Introduction
Before class started, the DB2 product was installed on the Database
server that you are using. A Database Administration Server (DAS)
was also created and started. Also the default DB2 instance with the
SAMPLE database was created. You will now be able to familiarize
yourself with the CLP and the GUIs.
Refer to Appendix B for the SAMPLE database model.
The general approach taken in the classroom for DB2 9 is to use a
VMware VM image running on the same PC that you log into.
When you are using Linux commands, there might be slight
differences depending on the versions of Linux (RedHat, SuSE, ...).
2-1
Student Exercises
Exercise instructions
Section 1: Exploring the CLP usage
Because you will use the default DB2 instance and the sample database, you must logon
with the appropriate userid and password to be able to perform all operations.
Note
You might have already completed Step 1 in Exercise 1.
__ 1. At the VMware Linux VM image, enter the username and password on the GUI login
screen of the virtual machine (VM). If this VM has to be started, the instructor will
give you directions on how to do so.
Enter inst00 as the Username
Enter ibm2blue as the Password
(Your login and password might differ, depending on the classroom environment and
setup. The instructor will give you the username and password if these are different
in your classroom.)
__ 2. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
__ 3. First, try to get general information about commands that can be used in the CLP.
db2 ? | more
You will get a list with all possible commands. Scroll through this list to see the
possibilities.
__ 4. Now examine what can be done with the get command.
db2 ? get | more
________________________________________________________________
__ 5. Next lets assume you receive an SQLSTATE=08003. How you can determine what
this SQLSTATE means? Execute the correct command and explain the meaning.
________________________________________________________________
__ 6. Next lets examine the options set for your CLP session. Issue the correct command
and determine which options are set to on.
________________________________________________________________
________________________________________________________________
2-2
V4.0
Student Exercises
Uempty
__ 7. Next lets create a script with the name myscript00.cmd, which connects to the
sample database, selects the columns empno and lastname from the table
employee and lists the database manager configuration. Use the appropriate tool
(for example, gedit or vi) to create and save the script.
gedit myscript00.cmd
or
vi myscript00.cmd
Enter these lines in the file:
db2start;
connect to sample;
select name, price from product;
get dbm cfg;
If using vi, save this by issuing :wq within vi.
__ 8. Now execute the created script using your CLP session and echo the current
command.
db2 -tvf myscript00.cmd
__ 9. As there is a lot of information returned from the select statement and the database
manager configuration, it might be helpful to store the information in a file called
myout00.txt to be able to examine it step by step. Execute the script again and
save the output in a file named myout00.txt.
db2 -tvf myscript00.cmd -z myout00.txt
__ 10. You can now examine the file using the more command.
more myout00.txt
2-3
Student Exercises
clpplus
Notice you are not connected to a database yet.
__ a. Connect to the SAMPLE database.
CONNECT inst00/ibm2blue@localhost:30000/sample
Note
If you cannot use the @ key for some reason, simple issue the CONNECT command
and you will be prompted for the rest of the information needed.
2-4
V4.0
Student Exercises
Uempty
2-5
Student Exercises
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com
.ibm.db2.luw.common.doc/doc/c0023859.html
__ 2. Start the Control Center: enter db2cc &. When given the opportunity to select the
Control Center view, just click the OK button. This will provide the advanced view.
__ 3. Click the + next to the All Databases folder in the left pane. Select the + left of the
SAMPLE database to explore what is under the database. You will see folders like
Tables, Views, Aliases, and so forth.
__ 4. Next click the folder named TABLES. In the Contents pane, all tables are shown.
__ 5. Select the EMPLOYEE table. What is displayed in the Detail pane?
________________________________________________________________
________________________________________________________________
__ 6. Now click the OPEN tab in the Detail pane. What is the result?
________________________________________________________________
________________________________________________________________
__ 7. Close this window and click the QUERY tab. What happens now?
________________________________________________________________
________________________________________________________________
__ 8. Leave the select * line and add a line with the following: select empno from
employee; and execute this using the green arrow on the top left corner or by
pressing Ctrl and Enter. Is the execution of two SQL statements successful and are
the results displayed under the Query results tab?
________________________________________________________________
________________________________________________________________
__ 9. Change back to the Control Center using the appropriate button (Object View tab),
and examine the different options by first selecting database and then right-clicking
the database.
Repeat this examination for other objects like instance, table, and so forth.
2-6
V4.0
Student Exercises
Uempty
__ 10. You can also examine the other files under sqllib subdirectory.
ls -l sqllib
__ 11. Determine your systems DB2 registry values:
db2set -all
__ 12. Log off as inst00 from the Windows XP VM image.
End of exercises
2-7
Student Exercises
2-8
V4.0
Student Exercises
Uempty
Introduction
You will now perform the steps needed to create an instance and
examine what file structures are built when creating an instance.
The general approach taken in the classroom for DB2 9 is to use a
VMware VM image running on the same PC that you login to.
When you are using Linux commands, there may be slight differences
depending on the versions of Linux (RedHat, SuSE, ...).
3-1
Student Exercises
Exercise instructions
Section 1: Creating the instance
Note
For the single-user VMware Linux environment, your user ID will generally be inst20.
Note
You may have already completed Step 1 in a previous exercise.
__ 1. Log in as inst00.
login: inst00
inst00s Password: ibm2blue
The login dialog may be different depending on the vendor of the Linux system
but will always require a username and password.
__ 2. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
You may get a message that the db2profile cannot be found this can be
ignored at this time.
__ b. You will need to start the DAS to use the GUIs.
su - dasusr1
(password is ibm2blue)
db2admin start
exit
__ 3. Switch user to root.
su - root
Enter root's password that your instructor gave you.
3-2
V4.0
Student Exercises
Uempty
Background Information Do not execute these steps.
The command syntax to create a DB2 Instance on your Linux server is
(DO NOT issue this command now; it will be issued later):
db2icrt -u FencedID InstName
You must be the Linux system administrator (root) in order to execute this
command. The command can be found in the DB2DIR/instance directory
where DB2DIR =
/opt/ibm/db2/V9.7
As an alternative, you can have the DB2 Installer utility, db2setup, create the
groups and usernames for the DB2 Administration Server and a default user
instance. The DB2 Installer program can also be used to create additional
instances after the DB2 product is installed. Reference the appropriate chapter
for your operating system in the Quick Beginnings Guide.
The InstName and FencedID names must already exist before you issue the
db2icrt command. You also need separate and unique Group names for the
InstName and FencedID names for security purposes. The next set of steps
have you create Linux group names and user names for InstName and
FencedID.
__ 4. Create your FencedID user group named fence20. The use of the term Fenced will
be discussed in lecture later.
Terminal
groupadd fence20
__ 5. Create your user group named adm20. This will be used to provide a level of
authorization in the database server.
Terminal
groupadd adm20
__ 6. Create your instance user name called inst20. inst20's primary group should be
adm20.
Terminal
__ 7. Create your fenced user name of fenced20. The primary group for fenced20 should
be fence20.
Terminal
3-3
Student Exercises
Terminal
Terminal
__ 10. You are now ready to create your instance. Create your instance using inst20 for
InstName and fenced20 for FencedID. This command may take a few minutes to
execute.
Remember, since you are logged in as root, the Linux operating system will not
automatically look in the current directory for an executable, so you must either
specify the complete path, or tell the operating system to look in the current directory
by prefixing the command with ./ as in ./db2icrt. (Thats a dot slash).
/opt/ibm/db2/V9.7/instance/db2icrt -u fenced20 inst20
Note
You may get an error indicating that ulimit cannot raise limit, but you should also
see a DB2 return code (DBI1070I) that indicates that the program db2icrt
completed successfully.
__ 11. Look at the instances that have been created using the db2ilist command. Do
you see any other instances?
_______________________________________________________________
/opt/ibm/db2/V9.7/instance/db2ilist
3-4
V4.0
Student Exercises
Uempty
__ 12. When you created your instance, a directory named sqllib was added under the
HOME directory of the instance (/home/inst20). List the attributes of this directory
by executing the following Linux command:
ls -dl /home/inst20/sqllib
The output should look like this:
drwxrwsr-t
3-5
Student Exercises
3-6
V4.0
Student Exercises
Uempty
__ 7. When you created your instance, the db2icrt command updated your .bashrc to
execute the db2profile command file. Look at your .bashrc file. Note the lines
added by DB2 to execute your db2profile.
You should see lines that indicate:
# The following three lines have been added by DB2.
if [ -f /home/inst20/sqllib/db2profile ]; then
. /home/inst20/sqllib/db2profile
fi
3-7
Student Exercises
________________________________________________________________
(If it is NOT Enterprise Server Edition with local and remote clients,
Communication Support may not have been installed correctly contact your
instructor.)
__ 4. What is the default database path?
________________________________________________________________
__ 5. How is the authentication set for your instance (where will the userid/password be
validated)?
________________________________________________________________
__ 6. Look at your DB2 Registry variables by issuing the following command:
db2set -all
__ 7. What is the DB2 Administration Server (DAS) name?
________________________________________________________________
__ 8. Look at the entry for the DB2 System Name it should be set to the machine name
for your server computer.
________________________________________________________________
__ 9. Update your Database Manager Configuration file to add the correct SYSADM
Group (this may already be done for you). Issue the following command:
db2 update dbm cfg using sysadm_group adm20
__ 10. Update your Database Manager Configuration file to add a TCP/IP Port number in
order to support remote communications. First, find the port number you will be
using:
view /etc/services
3-8
V4.0
Student Exercises
Uempty
Type G to get to the bottom of the file. Your port number (previously set up for you)
should be 30200. Issue :q! to exit the view.
__ 11. Issue the following command:
db2 update dbm cfg using svcename 30200
__ 12. Set the DB2COMM variable in your DB2 Registry indicating that you support TCP/IP
communications to your instance. Issue the following command:
db2set DB2COMM=TCPIP -i inst20
__ 13. Look at your DB2 Registry variables by issuing the following command:
db2set -all
__ 14. What value is DB2COMM set to?
________________________________________________________________
__ 15. Start your instance by issuing the db2start command. If you receive an error
message about not being able to start communications, contact your instructor.
Note
If you get a message about using evaluation period licenses, this can be ignored.
________________________________________________________________
3-9
Student Exercises
End of exercises
V4.0
Student Exercises
Uempty
Create a database
Create a table space
Execute a script file to create multiple table spaces
Access the SYSCAT views containing table space information
List table space information
List container information
Introduction
This exercise will step you through the creation of your database and
the table spaces that will be needed to hold the tables in your
database. In most cases, you will have an option in this exercise to
perform the functions using either of:
Using a Terminal session on your Linux system (referred to as
Terminal)
Using the graphical tools on your Linux system (referred to as GUI)
4-1
Student Exercises
Exercise instructions
Section 0: Setup
If not already done, start your Linux VMware VM image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
GUI
__ From the DB2 Control Center, open the Command Editor (either choose
the Tools option on the menu bar and then select Command Editor, or
choose the Command Editor icon on the tool bar (4th from the left)).
__ From the DB2 Command Editor, in the Commands box, enter:
? create database
__ Choose Selected from the menu bar.
__ Select Execute from the pull-down menu.
V4.0
Student Exercises
Uempty
__ 5. Before we create our MUSICDB database, remember what you learned in lecture
about what some of the defaults will be.
What is the default path that the database will be created on?
What will be the default table space type (SMS or DMS) for the table spaces that
will be created to hold User Tables, Catalog Tables, and Temporary Tables?
What are the default Extent and Prefetch sizes?
What are the default Territory, Code Set and Collating Sequence values?
_______________________________________________________________
__ 6. Create your database with a Database name of MUSICDB using the directories
shown. It will take a few minutes to create the database.
Terminal
4-3
Student Exercises
__ 7. You should now have your MUSICDB database created. The System Database
Directory contains an entry for all databases known by this instance. Check the
System Database Directory for an entry for the MUSICDB database by issuing the
list db directory command.
Terminal
4-4
V4.0
Student Exercises
Uempty
list db directory
__ Press Ctrl-Enter to cause the command to be executed.
__ 8. What is the Database alias name and where did it come from?
________________________________________________________________
__ 9. What does a Directory entry type of Indirect mean?
________________________________________________________________
__ 10. It is time to connect to your MUSICDB database. Check your current connection
state with the get connection state command.
Terminal
GUI
Select the green arrow icon under the Commands tab to execute the
command.
Terminal
GUI
connect to musicdb
4-5
Student Exercises
From the DB2 Command Editor, delete the last statement you entered in
the Commands box.
Choose Selected from the menu bar and then select History.
GUI
Select get connection state in the Commands box, and click the Paste
button.
Execute the command:
get connection state
__ 15. Every database has its own Database Configuration file that contains information
about the database and tuning parameters. Look at the Database Configuration file
for your MUSICDB database.
Terminal
GUI
__ 16. Find the default values for two specific configuration parameters for your database,
LOCKLIST and MAXLOCKS. Since there are a large number of configuration
parameters, we can use grep to find the specific ones we want when using a local
connection the option -i means case insensitive.
_______________________________________________________________
Terminal
GUI
Switch to the DB2 Control Center session looking for these two specific
configuration parameters and their values: LOCKLIST and MAXLOCKS.
__ 17. Values of some of the parameters can be changed. Update the following parameters
and specify the values shown.
Change maxlocks to 20
Change num_freqvalues to 12
Terminal
4-6
V4.0
Student Exercises
Uempty
GUI
Terminal
GUI
Scroll through the output. (You might need to scroll to the right to see both
Current and Delayed Values.)
__ 20. Some default table spaces were created during creation of the database. List the
table space information.
Terminal
GUI
__ 21. What are the table space names and what ID number is associated with the table
space?
________________________________________________________________
________________________________________________________________
________________________________________________________________
Note
dbsize returns the size of the database (in bytes) and is calculated as follows:
dbsize = sum (used_pages * page_size) for each table space (SMS and DMS).
dbcapacity returns the database capacity (in bytes) (not available on partitioned
database systems) and is calculated as follows: dbcapacity = SUM (DMS
usable_pages * page size) + SUM (SMS container size + file system free size per
4-7
Student Exercises
container). If multiple SMS containers are defined on the same file system, the file
system free size is included only once in the calculation of capacity.
__ 24. Table space container information can be displayed with the list tablespace
containers command. Use the Help facility to show the DB2 command syntax.
.
Terminal
GUI
__ 25. Show the container information for table space ID 0. What type of container is this
and where is it located?
________________________________________________________________
Terminal
GUI
__ 26. List the names of the system catalog tables. What are these tables?
________________________________________________________________
db2 list tables for system | more
-orTerminal
GUI
__ 27. Get more detailed information for the table spaces and indicate which table spaces
are set to automatic size increase.
4-8
V4.0
Student Exercises
________________________________________________________________
Uempty
Terminal
GUI
__ 28. Verify which default path containers are associated with the temporary table space
and the default user table space.
________________________________________________________________
Terminal
GUI
4-9
Student Exercises
V4.0
Student Exercises
Uempty
4-11
Student Exercises
From the Control Center right-click Table Spaces and choose Create.
__ Use the Create Table Space Wizard to specify the characteristics as
shown in the instructions above.
__ On the Name page, indicate name of tsp01. Click the I want to manage
my storage manually radio button.
__ On the Type page, indicate Regular.
__ On the Space Management page, indicate Database-managed space
(high performance).
GUI part 1 __ On the Containers page, click Add; specify 4 KB pages, then a size of
104. Indicate the file name as given in the instructions. (Select the
directory name of /dbauto/path1/inst20, but you will need to type
in dms/tsp01 as the Container name since the dms directory does not
yet exist.)
__ Click OK.
__ Ensure that on the Read/Write window, you indicate Extent size as 4
pages (four 4KB pages).
__ Ensure that on the Recovery window, you uncheck the box: Enable
dropped table recovery.
After specifying all the characteristics, check your settings by clicking Show
SQL on the Summary page before creating the table space. Your SQL
should look like this:
CREATE REGULAR TABLESPACE TSP01 PAGESIZE 4 K MANAGED BY
GUI part 2 DATABASE USING ( FILE '/dbauto/path1/inst20/dms/tsp01' 104 )
EXTENTSIZE 4 OVERHEAD 10.5 PREFETCHSIZE 4 TRANSFERRATE 0.14
BUFFERPOOL IBMDEFAULTBP DROPPED TABLE RECOVERY OFF;
Click Finish to create the table space.
Terminal
GUI
V4.0
Student Exercises
Uempty
Terminal
__ 5. Confirm that the additional six table spaces are present. Are your new table space
names listed?
________________________________________________________________
Terminal
GUI
4-13
Student Exercises
________________________________________________________________
__ 8. Where are the rest of the DMS table space files?
db2 list tablespace containers for 7
db2 list tablespace containers for 8
db2 list tablespace containers for 7
Tablespace Containers for Tablespace 7
Container ID = 0
Name
= /dbauto/path1/inst20/NODE0000/MUSICDB/T0000007/C0000000.LRG
Type
= File
Container ID = 1
Name
= /dbauto/path2/inst20/NODE0000/MUSICDB/T0000007/C0000001.LRG
Type
= File
Container ID = 2
Name
= /dbauto/path2/inst20/NODE0000/MUSICDB/T0000007/C0000002.LRG
Type
= File
db2 list tablespace containers for 8
Tablespace Containers for Tablespace 8
Container ID = 0
Name
= /dbauto/path1/inst20/NODE0000/MUSICDB/T0000008/C0000000.USR
Type
= File
Container ID = 1
Name
= /dbauto/path2/inst20/NODE0000/MUSICDB/T0000008/C0000001.USR
Type
= File
Container ID = 2
Name
= /dbauto/path2/inst20/NODE0000/MUSICDB/T0000008/C0000002.USR
Type
= File
__ 9. Display detailed information about the table spaces. You must be connected to the
database first.
Terminal
GUI
connect to musicdb;
list tablespaces show detail;
__ 10. What ID numbers are associated with each of the table spaces? How many usable
pages are there in the DMS table spaces? List a few differences between SMS and
DMS table spaces.
V4.0
Student Exercises
________________________________________________________________
Uempty
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 11. Show the container information for table space ID 4. What type of container is this
and where it is located?
_______________________________________________________________
_______________________________________________________________
Terminal
GUI
__ 12. Show the container information for your SMS table space ID 10. What type of
containers are being used and where are they?
________________________________________________________________
________________________________________________________________
Terminal
GUI
__ 13. From your Terminal session, change directory to the sms directory and list the
directory.
cd /dbauto/path1/inst20/sms/sms01
ls -l
__ 14. Table data is stored in .DAT files, indexes in .INX files, and LOB info in .LB and .LBA
files. And there is an SQLTAG.NAM file that holds overhead information about this
container, and the tables that are found there.
Currently there are no tables in this table space.
__ 15. From your Terminal session, create a table and an index on that table in this table
space. And then list the contents of the directory again. What do you see now? Are
the new files named after the table name?
db2 "create table t (i int) in sms01"
db2 "create index ii on t(i)"
ls -l
________________________________________________________________
Copyright IBM Corp. 1999, 2010
4-15
Student Exercises
Terminal
cd $HOME/lab4
more select_tablespaces.sql
db2 -tvf select_tablespaces.sql
select tbspace, definer, tbspaceid, tbspacetype, datatype
from syscat.tablespaces
GUI
If the results do not show up on the Query Results tab, either change your
select statement to the one shown in the Local instructions so you can see
the fields without excessive scrolling, or exit the Command Editor, reopen it,
and reissue your statement.
__ 19. What is the authorization ID of table space definer for the default table spaces?
________________________________________________________________
__ 20. Which of the various table spaces allows long data types?
________________________________________________________________
__ 21. Which table space only allows temporary table data?
________________________________________________________________
__ 22. Table space information for individual tables can be accessed through the
SYSCAT.TABLES view. The fields that contain table space information are:
TBSPACEID Table space ID of primary table space for this table
TBSPACE Name of primary table space for this table
INDEX_TBSPACE Table space containing the indexes for this table
4-16 DB2 9 DB Admin Workshop for Linux
V4.0
Student Exercises
LONG_TBSPACE Table space containing LONG or LOB data for this table
Uempty
4-17
Student Exercises
__ 25. Now use the MON_GET_CONTAINER function to return metrics about table space
containers:
db2 "SELECT varchar(container_name,70) as container_name,
varchar(tbsp_name,20) as tbsp_name,
pool_read_time
FROM TABLE(MON_GET_CONTAINER('',-2)) AS t
ORDER BY pool_read_time DESC"
__ a. This SQL has been done for you. View the select_mon_get_cont.sql file then
execute it:
more select_mon_get_cont.sql
db2 -tvf select_mon_get_cont.sql
Example output:
CONTAINER_NAME
--------------------------------------------------------------/dbauto/path1/inst20/NODE0000/MUSICDB/T0000000/C0000000.CAT
/dbauto/path1/inst20/NODE0000/MUSICDB/T0000002/C0000000.LRG
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000007/C0000001.LRG
/dbauto/path1/inst20/NODE0000/MUSICDB/T0000003/C0000000.LRG
/dbauto/path1/inst20/dms/tsp01
/dbauto/path1/inst20/dms/tsp03
/dbauto/path1/inst20/dms/tsp06
/dbauto/path1/inst20/dms/tsp02
/dbauto/path1/inst20/NODE0000/MUSICDB/T0000008/C0000000.USR
/dbauto/path1/inst20/NODE0000/MUSICDB/T0000001/C0000000.TMP
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000001/C0000001.TMP
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000001/C0000002.TMP
/dbauto/path1/inst20/NODE0000/MUSICDB/T0000007/C0000000.LRG
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000007/C0000002.LRG
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000008/C0000001.USR
/dbauto/path2/inst20/NODE0000/MUSICDB/T0000008/C0000002.USR
/dbauto/path1/inst20/sms/sms01
TBSP_NAME
POOL_READ_TIME
-------------- -------------SYSCATSPACE
613
USERSPACE1
25
TSP04
14
SYSTOOLSPACE
8
TSP01
6
TSP03
3
TSP06
2
TSP02
1
TSP05
1
TEMPSPACE1
0
TEMPSPACE1
0
TEMPSPACE1
0
TSP04
0
TSP04
0
TSP05
0
TSP05
0
SMS01
0
17 record(s) selected.
Terminal
GUI
connect reset
V4.0
Student Exercises
Uempty
4-19
Student Exercises
V4.0
Student Exercises
Uempty
End of exercises
4-21
Student Exercises
V4.0
Student Exercises
Uempty
Introduction
Now that the database and table spaces have been created, the
database objects needed for your MUSICDB database need to be
created. The DB2 Control Center (GUI exercises) or a DB2 statement
will be used to create at least the first object of each type, such as the
first table. Where there are multiple objects of the same type to be
created, a script file will be executed to create all the remaining objects
of a certain type.
5-1
Student Exercises
Exercise instructions
Throughout the exercise, if the DB2 Message Window appears, or if you are looking at a
Show SQL or Show Command window, in many cases, the exercise instructions will not
specifically tell you to close the window. Read the message, look at the SQL or Command,
and then click Close to close the window.
If a GUI window, other than the Control Center, appears to be hung, check to be sure that
the Control Center does not have a message window open. If there is a message window
open in the Control Center, it might impact the usability of the other GUI tools.
Commands are generally to be entered on the Command Editor, Commands tab. If you
want to enter them on the Terminal session (prefaced with db2), that will generally work as
well.
In some cases, the window might need to be scrolled to access the Action buttons on the
window (like OK, Cancel, or Apply). If you cannot see the Action button, scroll down the
window to see them.
If, at any point in the exercises, when you are importing a new script into the Command
Editor, Commands tab, you are prompted on whether you want to discard your current
script, indicate Yes.
Section 0: Setup
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
5-2
V4.0
Student Exercises
Uempty
Terminal
GUI
__ Scroll down the Files list until you reach a file named
create_table_artists.ddl and select it.
__ Note that the Path box gets filled in with the
create_table_artists.ddl file name.
__ Click OK.
5-3
Student Exercises
Command
Editor
__ 2. Verify the creation of your ARTISTS table and the table description.
Important
Ensure the definition of your table is correct. It could cause unexpected failures in later
exercises if your definitions are not done correctly.
Column
name
--------------ARTNO
NAME
CLASSIFICATION
BIO
PICTURE
Type
schema
--------SYSIBM
SYSIBM
SYSIBM
SYSIBM
SYSIBM
Type
name
Length
Scale Nulls
--------- -------- ----- -----SMALLINT
2
0 No
VARCHAR
50
0 Yes
CHARACTER
1
0 No
CLOB
102400
0 Yes
BLOB
512000
0 Yes
Terminal
GUI
connect to musicdb;
list tables for user;
describe table artists;
__ 3. You will create the remainder of the tables required using a script file called
create_tables.ddl. It is located in the directory $HOME/lab5 on the Linux
Database Server. Examine the DB2 statements in the file and answer the following
questions.
In which table space will both the indexes and data for table STOCK be placed? For
CONCERTS? For REORDER?
________________________________________________________________
________________________________________________________________
5-4
V4.0
Student Exercises
Uempty
A script has been provided for you:
Terminal
more create_tables.ddl
__ Select Commands tab on the Command Editor panel.
__ Selected -> Open (menu bar)
__ Verify that the System name is your local workstation.
__ In the Directories list, select the $HOME/lab5 directory by clicking twice.
GUI
__ Scroll down the Files list until you reach a file named
create_tables.ddl and select it.
__ Note that the Path box gets filled in with the create_tables.ddl file
name.
__ Click OK.
__ 5. Verify that all tables (ALBUMS, CONCERTS, REORDER, and STOCK) were
created successfully.
Terminal
db2
db2
db2
db2
db2
5-5
Student Exercises
pipe the output to a file named tabchk.file. The SQL can be found in a file
named tabchk.sql.
Place the output in the $HOME/lab5 subdirectory.
From your Terminal session, enter the following commands:
Terminal
Terminal
If you see any output (lines will be preceded with '<' and '>'), there are lines
that do not match between the files. The first line will indicate the
information in your output, the second line will indicate the information in the
master output. The first column will indicate which table has the incorrect
definition; the second column will indicate which column has the incorrect
definition. Examine this information, drop the table with a problem, and go
back to the step that defined it and define it correctly. Come back to the
previous step and check it again.
__ 8. Information about a table's definition to a table space can be found by accessing the
SYSCAT.TABLES view. Enter an SQL statement to select the columns TABNAME,
TBSPACE, and INDEX_TBSPACE from SYSCAT.TABLES for all tables with the
TABSCHEMA of 'INST20', order the data by TABNAME, and pipe the output to a file
named tbschk.file. The SQL can be found in a file named tbschk.sql.
Place the output in the $HOME/lab5 subdirectory.
________________________________________________________________
________________________________________________________________
5-6
V4.0
Student Exercises
Uempty
db2 -tf tbschk.sql > tbschk.file
more tbschk.file
Terminal
tbschk.sql contains: select tabname, tbspace, index_tbspace from
syscat.tables where tabschema = user order by tabname
On a DB2 Command Window:
db2 -tf tbschk.sql > tbschk.file
more tbschk.file
GUI
tbschk.sql contains: select tabname, tbspace, index_tbspace
from syscat.tables where
tabschema = user order by tabname
Terminal
If you see any output (lines will be preceded with '<' and '>'), there are lines
that do not match between the files. The first line will indicate the
information in your output, the second line will indicate the information in the
master output. The first column will indicate which table has the incorrect
definition; the second column will indicate the table space for the data; the
third column will indicate the index table space. Examine this information,
drop the table with a problem, and go back to the step that defined it and
define it correctly. Come back to the step where you ran the select against
SYSCAT.COLUMNS (4 steps ago) and check the columns and the table
spaces again.
5-7
Student Exercises
__ 10. Grant select privilege on tables that are owned by inst20 to public by executing a
script file, grants.cmd (an operating system command file).
more grants.cmd
./grants.cmd
5-8
V4.0
Student Exercises
Uempty
GUI
__ 2. Create a unique index called ITEMNO on the ITEMNO column in ALBUMS table.
Terminal
5-9
Student Exercises
__ Using the DB2 Control Center, from the MUSICDB objects list, right-click
and select Indexes.
__ Select Create > Index from the pop-up menu.
__ Indicate the following:
__ Index schema of inst20.
__ Index name of itemno.
__ Table schema of inst20.
GUI
__ 3. Select the information from the catalog tables about your indexes. The columns you
should select are the first 18 characters of TABNAME, UNIQUERULE, the first 18
characters of INDNAME, and the first 30 characters of COLNAMES from
SYSCAT.INDEXES, where INDSCHEMA is INST20, order by TABNAME and
INDNAME.
If UNIQUERULE = "U", then only unique values are allowed.
If UNIQUERULE = "D", then duplicate values are allowed.
If UNIQUERULE = "P", then it is a Primary key.
Do you see your new indexes?
________________________________________________________________
A script has been provided for you:
Terminal
more select_index.sql
db2 -tvf select_index.sql
On the Command Editor:
GUI
V4.0
Student Exercises
Uempty
__ 4. Use the select you executed in the previous step and route the output to
indchk.file. The SQL can be found in a file named indchk.sql.
Check your results against indchk.master.
Ensure that there are no differences between your output and the master
output, or the following exercises might not work correctly!
db2 -tf indchk.sql > indchk.file
more indchk.file
diff -w indchk.file indchk.master | more
If you see no output (the command prompt comes back immediately), your
table definitions match our master file.
Terminal
If you see any output (lines will be preceded with '<' and '>'), there are lines
that do not match between the files. The first line will indicate the
information in your output, the second line will indicate the information in the
master output. The first column will indicate which table has the incorrect
definition; the third column will indicate the index that has a problem.
Examine this information, drop the index with a problem, and go back to the
step that defined it and define it correctly. Come back to this step and check
the indexes again.
indchk.sql contains: select substr(tabname,1,18), uniquerule,
substr(indname,1,18), substr(colnames,1,30) from syscat.indexes where
indschema = user and uniquerule <> P order by tabname, indname
5-11
Student Exercises
more create_music_view.ddl
db2 -tvf create_music_view.ddl
__ On the Control Center, from the MUSICDB objects list, right-click and
select Views.
__ Select Create from the pop-up menu.
__ Indicate a View schema of inst20.
GUI
Terminal
GUI
__ Scroll down the Files list until you reach a file named
create_view.ddl and select it. Alternatively, you can type in the full
path name in the File name field.
__ Click OK.
__ Press Ctrl-Enter to execute.
V4.0
Student Exercises
Uempty
The SQL has been done for you. View the select_views.sql file, then
execute it:
Terminal
more select_views.sql
db2 -tvf select_views.sql
Use the DB2 Command Editor to execute one of the following command
sets:
GUI
connect to musicdb;
list tables for user;
or
select tabschema, tabname, type
from syscat.tables where tabschema = user;
or
select viewschema, viewname
from syscat.views where definer = user;
5-13
Student Exercises
Terminal
GUI part 1
GUI part 2
__ 2. Information about aliases can be found in the SYSCAT.TABLES view. Tables, views,
and aliases for the current user can also be shown with the LIST TABLES
statement. A TYPE of A is an alias.
Issue one of the following statements and verify your aliases are listed.
LIST TABLES statement
OR
Select columns TABNAME and TYPE from the SYSCAT.TABLES view with a
TABSCHEMA of INST20.
V4.0
Student Exercises
Uempty
db2 list tables for user
Terminal
or
db2 "select substr(tabname,1,18), type
from syscat.tables where tabschema = user
From the Command Editor:
GUI
connect to musicdb;
list tables for user;
or
select tabname, type from syscat.tables where tabschema = user;
5-15
Student Exercises
more alter_table_albums.ddl
db2 -tvf alter_table_albums.ddl
__ Using the DB2 Control Center, from the MUSICDB objects list, left-click
and select Tables.
GUI part 1 __ In the Contents pane, scroll down until you reach the ALBUMS table.
__ Right-click and select ALBUMS and then select Alter from the pop-up
menu.
Define the primary key.
__ Select Keys tab.
GUI part 2
V4.0
Student Exercises
Uempty
__ 2. Now alter the STOCK table to define its referential integrity relationships, by
executing the script file, create_ri.ddl.
more create_ri.ddl
Terminal
db2 -tvf create_ri.ddl
__ Select Commands tab on the Command Editor panel.
__ Select Selected -> Open from the menu bar.
__ Verify that the System name is your local workstation.
GUI
5-17
Student Exercises
more select_ri.sql
db2 -tvf select_ri.sql
From the DB2 Command Editor, Commands tab:
GUI
__ 4. Run a select to retrieve the first 18 characters of TABNAME, and the PARENTS and
CHILDREN columns from SYSCAT.TABLES where the TABSCHEMA is equal to
your userid. Order the results by TABNAME and direct your output to a file named
richk.file. The SQL can be found in a file named richk.sql.
Check your output against the richk.master file.
Ensure that there are no differences between your output and the master
output, or the following exercises might not work correctly!
V4.0
Student Exercises
Uempty
db2 -tf richk.sql > richk.file
more richk.file
diff -w richk.file richk.master | more
If you see no output (the command prompt comes back immediately), your
table definitions match our master file.
Terminal
If you see any output (lines will be preceded with '<' and '>'), there are lines
that do not match between the files. The first line will indicate the
information in your output, the second line will indicate the information in the
master output. The first column will indicate the table that has a different
definition from our solution; the second column indicates how many
relationships it is in as a dependent; the third column indicates how many
relationships it is in as a parent.
Examine this information, go back to the information in the exercise that
directed you on what you should set up, and alter the table with a problem
(or its dependent) to correct the problem.
Come back to this step and check it again.
Contents of richk.sql: select substr(tabname,1,18), parents, children from
syscat.tables where tabschema = user order by tabname
5-19
Student Exercises
Terminal
GUI
Command
Editor
V4.0
Student Exercises
Uempty
more select_check_constraint.sql
db2 -tvf select_check_constraint.sql
From the Command Editor:
select constname, tabname, colname from syscat.colchecks;
GUI
If you see any output (lines will be preceded with '<' and '>'), there are lines
that do not match between the files. The first line will indicate the
information in your output, the second line will indicate the information in the
master output. If the differences are just spacing between the items (not
within the quotes), then the differences are OK. If there are any other
differences, you will need to drop the constraint you have defined, and
define it again correctly. Then, come back to this step and check it again.
ckchk.sql contains: select substr(text,1,100) from syscat.checks where
constname = 'CCTYPE'
5-21
Student Exercises
more create_trigger.ddl
db2 -tvf create_trigger.ddl
__ Select Commands tab on the Command Editor panel.
__ Selected -> Open (menu bar)
__ Verify that the System name is your local workstation.
__ In the Directories list, select the $HOME/lab5 directory by clicking twice.
GUI
__ Scroll down the Files list until you reach a file named
create_trigger.ddl and select it.
__ Note that the Path box gets filled in with the create_trigger.ddl file
name.
__ Click OK.
V4.0
Student Exercises
Uempty
more select_trigger.sql
db2 -tvf select_trigger.sql
From the Command Editor, Commands tab:
GUI
5-23
Student Exercises
Schema
-------INST20
INST20
INST20
INST20
INST20
Type
----T
T
T
A
V
Creation time
---------2009-08-18-14.53.43.884059
2009-08-18-14.52.06.265003
2009-08-18-14.53.44.470940
2009-08-18-15.13.46.182169
2009-08-18-15.06.37.013432
Copyright IBM Corp. 1999, 2010
V4.0
Student Exercises
Uempty
MUSIC
INST20
REORDER
INST20
SINGERS
INST20
STOCK
INST20
T3
INST20
10 record(s) selected.
V
T
A
T
G
2009-08-18-15.05.55.212258
2009-08-18-14.54.25.192124
2009-08-18-15.13.30.307215
2009-08-18-14.53.44.358608
2009-08-18-15.22.55.352803
__ 8. In Window 2, insert a row of data into the inst20.t3 table and check it with a select:
db2 "insert into inst20.t3 values (100)"
db2 "select * from inst20.t3"
Example output:
C1
----------100
1 record(s) selected.
5-25
Student Exercises
Type
----T
T
T
A
V
V
T
A
T
G
Creation time
---------2009-08-18-14.53.43.884059
2009-08-18-14.52.06.265003
2009-08-18-14.53.44.470940
2009-08-18-15.13.46.182169
2009-08-18-15.06.37.013432
2009-08-18-15.05.55.212258
2009-08-18-14.54.25.192124
2009-08-18-15.13.30.307215
2009-08-18-14.53.44.358608
2009-08-18-15.22.55.352803
Notice that the t3 structure still remains for you to use again.
V4.0
Student Exercises
Uempty
GUI
GUI
xquery db2-fn:xmlcolumn('CUSTOMER.INFO');
__ Execute the query by pressing CTRL-Enter.
__ 4. Now use XQUERY with SQL and db2-fn:sqlquery function to retrieve all of the
XML documents from the customer tables info column.
________________________________________________________________
________________________________________________________________
Terminal
5-27
Student Exercises
GUI
__ 5. Use XQUERY and the db2-fn:sqlquery function to retrieve, from the INFO column
in the CUSTOMER table, all customers where the customer ID (column CID) equals
1002.
________________________________________________________________
________________________________________________________________
Terminal
GUI
__ 6. Use the DB2 Control Center and the XML Document View to display the
CUSTOMER.INFO document in the first row of the table. View the document in both
Tree view and Source view.
________________________________________________________________
________________________________________________________________
V4.0
Student Exercises
Uempty
__ Using the DB2 Control Center connect to the SAMPLE database.
__ From the SAMPLE objects list, select Tables, then right-click the
CUSTOMER table icon and select Open.
__ On the first row of the table in the Open Table panel, click the three dots
(...) in the INFO column.
GUI
__ The XML Document Viewer panel opens, displaying the Tree view of the
document. Note that you can expand the various elements to view the
information.
__ Click the Source view to see the actual document code.
__ Click the Preferences button and select Format Text, then click OK. Note
the structural view.
5-29
Student Exercises
V4.0
Student Exercises
Uempty
r on /home/inst20/lab5/create_table_artists.out;
create table artists
(artno
smallint not null,
name
varchar(50),
classification char(1) not null,
bio
clob(100K) logged compact,
picture
blob(500K) not logged compact,
primary key (artno))
in tsp01
index in tsp02
long in tsp03 ;
create_tables.ddl
connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/create_tables.log
r on /home/inst20/lab5/create_tables.out;
create table albums
(title
varchar (50),
artno
smallint not null,
itemno
smallint not null)
in tsp04
index in tsp05;
create table
(itemno
type
price
qty
in tsp06;
stock
smallint not null,
char (1) not null,
decimal (5,2) not null with default,
int not null with default)
create table
(artno
date
city
in tsp04;
concerts
smallint not null,
date not null,
varchar (25) not null with default)
5-31
Student Exercises
create_trigger.ddl
Connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/create_trigger.log
r on /home/inst20/lab5/create_trigger.out;
create trigger reorder
after update of qty on stock
referencing new as n
for each row
mode db2sql
when (n.qty <= 5)
insert into reorder values (n.itemno, current timestamp);
create_view.ddl
connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/create_view.log
r on /home/inst20/lab5/create_view.out;
create view inventory (type, itemno, totcost, totqty)
as select type, itemno, sum (price * qty), sum (qty)
from stock group by type, itemno;
grants.cmd
db2 "connect to musicdb user inst20 using ibm2blue"
db2 "grant select on table artists to public"
db2 "grant select on table albums to public"
db2 "grant select on table stock to public"
db2 "grant select on table concerts to public"
db2 "grant select on table reorder to public"
indchk.sql
select substr(tabname,1,18), uniquerule, substr(indname,1,18),
substr(colnames,1,30)
from syscat.indexes
where indschema = user order by tabname, indname;
list_all.sql
connect to musicdb user inst28 using ibm2blue;
V4.0
Student Exercises
Uempty
5-33
Student Exercises
r on /home/inst20/lab5/select_index.out;
select substr(tabname,1,18) as tabname, uniquerule,
substr(indname,1,18) as indname, substr(colnames,1,30) as colnames
from syscat.indexes
where indschema = user and uniquerule <> 'P'
order by tabname, indname;
select_ri.sql
connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/select_ri.log
r on /home/inst20/lab5/select_ri.out;
select substr(constname,1,18) as constname,
substr(tabname,1,18) as tabname,
substr(reftabschema,1,14) as reftabschema,
substr(reftabname,1,14) as reftabname,
deleterule,
substr(fk_colnames,1,14) as fk_colnames,
substr(pk_colnames,1,14) as pf_colnames
from syscat.references where tabschema = user;
select substr(tabname,1,18) as tabname, parents, children
from syscat.tables where tabschema = user
and (parents > 0 or children > 0) order by 2, 3 desc;
select_trigger.sql
connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/select_trigger.log
r on /home/inst20/lab5/select_trigger.out;
select substr(trigname,1,18) as trigname,
substr(tabname,1,18) as tabname, trigevent
from syscat.triggers where trigname = 'REORDER';
select substr(trigname,1,18) as trigname,
btype,
substr(bschema,1,14) as bschema,
substr(bname,1,14) as bname
from syscat.trigdep where trigname = 'REORDER';
5-34 DB2 9 DB Admin Workshop for Linux
V4.0
Student Exercises
Uempty
select_views.sql
connect to musicdb user inst20 using ibm2blue;
update command options using
l on /home/inst20/lab5/select_views.log
r on /home/inst20/lab5/select_views.out;
select substr(tabschema,1,18) as tabschema,
substr(tabname,1,18) as tabname, type
from syscat.tables
where tabschema = user ;
select substr(viewschema,1,18) viewschema,
substr(viewname,1,18) as viewname
from syscat.views where definer = user ;
tabchk.sql
select tabname, colname, typename
from syscat.columns
where tabschema = user
order by 1,2;
tbschk.sql
select tabname, tbspace, index_tbspace
from syscat.tables
where tabschema = user
order by tabname;
End of exercise
5-35
Student Exercises
V4.0
Student Exercises
Uempty
Use the Import utility to insert data from a file into a table
Use the Load utility to fast load data from a file into a table
Create exception tables
Manage check constraints, triggers, and set integrity pending
status
Introduction
Now that the database objects have been created in your MUSICDB
database, it is time to load data into the tables. This will be
accomplished by using the Import and Load utilities.
Note: You will be using scripts to do some of the steps within this
exercise.
With some codeset and territory values, you might need to use the
MODIFIED BY FORCEIN syntax on the Load or Import statements.
Your instructor will let you know if this is a requirement for your
environment.
6-1
Student Exercises
Exercise instructions
Section 0: Setup
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
Terminal
cd $HOME/lab6
db2 "? import" | more
more import_artists.sql
db2 -tvf import_artists.sql
__ On the Control Center, from your MUSICDB database objects list, select
Tables by clicking with your left mouse button.
GUI part 1
__ In the contents pane, scroll down the list of tables until you reach
ARTISTS.
__ Select ARTISTS with your right mouse button and select Import from the
pop-up menu.
6-2
V4.0
Student Exercises
Uempty
6-3
Student Exercises
________________________________________________________________
__ 5. The file you will use to import data into the STOCK table is called
import_stock.sql.
more import_stock.sql
Terminal
db2 -tvf import_stock.sql
From the Command Editor, Commands tab:
__ Select Selected -> Open from the menu bar.
__ Find the file $HOME/lab6/import_stock.sql and select it. Note that
the Path box gets filled in with the import_stock.sql file name.
GUI
__ Click OK.
__ Press Ctrl-Enter to execute the script.
Terminal
GUI
6-4
connect to musicdb;
select * from stock where qty <= 5;
select * from reorder;
V4.0
Student Exercises
Uempty
GUI
6-5
Student Exercises
6-6
V4.0
Student Exercises
Uempty
Terminal
more load_concerts.sql
db2 -tvf load_concerts.sql
In the Control Center, from your MUSICDB database objects list, left-click
and select Tables.
In the right pane, scroll down the list of tables until you reach CONCERTS.
Right-click and select CONCERTS and then select Load from the pop-up
menu. (You might need to refresh the list of tables if you do not see the
CONCERTS table.)
__ On the Type window, indicate Append data to table
__ On the Files window, indicate:
__ Input file format of Integrated Exchange Format (IXF)
GUI
6-7
Student Exercises
Command
Editor
__ 2. Record the number of rows successfully loaded and committed from the messages
file.
Number of Rows Committed:
________________________________________________________________
6-8
V4.0
Student Exercises
Uempty
__ 2. The LOAD command loads rows into the ARTISTS table using a different IXF input
file from the earlier IMPORT. Note that it uses the REPLACE option to replace the
previously imported rows in the ARTISTS table. The artexp table is specified to
contain exception rows for the ARTISTS table during the load with the FOR
EXCEPTION OPTION. What type of violations will be stored in this table during the
load?
________________________________________________________________
________________________________________________________________
__ 3. Record the number of rows committed.
Number of Rows Committed:
____________________________________________________________
__ 4. Check the ARTEXP table to see if any exception rows violated the unique key index
on the ARTISTS table. Use the script file select_exceptions.sql.
____________________________________________________________
____________________________________________________________
more select_exceptions.sql
Terminal
db2 -tvf select_exceptions.sql
On the DB2 Command Editor, Commands tab:
GUI
6-9
Student Exercises
________________________________________________________________
________________________________________________________________
Terminal
GUI
more load_replace_artists.msg
From a DB2 Command Window:
more load_replace_artists.msg
V4.0
Student Exercises
Uempty
__ 2. What SQL code did you get on the first SELECT statement? Check the meaning of
the SQL code using the online help facilities. What action do you need to take to
clear this status?
________________________________________________________________
________________________________________________________________
__ 3. Check the check constraints status of the tables by executing the script
list_table_status.sql and record the status.
Table Name
STATUS
FK_CHECKED
CC_CHECKED
ARTISTS
more list_table_status.sql
Terminal
db2 -tvf list_table_status.sql
On the DB2 Command Editor, Commands tab:
GUI
__ 4. The SET INTEGRITY SQL statement can be used to check for referential constraint
exceptions. Exception tables should be specified for all tables in a Set Integrity
Pending state (in this case, just ARTISTS). Run the script
set_integ_status_artists.sql to check for referential constraint exceptions
in the ARTISTS table.
Copyright IBM Corp. 1999, 2010
6-11
Student Exercises
more set_integ_status_artists.sql
Terminal
db2 -tvf set_integ_status_artists.sql
On the DB2 Command Editor, Commands tab:
GUI
STATUS
FK_CHECKED
CC_CHECKED
ALBUMS
STOCK
Terminal
GUI
__ 7. The SET INTEGRITY SQL statement can be used to check for referential constraint
exceptions. Exception tables should be specified for all tables in a Set Integrity
Pending state (in this case, ALBUMS and STOCK). Run the script
set_integ_status_2.sql to check for referential constraint exceptions in the
ALBUMS and STOCK tables.
more set_integ_status_2.sql
Terminal
db2 -tvf set_integ_status_2.sql
On the DB2 Command Editor, Commands tab:
GUI
V4.0
Student Exercises
Uempty
__ 8. Select from the tables to verify that the Set Integrity Pending status has been
removed from the tables. Run the scripts list_table_status.sql and
select_tables.sql.
Were there any constraint pending conditions?
________________________________________________________________
Terminal
GUI
__ 9. Where are the rows that were moved out of the tables in order to get the table out of
constraint pending? How can you see them?
________________________________________________________________
__ 10. Examine the rows that were moved to your exception tables using the script file
select_exceptions.sql.
Terminal
GUI
__ 11. The rows that were moved from the STOCK table to STOEXP and from the
ALBUMS table to ALBEXP were foreign key rows that did not have a matching
parent key row in the ARTISTS table. You wish to carry this artist's work. Execute
SQL to insert a row for item number 100, title of Patti & Cart Wheels, classification
of S into the ARTISTS table.
A script file is available to insert a row into the ARTSTS table.
Terminal
more insert_artists.sql
db2 -tvf insert_artists.sql
6-13
Student Exercises
GUI
__ 12. Now you wish to use the exception table rows in STOEXP and in ALBEXP to insert
rows into the STOCK and ALBUMS tables, respectively. View the rows that are in
the two exception tables by using the script called select_exceptions.sql.
Note that you do not want to insert the N+1 and N+2 columns into the STOCK and
ALBUMS tables. Examine the MSG column in the two exception tables.
________________________________________________________________
________________________________________________________________
__ 13. Run a script called insert_from_exceptions.sql to insert the rows in the
exception tables into the STOCK and ALBUMS tables. The script also selects from
the ALBUMS and STOCK tables to see if the rows were inserted properly.
________________________________________________________________
more insert_from_exceptions.sql
Terminal
db2 -tvf insert_from_exceptions.sql
On the DB2 Command Editor, Commands tab:
GUI
V4.0
Student Exercises
Uempty
Terminal
GUI
__ Right-click and select STOCK and then select Alter from the pop-up
menu.
__ Select Check Constraints tab.
__ Note the check constraint requirement.
__ Click Cancel.
__ 2. The hot new band, Double Dare, has made a Music Video that they want you to
carry. Connect to the database and attempt to insert stock data for this artist with an
ITEMNO of 302, TYPE of V, PRICE of 100.00, and QTY of 20.
Why did you get the message SQL0545N?
________________________________________________________________
________________________________________________________________
Terminal
GUI
__ 3. Reissue the insert SQL statement with the TYPE = 'C'. You have decided to only
carry the CD type.
________________________________________________________________
________________________________________________________________
6-15
Student Exercises
Terminal
GUI
V4.0
Student Exercises
Uempty
Terminal
GUI
__ 2. The Double Dare CD, ITEMNO 302, entitled I Dare You is selling very fast. Update
the STOCK table to reflect the current inventory of 3. Note that no messages occur
when a trigger is fired.
________________________________________________________________
________________________________________________________________
Terminal
GUI
__ 3. Query the REORDER table to see if the TRIGGER was fired during the previous
update to the QTY in the STOCK table for ITEMNO 302.
________________________________________________________________
________________________________________________________________
Terminal
GUI
__ 4. Previously you created an alias called EMPTYSTOCK for the REORDER table.
Does it give the same results as querying directly against REORDER?
________________________________________________________________
________________________________________________________________
6-17
Student Exercises
Terminal
GUI
V4.0
Student Exercises
Uempty
__ 2. Use the DB2 DESCRIBE tool and compare the results with the DDL file.
Terminal
6-19
Student Exercises
The queen.txt and beatles.txt are the text files where the data for the
ARTISTS.BIO column which is a CLOB(100 KB) is stored.
The queen.tif and beatles.tif are the binary files where the data for the
ARTISTS.PICTURE column which is a BLOB(500 KB) is stored.
A script has been provided for you:
Terminal
more load_lobs.sql
db2 -tvf load_lobs.sql
V4.0
Student Exercises
Uempty
On the DB2 Control Center, from your MUSICDB database objects list,
left-click and select Tables.
__ In the Contents pane, scroll down the list of tables until you reach
ARTISTS.
GUI part 1
__ Right-click and select ARTISTS and then select Load from the pop-up
menu.
__ On the Type window, indicate Append data to table.
__ On the Files window, indicate:
__ Input file format of Delimited Text (DEL)
__ Input file location of Server
GUI Part 2
__ On the Columns window, check the check box to indicate Use these
directories to find large (LOB) object data and enter directory name of:
GUI part 3
$HOME/lab6/lobs
__ On the Schedule window, indicate Run now without saving task history.
GUI part 4 __ On the Summary window, view the command, and click Finish to run the
command.
Command
Editor
__ 2. Look at the message file load_lobs.msg. Near the bottom of the file, note that the
SQL3509W message appears. Note that the number of rows deleted is two. From
your Terminal session, enter the command:
db2 ? SQL3509
__ 3. Did the two new rows get inserted into the ARTISTS table and replace the existing
rows? Why not?
________________________________________________________________
________________________________________________________________
6-21
Student Exercises
more import_lobs.sql
db2 -tvf import_lobs.sql
V4.0
Student Exercises
Uempty
On the DB2 Control Center, from your MUSICDB database objects list,
left-click once and select Tables.
GUI part 1
__ In the Contents pane, scroll down the list of tables until you reach
ARTISTS.
__ Right-click and select ARTISTS and then select Import from the pop-up
menu.
Specify the file to be imported.
__ Enter a name of $HOME/lab6/lob_in in the Import file box.
__ Select Delimited ASCII format radio button.
GUI part 2 __ Select pull-down arrow to the right of the Import mode box.
__ Select INSERT_UPDATE from the list.
__ Enter a file name of $HOME/lab6/import_lobs.msg in the Message
file box.
Specify the LOB information.
__ Select Columns tab.
GUI part 3 __ Select checkbox next to Use these directories to find large (LOB) object
data.
__ Enter a value of $HOME in the LOB paths box.
GUI part 4
__ 7. Check the results by executing the script file select_artists.sql. Are the two
rows with the LOB data there?
________________________________________________________________
________________________________________________________________
Terminal
GUI
6-23
Student Exercises
V4.0
Student Exercises
Uempty
6-25
Student Exercises
V4.0
Student Exercises
Uempty
6-27
Student Exercises
End of exercise
V4.0
Student Exercises
Uempty
Introduction
The database objects in your MUSICDB database have now been
populated with data (through imports, loads, inserts, or updates). Now
you will perform the tasks to enable yourself to re-create your data in
case of loss or destruction of the data.
7-1
Student Exercises
Exercise instructions
Section 0: Setup
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
cd $HOME/lab7
more recovery_setup.cfg
db2 -tvf recovery_setup.cfg
On the Command Editor, Commands tab:
GUI
7-2
V4.0
Student Exercises
Uempty
________________________________________________________________
Terminal
GUI
Control
Center
__ Right-click and select MUSICDB in the left pane and then select
Configure Parameters from the pop-up list.
__ Scroll to the Logs section.
Command
Editor
__ 4. How many primary log files will be allocated? When will these files be allocated?
________________________________________________________________
________________________________________________________________
__ 5. How many secondary log files will be allocated? When will these files be allocated?
________________________________________________________________
________________________________________________________________
__ 6. For which type of logging is your database currently configured (log retention or
circular)? What parameters provide this information?
________________________________________________________________
________________________________________________________________
__ 7. Which types of recovery are supported by this type of logging Crash, Version, or
Roll forward?
________________________________________________________________
__ 8. Where do the log files reside?
________________________________________________________________
__ 9. Assume the database manager was active. A short interruption of power made it
necessary to perform a crash recovery. Will manual intervention be required to
cause this crash recovery? What configuration parameter relates to this issue?
________________________________________________________________
________________________________________________________________
__ 10. From your Terminal session, change your current directory to the location where the
automatic storage data is placed.
7-3
Student Exercises
Note
Do NOT EDIT these files!
cd /dbauto/path1/inst20/NODE0000/MUSICDB
ls -l
__ 11. Determine the contents of this directory.
What are T0000000, T0000001, T0000002, T0000003, T0000007, T0000008,
T00000011 files?
________________________________________________________________
________________________________________________________________
__ 12. Is it appropriate to keep the log files in the same path? (Consider the type of
recovery currently supported in your answer.)
________________________________________________________________
________________________________________________________________
__ 13. Now, list the contents of SQLOGDIR.
cd /database/inst20/NODE0000/SQL00001/SQLOGDIR
ls -l
__ 14. How many log files are in this directory? Does this match what you expected
considering your configuration file? Why or why not?
________________________________________________________________
________________________________________________________________
__ 15. Increase the quantity in the STOCK table by 1 but do not commit the update. If you
are not certain how to turn off auto-commit, check the solutions.
Terminal
7-4
V4.0
Student Exercises
Uempty
__ 16. If you encounter an error, go to the next question. If you did NOT encounter any
error, decrease the quantity in the STOCK table by 1.
________________________________________________________________
Terminal
GUI
__ 17. What did you observe? What was the SQL code you received?
________________________________________________________________
________________________________________________________________
__ 18. Issue a command to retrieve help on the SQL code. Why did the statement fail?
________________________________________________________________
________________________________________________________________
Terminal
GUI
? sql0964
GUI
If you are completing the GUI Remote Administration exercises, reset your
Tools Settings options in the DB2 Command Editor to enable Automatically
commit SQL statement.
7-5
Student Exercises
__ 19. Since the unit of work could not complete successfully, issue a rollback.
Note
This particular problem should not occur in production environments during an
attempt to update only 778 rows. The log file size on your lab databases have
intentionally been set artificially low.
________________________________________________________________
Terminal
db2 rollback
GUI
rollback
__ 20. From your Terminal session, determine the number of log files allocated at this point.
How many are there?
________________________________________________________________
ls -l
__ 21. What accounts for the additional files?
________________________________________________________________
________________________________________________________________
__ 22. From your Terminal session, create a directory that will contain backups of your
database:
mkdir $HOME/restore
Note
In this exercise environment, the directory path you will backup to is
$HOME/restore. Normally, a scheme for isolating backups from the database
supported by the backups would be preferable.
7-6
V4.0
Student Exercises
Uempty
more archive_setup.cfg
db2 -tvf archive_setup.cfg
On the Control Center:
__ Right-click and select MUSICDB in the left pane, and then select
Connect from the pop-up list.
__ Right-click and select MUSICDB in the left pane, and then select
Configure Parameters from the pop-up list.
GUI
__ In the Logs area, find the LOGARCHMETH1 keyword. Click to the right
of the keyword (where it currently says No), and then click the button
with three dots.
__ On the Change Database Configuration Parameter window, change the
value to LOGRETAIN. Click OK.
__ On the Database Configuration window, click OK.
__ Read the message displayed, then select Close.
Command
Editor
db2 terminate
On the Command Editor:
terminate
__ 3. To find out for sure if log retention logging is in effect now, look at the Log retain
status indicator in the db cfg.
Copyright IBM Corp. 1999, 2010
7-7
Student Exercises
Note
One parameter indicates the current setting of LOGRETAIN. However, the value of
ON does not necessarily mean that log retention logging is currently being used.
What is actually being used is indicated by the parameter, Log retain for
recovery status indicator. Be certain to examine both parameters when
investigating the type of logging that is currently used, and the type that will be
used. (In many cases, these parameters will match.)
Terminal
GUI
GUI
GUI
__ 7. Connect to musicdb database. Was the connection successful? What does the SQL
message indicate? Why does the database manager require a new backup?
________________________________________________________________
________________________________________________________________
7-8
V4.0
Student Exercises
________________________________________________________________
Uempty
Terminal
GUI
connect to musicdb
__ 8. Did the attempt to connect to the database cause log retention to be activated?
________________________________________________________________
Terminal
GUI
GUI
__ 10. Complete a series of SQL statements to change data in the ARTISTS table and
record points in time associated with these changes.
__ a. Change the name of ARTNO 77 to "Melanie and the Mechanics".
__ b. Record the time.
__ c. Change the name of ARTNO 77 to "Bill Pellett and the Comets".
__ d. Record the time.
Use values current time, an SQL statement that displays the current time, or
values current time - current timezone, an SQL statement that displays
the current time in UTC time (Universal Time, often designated as GMT), to
determine the approximate time of each change.
Copyright IBM Corp. 1999, 2010
7-9
Student Exercises
Terminal
GUI
Terminal
db2 "update artists set name = 'Bill Pellett and the Comets'
where artno = 77"
db2 "values current time - current timezone"
update artists set name = 'Bill Pellett and the Comets' where
artno = 77;
values current time;
GUI
Record time associated with Bill Pellett and the Comets - __________________
GUI
__ Click Cancel.
-orOn the Command Editor:
get db cfg for muscidb
V4.0
Student Exercises
Uempty
__ 12. Restore your backup.
Terminal
GUI
Terminal
GUI
__ 14. Earlier in this exercise, you attempted to connect to the database when it was in a
backup pending status. You received a message during the connect to indicate this
status. There is another method to determine the state of the database. Examine the
db cfg again. Check the Recovery Status values.
Terminal
GUI
7-11
Student Exercises
__ 16. This state will occur if a restore is done to an online backup or a restore is done to
an offline backup without specifying the WITHOUT ROLLING FORWARD phrase.
In order to clear this flag and apply the log records associated with the Melanie and
the Mechanics update, perform a ROLLFORWARD DATABASE. You want to
rollforward to the timestamp associated with the Melanie and the Mechanics update.
You also want the database removed from the rollforward pending state upon
completion of the command.
________________________________________________________________
Terminal
Ensure you convert the time you recorded to the equivalent UTC time, and
issue the following command:
db2 rollforward db musicdb to yyyy-mm-dd-hh.mm.ss.nnnnnn and
complete (Use 000000 for nnnnnn.)
On the Control Center:
__ Right-click and select MUSICDB in the left pane and then select
Roll-forward from the pop-up list. (If you get a message that the selected
database is not in roll-forward pending, choose to Shutdown DB2 Tools,
and restart the Control Center.)
Specify the parameters to perform a roll forward:
__ Select radio button to the left of Rollforward to a point in time - Local.
__ The date in the Rollforward to transaction box should be accurate.
GUI
__ Change the time associated with that date to match the time you
documented for the Melanie and the Mechanics update. First update
the hour, then cursor to the right and update the minutes, then cursor to
the right and update the seconds.
__ On the Final State window, Select the Complete the roll forward and
return to the active state radio button.
__ Click Finish.
__ Read the DB2 message, then select Close.
__ 17. Check the db cfg again to see if the Rollforward pending status was removed.
Was it?
________________________________________________________________
Terminal
V4.0
Student Exercises
Uempty
GUI
Terminal
GUI
connect to musicdb;
select name from artists where artno = 77;
__ 20. Even though the database manager is configured for roll forward recovery, you are
not required to apply log records.
Restore the database backup and indicate that you do not want to roll forward.
Terminal
GUI
__ 21. Check the pending status of your database by looking at the db cfg.
Is the database in any pending state? Did the value returned to you match what you
expected?
________________________________________________________________
Copyright IBM Corp. 1999, 2010
7-13
Student Exercises
________________________________________________________________
Terminal
GUI
connect to musicdb
get db cfg for musicdb
Terminal
GUI
connect to musicdb;
select name from artists where artno = 77;
Note
You can only specify the phrase without rolling forward if you are restoring from an
offline backup.
V4.0
Student Exercises
Uempty
GUI
__ 2. Examine your db cfg and verify that your database is now configured for circular
logging.
Terminal
GUI
connect to musicdb;
get db cfg for musicdb;
Log retain for recovery status should be equal to NO.
7-15
Student Exercises
V4.0
Student Exercises
Uempty
terminate;
connect to musicdb user inst20 using ibm2blue;
End of exercise
7-17
Student Exercises
V4.0
Student Exercises
Uempty
Introduction
This exercise has three sections that will provide you with the
opportunity to investigate and monitor locking that occurs within your
database. The first section presents tasks necessary to use the
Snapshot Monitor in order to perform basic lock monitoring. The
second section highlights the concept of lock timeouts. The third
section illustrates the concept of deadlocks.
A fourth section is included that will enable you to reset your exercise
environment.
You should complete the exercise sections in the order that they
are presented. If you do not finish the first three sections, be sure
to complete the fourth section to ensure that your database is in
a state that can be used to complete other exercises for the
course.
Note: In this exercise, there are a couple of unique items that you
should pay special attention to:
You will be working with two different userids (inst20 and user20).
Pay attention to which ID you are supposed to be on for each step.
You will be turning off autocommit in CLP by using the '+c' option.
Be careful if you issue a CLP SQL statement without the '+c'
option, work that was being held will be committed.
8-1
Student Exercises
8-2
V4.0
Student Exercises
Uempty
Exercise instructions
Section 0: Setup
You will be opening three (3) Terminal sessions and specifying the user prompt on all 3
Terminals. These three sessions will be used to cause locking contention, and you will
examine the locks taken.
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. Open a Terminal session and set the user prompt to user20.
__ a. Right-click the empty Linux desktop and select Open Terminal. Then execute
the following command:
export PS1='user20 > '
__ 2. Open a second Terminal session and set the user prompt to inst20.
__ a. Right-click the empty Linux desktop and select Open Terminal. Then execute
the following command:
export PS1='inst20 > '
__ 3. Open a third Terminal session and set the user prompt to monitor.
__ a. Right-click the empty Linux desktop and select Open Terminal. Then execute
the following command:
export PS1='monitor > '
__ 4. On all 3 Terminal sessions, change directory to /home/inst20/lab8.
cd /home/inst20/lab8
8-3
Student Exercises
8-4
V4.0
Student Exercises
Uempty
__ 8. From your monitor window, examine the database monitor elements related to
locking by issuing the following:
db2 get snapshot for locks on musicdb > lock1.out
view lock1.out
__ 9. Are any locks currently held?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 10. How many applications are currently connected?
________________________________________________________________
__ 11. What agents are currently holding locks? What applications are associated with
this/these agents?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 12. Is there ever a case where a row lock is possessed without a corresponding lock on
the table which contains the row?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 13. What lock modes are shown for tables under which a row is locked?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 14. How many different tables are being locked by the agent?
________________________________________________________________
________________________________________________________________
Note
You might also see one or more Internal Plan Locks. Any execution of SQL,
whether it is static or dynamic, must be done while holding a Plan (or Package)
lock. This ensures that no one can drop the package that is being executed.
8-5
Student Exercises
8-6
V4.0
Student Exercises
Uempty
8-7
Student Exercises
________________________________________________________________
________________________________________________________________
__ 9. From your monitor window, change the locktimeout parameter to 90 seconds by
issuing the following commands: Also, update the monitor switches to turn lock and
uow on.
db2
db2
db2
db2
db2
terminate
force application all
update db cfg for musicdb using locktimeout 90
update monitor switches using lock on uow on
connect to musicdb
__ 13. Enter the following from the inst20 window where you now have isolation level set to
Repeatable Read.
db2 connect to musicdb user inst20 using ibm2blue
db2 +c "select * from stock where itemno = 200"
__ 14. From your monitor window, obtain a snapshot and state the number of row locks
against the STOCK table that the inst20 application holds.
db2 get snapshot for locks on musicdb > lock2.out
8-8
V4.0
Student Exercises
Uempty
view lock2.out
________________________________________________________________
__ 15. From your user20 window, issue the following:
db2 connect to musicdb user user20 using ibm2blue
db2 +c "update inst20.stock set qty = qty + 1 where itemno = 200 "
__ 16. It appears that user20 is in a lock wait. Investigate this condition by getting another
snapshot from your monitor window.
db2 get snapshot for locks on musicdb > lock3.out
view lock3.out
__ 17. Analyze the portion of the snapshot that is related to the user20 application. Can
you suggest the object that is causing the contention?
________________________________________________________________
________________________________________________________________
________________________________________________________________
Note
You might also see an Internal Variation Lock. Any execution of dynamic SQL is
done via an entry in the SQL cache known as a variation. In order to ensure that
the variation remains valid during the execution, Internal Variation Locks are
acquired on behalf of the entry.
__ 18. In the monitor window, use the new MON_GET_CONNECTION function to view
metrics about the connections:
db2 "SELECT application_handle, rows_returned, tcpip_send_volume
FROM TABLE(MON_GET_CONNECTION(cast(NULL as bigint), -2)) AS t
ORDER BY rows_returned DESC"
__ a. The SQL has been done for you. View the select_mon_get_conn.sql then
execute it:
more select_mon_get_conn.sql
db2 -tvf select_mon_get_conn.sql
APPLICATION_HANDLE
ROWS_RETURNED
TCPIP_SEND_VOLUME
-------------------- -------------------- -------------------282
3
0
295
0
0
275
0
0
3 record(s) selected.
Copyright IBM Corp. 1999, 2010
8-9
Student Exercises
__ 19. If your user20 application is still waiting, sit back and relax for a minute or so.
Eventually, something should happen.
What message is returned to the user20 application and why?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 20. Roll back the unit of work in the inst20 window to release the share locks.
db2 rollback
__ 21. Is it necessary to roll back the update attempted by the user20 application? Why or
why not?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 22. Run the MON_GET_CONNECTION function again and compare the results from
the previous run.
APPLICATION_HANDLE
ROWS_RETURNED
TCPIP_SEND_VOLUME
-------------------- -------------------- -------------------282
3
0
295
3
0
275
0
0
3 record(s) selected.
__ 23. You will need to execute the next three commands sequentially with the 90 second
timeout period.
__ a. In your user20 window, execute the following:
db2 connect to musicdb user user20 using ibm2blue
db2 +c "update inst20.stock set qty = qty + 1 where itemno = 200 "
__ b. In your inst20 window, execute the following:
db2 connect to musicdb user inst20 using ibm2blue
db2 +c "update stock set qty = qty + 1 where itemno = 200 "
__ c. In your monitor window, execute the lock script:
db2 -tvf select_locks.sql >> lock4.out
__ d. When the locktimeout occurs and inst20 returns the timeout error, execute the
script again:
V4.0
Student Exercises
Uempty
8-11
Student Exercises
Section 3: Deadlocks
__ 1. Examine the deadlock check time parameter currently active for your database by
issuing the following from your monitor window.
db2 get db cfg for musicdb | grep -i dlchk
__ 2. What is the value and how much time does this designate?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 3. Issue the following series of SQL statements. Pay particular attention to the
window from which you submit the statements and the order of the
statements. Execute steps a and b, then for Steps c and d below, enter the SQL for
Step c, but do not execute. Then enter the SQL for Step d in its window, but do not
press Enter. Now go back to Step c's window and execute. Then press Enter from
Step d's window. This will insure that the timeout parameter you have set will not
interfere with the intention of the exercise. Remember... we want the isolation
level set to Repeatable Read in our inst20 window.
a. inst20 => db2
b. user20 => db2
= 200"
c. inst20 => db2
d. user20 => db2
= 100"
V4.0
Student Exercises
Uempty
__ 7. What is the status for the application associated with the user20 window? Why is
this the case?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 8. Release the locks held by the inst20 window application.
db2 rollback
__ 9. Highlight the difference between deadlock and timeout by entering the following
series of commands and SQL statements:
a. inst20 => db2 +c "select * from stock where itemno = 100"
b. user20 => db2 +c "update inst20.stock set qty = qty + 1 where
itemno = 200"
c. inst20 => db2 +c "select * from stock where itemno = 200"
__ 10. Wait for the database manager to take some action. What action did the database
manager take? Why is this the case?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 11. Roll back the changes requested by the user20 window.
db2 rollback
__ 12. Make a general statement about the relationship of the configuration parameters
dlchktime and locktimeout.
________________________________________________________________
________________________________________________________________
________________________________________________________________
8-13
Student Exercises
V4.0
Student Exercises
Uempty
End of exercises
8-15
Student Exercises
V4.0
Student Exercises
Uempty
Introduction
This exercise emphasizes the content of the lecture as well as
explores additional areas in greater depth. You will use a variety of
tools to investigate common problems, and affect a resolution to the
problems.
Appendix C contains the Application Performance Tools exercise.
9-1
Student Exercises
Exercise instructions
Section 0: Setup
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
cd $HOME/lab9
more diag_setup.sql
db2 -tvf diag_setup.sql
__ From the DB2 Control Center, expand the All Systems folder until you
get to the inst20 instance.
__ Right-click your instance, and select Configure parameters from the
pop-up menu.
__ Select DIAGLEVEL, and change the parameter from 3 to 4.
__ Click OK.
GUI
__ Right-click your instance, and select Stop from the pop-up menu.
__ Click OK on the Warning window.
__ Acknowledge the DB2 Message.
__ To restart your instance, right-click your instance, and select Start from
the pop-up menu.
__ Acknowledge the DB2 Message.
9-2
V4.0
Student Exercises
Uempty
Terminal
db2stop force
db2start
__ From the DB2 Control Center, expand the All Systems folder until you
get to the inst20 instance.
__ Right-click your instance, and select Stop from the pop-up menu.
__ Click OK on the warning window.
GUI
view $HOME/sqllib/db2dump/db2diag.log
9-3
Student Exercises
__ From the DB2 Control Center, open the Command Editor (either choose
the Tools option on the menu bar and then select Command Editor, or
choose the Command Editor icon on the tool bar (4th from the left)).
__ Choose Selected from the menu bar.
__ Choose Open from the menu bar, and navigate to:
GUI
$HOME/sqllib/db2dump
__ Select db2diag.log.
__ View the db2diag.log information in the Command Editor Command
window.
__ 4. Now lets cause some violation to occur, and view the db2diag.log file to see the
results.
Force all applications off the MUSICDB database.
In a terminal window, change the name of the tsp02 tablespace to tsp02.old.
Delete the db2diag.log file.
Connect to the MUSICDB database.
Can you determine the exact error condition we just imposed?
What is the first error message you see?
What process is having the difficulty?
Is there an SQL error code you can look up?
________________________________________________________________
________________________________________________________________
________________________________________________________________
A script has been provided for you:
more disable_tablespace.cmd
Terminal
./disable_tablespace.cmd
db2 force applications all
view $HOME/sqllib/db2dump/db2diag.log
9-4
V4.0
Student Exercises
Uempty
9-5
Student Exercises
Section 2: Miscellaneous
Some other information gathering tools will be used here, including the LIST
APPLICATIONS command, the db2set command, and the db2pd command.
__ 1. Use the LIST APPLICATIONS command to view the applications using the
MUSICDB database.
Remove all applications from your instance.
Use the LIST APPLICATIONS command to view application in the MUSICDB
database.
Are there any applications connected to MUSICDB?
Connect to the MUSICDB database.
Re-run the LIST APPLICATIONS command. Now are there any applications
connected to the MUSICDB database? If so, How many, and what are they?
________________________________________________________________
________________________________________________________________
________________________________________________________________
cd $HOME/lab9
db2 force applications all
db2 list applications for db musicdb show detail
Terminal
GUI
__ 2. The db2set command is a command line command, issued at the operating system
level. This information will help you when troubleshooting a communication problem.
Execute the db2set command, showing all profile variables that are set.
List the variables found.
9-6
V4.0
Student Exercises
Uempty
________________________________________________________________
________________________________________________________________
________________________________________________________________
db2set -all
__ 3. The db2pd command is a command line command, issued at the operating system
level.
Execute the db2pd command for the MUSICDB database, and put the output in
a file.
For the applications connected to MUSICDB, what kind of transactions are they
performing?
Are there any locks imposed?
How many buffer pools are there? How many buffer pool pages are being used?
How many usable pages are in the tsp01 table space?
What table space has the largest number of used pages?
Have any of the tables been reorganized?
________________________________________________________________
________________________________________________________________
________________________________________________________________
db2 connect to musicdb
db2pd -db musicdb > stats.out
view stats.out
9-7
Student Exercises
more diag_reset.sql
db2 -tvf diag_reset.sql
__ From the DB2 Control Center, expand the All Systems folder until you
get to the inst20 instance.
__ Right-click your instance, and select Configure parameters from the
pop-up menu.
__ Select DIAGLEVEL, and change the parameter from 4 to 3.
__ Click OK.
GUI
__ Right-click your instance, and select Stop from the pop-up menu.
__ Click OK on the warning window.
__ Acknowledge the DB2 Message.
__ To restart your instance, right-click your instance, and select Start from
the pop-up menu.
__ Acknowledge the DB2 Message.
9-8
V4.0
Student Exercises
Uempty
End of exercises
Copyright IBM Corp. 1999, 2010
9-9
Student Exercises
V4.0
Student Exercises
Uempty
Introduction
DB2 has the capability to control access to its data by granting
privileges to authorized individuals and groups of individuals. Blocks of
privileges can also be provided to users through the use of various
authority levels and roles. In addition, authentication plays an integral
part in the overall security scheme of DB2.
In the exercises that follow, you will have the opportunity to investigate
these areas concerning DB2 Security. You will also have several
optional opportunities if you wish to go beyond the objectives of the
exercises.
10-1
Student Exercises
Exercise instructions
Section 0: Setup
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
V4.0
Student Exercises
Uempty
su - user20
__ 7. Connect to the musicdb database as user20.
db2 connect to musicdb user user20 using ibm2blue
__ 8. Recall that you created several table spaces during a prior exercise and you
determined and placed specific tables in those table spaces as part of your design.
Assume that you are user20 and you have just connected. You now want to create a
test table in order to play with some SQL recursive query syntax that you have just
learned.
Issue the following SQL statements to determine the table spaces that have been
defined on the system.
db2 "select substr(tbspace,1,18) as tbspace from syscat.tablespaces
__ 9. Did the SQL statement complete successfully? Why or why not?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 10. As user20, you decide that the TSP04 table space looks as good as any to contain
your test table. Issue the following SQL statement to create your test table.
db2 "create table test1 (partno integer, subpart integer) in TSP04"
Did the statement complete successfully? What does this imply?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 11. As user20, you ask your good friend, the corporate spy, who happens to have
DBADM privilege, to grant you USE privilege on TSP04.
Put on your alter-ego of corporate spy, and on your inst20 session, grant USE on
TSP04 to user20.
db2 connect to musicdb user inst20 using ibm2blue
db2 grant use of tablespace tsp04 to user user20
__ 12. As user20, try creating your test table again. Issue the following SQL statement to
create your test table.
db2 "create table test1 (partno integer, subpart integer) in TSP04"
Did the SQL statement complete successfully? What does this imply?
________________________________________________________________
________________________________________________________________
________________________________________________________________
Copyright IBM Corp. 1999, 2010
10-3
Student Exercises
__ 13. Assume user20 used table test1 for some period of time. You should now assume
the role of the system administrator (the good guy, not the corporate spy). You have
been analyzing a problem concerning the space available for the TSP04 table
space. As part of your analysis, you need to know the table space ID for TSP04.
What is the table space ID for TSP04? Document it below.
________________________________________________________________
________________________________________________________________
Terminal
As inst20:
db2 list tablespaces | more
From the DB2 Control Center, select Views under the MUSICDB database
in the Object tree.
__ Right-click and select TABLESPACES view.
GUI
cd $HOME/lab10
more select_tsp04_tables.sql
db2 -tvf select_tsp04_tables.sql
In the Command Editor, Commands tab.
GUI
__ 15. What do you discover? When you originally estimated the size requirements for
TSP04, did you anticipate this?
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
V4.0
Student Exercises
Uempty
__ 16. As inst20, drop the table USER20.TEST1 to remove it from the database.
Terminal
GUI
__ 17. Why were you able to drop a table in a schema different than your own?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 18. Certainly, you do not want your production system exposed to object creation from
users creating test objects. List the steps you should take in order to begin securing
the production environment.
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 19. Revoke the default database privileges that have been granted to PUBLIC.
Terminal
10-5
Student Exercises
From the DB2 Control Center, select + sign to the left of User and Group
Objects under the MUSICDB database in the Object tree.
__ Select DB Groups under User and Group Objects.
__ Right-click and select PUBLIC group from the Contents pane and then
select Change from the pop-up menu.
GUI
__ 20. From your user20 session, attempt to submit the CREATE TABLE statement again.
db2 "create table test1 (partno integer, subpart integer) in TSP04"
__ 21. Is the user able to create a table?
________________________________________________________________
__ 22. Is user20 still connected to the database? Does this mean the revoke of the
CONNECT authority was not completed?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 23. Assume you had a business reason to revoke connect from a user and then ensure
that any connections established by the user were disconnected. You have already
completed the revoke. Document and complete the remaining steps required.
(Consult the solutions if you are not sure of your process.)
________________________________________________________________
________________________________________________________________
________________________________________________________________
As inst20:
Terminal
V4.0
Student Exercises
Uempty
GUI
__ From the DB2 Control Center, right-click and select inst20 instance in
the Object tree of the Objects pane, and then select Applications from
the pop-up menu.
__ Right-click and select application associated with user20.
__ Click Force.
__ Click Close.
__ 24. From your user20 session, attempt to submit the CREATE TABLE statement again.
db2 "create table test1 (partno integer, subpart integer) in TSP04"
__ 25. The result of the attempt leads you to believe the connection was lost or was
terminated. Submit the following from the user20 session.
db2 connect to musicdb user user20 using ibm2blue
__ 26. Was the connect successful?
________________________________________________________________
________________________________________________________________
__ 27. Exit from the user20 window.
10-7
Student Exercises
__ 2. Determine the members of the adm20 group, by issuing the following command
from your Terminal session.
cat /etc/group | grep adm20
The first column (each column is delimited by : (colon)) is the group ID and the third
column shows the group id of the adm20 group; however, the users whose primary
group is adm20 are not displayed with this command. You need to see the file
/etc/passwd to find the users whose primary group is the group id for adm20.
cat /etc/passwd | grep grpid (from the previous command)
The first column is the user id and the fourth column is the primary group's group ID.
__ 3. What IDs currently act as SYSADM? How would you provide the SYSADM
capability to additional users?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 4. Have you made adm20 a SYSADM? What types of tasks could this user now
complete?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 5. Complete the necessary steps to make adm20 a system administrator.
Do this by executing the following set of commands:
su - root
useradd -g adm20 adm20
10-8 DB2 9 DB Admin Workshop for Linux
V4.0
Student Exercises
Uempty
The first occurrence of adm20 is the group name; the second occurrence of adm20
is the userid that you wish to add to the group.
HP-UX, Look at the primary group ID of adm20.
Linux,
Solaris more /etc/passwd
__ 6. Have you made adm20 a sysadm? What types of tasks could this user now
complete?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 7. Establish a session from which you will connect to the database as adm20.
Open a new Terminal session and switch user to adm20 with a password of
ibm2blue.
su - adm20
__ 8. From the adm20 session, prove that adm20 has the appropriate authority. Recall in
the previous section that the database privileges have been REVOKED from
PUBLIC.
Complete the following:
db2
db2
db2
db2
db2
db2
__ 9. Did any of the commands or SQL statements fail due to authorization errors? Could
this ID even drop some of the inst20's objects? (DO NOT attempt to test this case.)
What is your opinion regarding the number of users that should be placed in the
sysadm group? What does this imply about coordination between database and
operating system security personnel?
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
10-9
Student Exercises
________________________________________________________________
__ 10. Determine your current authorizations for your adm20 ID by issuing:
db2 get authorizations
__ 11. What, if any, item in the list is designated as YES? What does this mean?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 12. From the inst20 session, determine your database authorizations for your inst20
ID.
db2 connect to musicdb
db2 get authorizations
__ 13. Notice that several Direct authorities are designated as YES. Does this mean that
inst20 has greater capability than adm20? For example, can only inst20 create a
not fenced user-defined function?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 14. Since the adm20 ID is a sysadm, it can update the database manager
configuration. From the adm20 session, update the configuration so that the sysctrl
group is set to ctrl20 and the sysmaint group is set to maint20.
db2 update dbm cfg using sysctrl_group ctrl20 sysmaint_group maint20
__ 15. Was adm20 allowed to update the database manager configuration file?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 16. List the database manager configuration file to verify the changes have been made
by issuing the following command:
db2 get dbm cfg | more
__ 17. What enabled you, from your adm20 session, to issue DB2 commands against your
inst20 instance?
________________________________________________________________
__ 18. Make the changes effective by completing the following from your inst20 session.
db2 terminate
10-10 DB2 9 DB Admin Workshop for Linux
V4.0
Student Exercises
Uempty
db2stop force
db2start
__ 19. As adm20, check the current contents of the dbm cfg to see if the update for
SYSCTRL_GROUP and SYSMAINT_GROUP that adm20 did was effective.
db2 attach to inst20 user adm20 using ibm2blue
db2 get dbm cfg show detail | more
Check the sysctrl_group and sysmaint_group parameters.
__ 20. Establish two sessions, one for ctrl20 and the other for maint20.
10-11
Student Exercises
________________________________________________________________
________________________________________________________________
__ 28. From your ctrl20 session, attempt to submit some SQL statements.
db2 "create table test1 (col1 char(1))"
db2 drop table inst20.reorder
db2 "select * from inst20.music"
__ 29. Were any of the statements successful? DO NOT DO ANY OTHER GRANTS AT
THIS TIME! Do you think the same could be said if the same statements were
attempted from the ID possessing sysmaint authority?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 30. Try one more SELECT statement from the sysctrl user of ctrl20.
db2 select name from inst20.artists
__ 31. Why did this SQL statement work? Is the sysctrl authority regarding table access
table specific?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 32. Illustrate the reason the previous SELECT statement was successful, by entering
the following from ctrl20:
db2 "select grantee from syscat.tabauth where tabname = 'ARTISTS'"
__ 33. Issue a db2 terminate from both the ctrl20 and maint20 sessions.
From the ctrl20 session:
db2 terminate
From the maint20 session:
db2 terminate
__ 34. You have decided that you would like to make dba20 a DBADM at your installation.
From your inst20 session, make dba20 a DBADM.
________________________________________________________________
________________________________________________________________
V4.0
Student Exercises
Uempty
On your inst20 session:
Terminal
GUI
__ 35. Check the database authorization catalog view to see the result of your grant.
db2 "select * from syscat.dbauth" | more
Notice it shows dba20 has DBADM authority and that it was granted by inst20.
__ 36. How do you distinguish the internal DBADM grant given to the database creator
from other DBADM grants, since the privilege list is identical?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 37. Establish a Terminal session for dba20 by opening another Terminal session and
switching users.
su - dba20
__ 38. From the dba20 session, connect to database MUSICDB.
db2 connect to musicdb user dba20 using ibm2blue
__ 39. As the new DBADM, attempt to execute the following series of SQL statements.
db2
db2
db2
db2
10-13
Student Exercises
__ 40. Did any of the SQL statements fail? Notice that you have created and dropped
objects in another user's schema. Can any user with the authority to create tables
complete such a task?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 41. The SELECT statement is just a simple example of a Data Manipulation Language
(DML) statement privilege given to DBADM. Can you make a broader statement
concerning DBADM and DML?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 42. In addition to wide-sweeping SQL authority, dbadm has the capability to run certain
utilities against the database, such as REORG and RUNSTATS. However, you do
not need to demonstrate such capabilities at this time.
Recall that sysctrl had the privilege to issue the force command but did not have
the privileges associated with data access. What if your desire was to create an ID
with DBADM authority in addition to SYSCTRL authority - how would you
accomplish the desired result?
V4.0
Student Exercises
Uempty
10-15
Student Exercises
________________________________________________________________
________________________________________________________________
__ 8. Prove (or disprove) your response to the prior question by submitting the following
from the dba20 session. Recall dba20 created table test1.
db2 connect to musicdb user dba20 using ibm2blue
db2 "select * from test1"
db2 drop table test1
__ 9. Recall that maint20 was granted delete authority by dba20 when the ID possessed
the DBADM authority. Is this user still permitted to delete from the INST20.ALBUMS
table? Find out by submitting the following SQL statement from the maint20
session.
db2 delete from inst20.albums where itemno = 311
__ 10. What does the result indicate?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 11. In order to try to be forgiven by inst20, dba20 wants to revoke the grant previously
given to maint20. From the dba20 session, issue:
db2 revoke delete on inst20.albums from maint20
__ 12. Even though dba20 originally did the grant, is this ID able to revoke the privilege?
________________________________________________________________
________________________________________________________________
__ 13. inst20 decides to check on the authorization status of dba20, as well as revoke the
delete privilege from maint20. Do these from the DB2 Control Center as follows:
Check the database privileges:
__ From the DB2 Control Center, select Views under the MUSICDB
database in the Object tree (left-hand panel).
GUI
__ Right-click and select DBAUTH view and then select Open from the
pop-up menu.
__ Note the privileges dba20 still possesses.
__ Click Close button.
V4.0
Student Exercises
Uempty
__ From the DB2 Control Center, select Tables under the MUSICDB
database in the Object tree (left-hand panel). Right-click Tables and
select Refresh.
__ Right-click and select ALBUMS table and then select Privileges from the
pop-up menu.
GUI
Terminal
10-17
Student Exercises
From the DB2 Control Center, select Tables under the MUSICDB database
in the object tree.
__ Right-click and select ALBUMS table and then select Privileges from the
pop-up menu.
__ On the Group tab, click the Add Group button.
GUI
__ 17. The dba20 user is a member of the staff group. You have now given the privilege to
insert into the ALBUMS table to the staff group.
Can all of the users in the staff group insert into the ALBUMS table, or do they
require an additional privilege as well?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 18. Recall that dba20, previously a DBADM, still retains connect privilege. Also, recall
that this user was not able to insert into the ALBUMS table after the DBADM
authority was revoked. Attempt an insert now that the privilege has been granted to
a group of which dba20 is a member. Be sure to issue the SQL statement from the
dba20 session.
db2 insert into inst20.albums values('DB2 Connect is My Life', 99,
310)
You should see that the statement executes successfully.
__ 19. The dba20 ID also retained the privilege to BINDADD. With this in mind, assume
that dba20 attempts to bind a program that contains the insert statement previously
executed as a static statement.
Would such a bind attempt be successful? Why or why not?
________________________________________________________________
________________________________________________________________
________________________________________________________________
V4.0
Student Exercises
Uempty
__ 20. From the DB2 Control Center, determine the current privileges granted against the
ALBUMS table.
From the DB2 Control Center, select Tables under the MUSICDB database in the
Object tree.
__ Right-click Tables and select Refresh.
__ Right-click and select ALBUMS table and then select Privileges from the
pop-up menu.
__ Note the Privileges INST20 has.
__ Select Group tab.
__ Note the Privileges PUBLIC has.
__ Note the Privileges STAFF has.
__ Click Cancel button.
__ 21. Which group ID has the privilege to select from the table? Who belongs to this
group?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 22. Since you have already determined that dba20 can still connect to the database,
determine if the PUBLIC group is functional by issuing the following SQL statement
from the dba20 session.
db2 "select * from inst20.albums"
__ 23. Can you state that the privilege is only present because of the grant to the PUBLIC
group?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 24. Close the dba20 session by entering:
db2 terminate
exit
__ 25. Close the maint20 session by entering:
db2 terminate
exit
__ 26. Close the adm20 session by entering:
db2 terminate
exit
Copyright IBM Corp. 1999, 2010
10-19
Student Exercises
V4.0
Student Exercises
Uempty
connect to musicdb
revoke select on albums from public
grant secadm on database to inst00
grant connect on database to user20
10-21
Student Exercises
V4.0
Student Exercises
Uempty
10-23
Student Exercises
End of exercises
V4.0
Student Exercises
EXempty
vi pgmname.sqc
:wq -or- ZZ
:w
:q!
i
ESC
O
o
u
.
:set nu
Ctrl-b
Ctrl-f
##Ctrl-u
##Ctrl-d
h -or- left arrow
l -or- right arrow
k -or- up arrow
A
^
H
1G
G
Position cursor under character in insert
mode and press enter
Place cursor at end of first line, then press J
R
dw
dd
D
Appendix A. UNIX vi Editor command summary
A-1
Student Exercises
Copy line(s)
Move line(s)
Copy lines from another file and insert
after cursor
Make a copy of the file you are editing
Search DOWN for next occurrence of
text
Search UP for next occurrence of text
Go to a particular line
Delete character at cursor
Copy line to buffer
Pull line from buffer
Change word
A-2
:startline#,endline# co taretline#
:startline#,endline# m taretline#
:r filename
:w filename
/text
?text
:line# G
x
Y
P
cw
V4.0
Student Exercises
Uempty
Introduction
This exercise contains several components to emphasize the content
of the lecture as well as explore additional areas in greater depth. This
exercise will have you performing most of the tasks from the Control
Center.
B-1
Student Exercises
Exercise instructions
Section 1: Access strategies and utilities
This exercise will use the Visual Explain GUI tool on the Linux client. The same content
(but not as easy to read) can be found using the text-based db2exfmt tool.
If not already done, start your Linux VMware image and log in as the inst20 user.
__ 1. If not already done, open a Terminal session.
__ a. Right-click the empty Linux desktop and select Open Terminal.
__ 2. Set up for the exercise by creating the Explain tables. The DDL for creating the
Explain tables can be found in sqllib/misc/EXPLAIN.DDL, but the DB2 provided
DDL will attempt to put the tables into your DMS01 table space which does not have
enough space. A customized version of the file can be found in your home directory
that stores the tables in USERSPACE1; it is named explain.ddl.
Information
Note that the EXPLAIN.DDL file can change from release to release of DB2.
__ 3. Copy the appdxc directory to your HOME directory, then create the explain tables:
cd $HOME/labappdxc
view explain.ddl
db2 connect to musicdb
db2 -tvf explain.ddl
__ 4. View the application file myapp.sqc. Look for the following lines:
strcpy(userid, inst20);
strcpy(password, ibm2blue);
strcpy(dbname, musicdb);
Quit the file view.
__ 5. Populate the Explain tables for the application, myapp.sqc.
db2 prep myapp.sqc explain yes explsnap yes
__ 6. Now, on your Control Center session, ensure that you are connected to the
MUSICDB database.
On your Terminal session, start the Control Center:
db2cc &
__ 7. On the Control Center, open the Show Explained Statements History window.
B-2
V4.0
Student Exercises
Uempty
__ 8. You should see at least one package, the one for MYAPP that you just created
through your Terminal session. Scroll to the right to see all of the columns of
information. This application happens to have just one SQL statement (thus only
one line in the Explained Statements History). What is the statement?
________________________________________________________________
________________________________________________________________
__ 9. When you created the package earlier in this exercise, you were told to use the
options:
explain yes explsnap yes
EXPLAIN YES captures Explain data for use with the db2exfmt tool.
EXPLSNAP YES captures Explain data for use with Visual Explain.
Looking at the output on the Explained Statements History, can you see information
that tells you that Visual Explain could be used for this statement?
________________________________________________________________
__ 10. Look at the Visual Explain output for this statement.
Look at the Visual Explain graph starting at the bottom and moving to the top. What
table is being accessed?
________________________________________________________________
__ 11. How is the ALBUMS table being accessed - by index or table scan?
________________________________________________________________
__ 12. Is a sort required? If so, what is the sort supporting?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 13. Is this access strategy appropriate? (Consider that the only index on the ALBUMS
table is based on the column ITEMNO.)
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 14. Assume you have decided to create an index on the ARTNO column of the
ALBUMS table to support ordering requirements as well as support joins to the
ARTISTS table. (After all, ARTNO on the ALBUMS table is a foreign key.) Create an
index called artno on the artno column in the ALBUMS table.
Terminal
B-3
Student Exercises
In the Objects pane of the DB2 Control Center, right-click and select
Indexes.
__ Select Create > Index from the pop-up menu.
Indicate the following:
__ Index name of artno.
__ Table schema of INST20
GUI
__ 15. Issue a REBIND so that a new access path is considered. Document your command
below. Remember to connect to your database before running the rebind.
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 16. Did the rebind use the original .bnd file on your workstation?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 17. On the Explained Statements History window, refresh the view. Do you see a new
version of the Explain data? How can you tell which is the newest version?
________________________________________________________________
__ 18. Look at the access strategy for the package with Latest Bind = Yes.
Did the access strategy change? Even though you have completed REBIND before
running Explain, can you positively state that the access strategy is appropriate?
________________________________________________________________
________________________________________________________________
________________________________________________________________
B-4
V4.0
Student Exercises
Uempty
__ 19. Since the index on ARTNO was just created, you can state that the statistics for this
index have not been gathered (though, CREATE INDEX has an option to COLLECT
STATISTICS, we did not specify that). A simple way to check if the table statistics
have been updated is to query the SYSSTAT.TABLES view. Issue the SQL
statement from your Terminal session:
db2 "select card from sysstat.tables where tabname = 'ALBUMS' "
__ 20. What value was returned and what does it indicate?
________________________________________________________________
__ 21. Another technique to check on whether statistics have been gathered is to use
REORGCHK. Run the following command from your Terminal session to display the
results of the applied formulas against your tables without executing RUNSTATS
first.
cd
db2 -r rchk1.out reorgchk current statistics on table user
Examine the rchk1.out file. How can you tell from the output that statistics need to
be gathered on all your user tables, as well as their indexes?
more rchk1.out
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 22. Execute RUNSTATS on your ALBUMS table and also request the greatest amount
of statistics possible including those for indexes.
________________________________________________________________
Terminal
GUI
B-5
Student Exercises
__ 23. Execute REORGCHK again from your Terminal session, but only against the
ALBUMS table, to examine the formulas against the statistics you just gathered for
the ALBUMS table.
Although you would also gather statistics on your other tables in a production
environment, this is not necessary for the exercise.
db2 -r rchk2.out reorgchk current statistics on table inst20.albums
more rchk2.out
__ 24. Are any of the formulas out of bounds? If any are out of bounds, does that mean that
you should perform a REORG?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 25. Is the table in any particular clustering sequence?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 26. From your Terminal session, rebind the application, and then look at the new access
plan.
db2 rebind myapp
__ 27. Did the access strategy change? Can you propose a reason why or why not? Is this
access strategy appropriate?
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 28. Assume the size of the ALBUMS table was much greater than it actually is, and also
assume that the table was only clustered according to the index on ITEMNO. What
would you need to do to change the clustering so that the data was in the same
order as the index on ARTNO?
________________________________________________________________
________________________________________________________________
__ 29. Reorganize the ALBUMS table according to the index on ARTNO. You should
specify TEMPSPACE1 as the table space used to support the REORG.
________________________________________________________________
________________________________________________________________
B-6
V4.0
Student Exercises
Uempty
Terminal
GUI
__ 30. What other steps would be necessary if you were attempting to optimize the
statement in the myapp application and you wanted to reflect the result of your
REORG? (You need not actually perform these steps.)
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
__ 31. Close all of your access plan graph windows and the Explained Statements History
window.
__ 32. The key points of this section were NOT to evaluate the actual access strategies
determined against the small lab objects, but to gain understanding regarding the
importance and relationships of RUNSTATS, REORG, and REBIND.
__ 33. If you want to explore further, from the Control Center, you can select the database,
and from the pop-up menu, select Explain SQL... to Explain statements that you
enter dynamically. You can also examine the options on the Access Plan Graph
window. More information on this tool and performance analysis is provided in the
Performance Workshop course.
B-7
Student Exercises
myapp.sqc
End of exercise
B-8
V5.3
backpg
Back page