Db2 v8最好的上手试验教程-来自IBM官方

试验一


DB2 Migration Workshop
LAB 1 – Working with Instances and Databases

Expected duration: 45 mins

This lab will help you solidify concepts of instances and databases. It is to be completed BEFORE the presentation “DB2 Instances and Databases”.

The following commands are demonstrated:


db2icrt

ACTIVATE DB

FORCE APPLICATION

db2idrop

DEACTIVATE DB

GET CONNECTION STATE

db2ilist

CONNECT

LIST APPLICATIONS

db2start

CONNECT RESET

LIST DB DIRECTORY

db2stop

CREATE DATABASE

LIST TABLES

db2admin start

DROP DATABASE



db2admin stop

DESCRIBE TABLE



Prerequisites:

DB2 Version 8 Server has been installed

Highly Recommended: Before each step in this lab, locate the demonstrated command in the CLP quick reference and mark it with an asterix (*). This will help you get familiar with the DB2 CLP cheat sheet.

Note: On Linux/UNIX, you can switch users (su) without logging out by using:

       su –                    .. to switch to root user (dash is important!)
       su –  [userid]        .. to switch to non-root user (dash is important!)
       exit               .. to exit switch back


1.      Windows: Launch the DB2 Command Window.

Start > Programs > IBM DB2 > Command Line Tools > Command Window
(or simply, db2cmd)

Linux: Login to the operating system as user db2inst1

2.      When DB2 is installed, no database exists. Create the SAMPLE database provided by DB2.

db2sampl

3.      Verify that a database now exists. Show the contents of the database directory:

db2 list db directory

Fill in the following information from the database directory:

Database alias        ____ SAMPLE14 __________
Directory entry type _____远程_________

4.      DB2 does not have database users. Instead, DB2 uses userIDs and passwords defined at the operating system (or network authentication facilities) to authenticate users. Connect to the SAMPLE database using the userID logged into the operating system.

db2 connect to sample

5.      See all connections that currently exist for the current instance

db2 list applications

Fill in the following information:

Application name:      ___ ADMINIST _____ ADMINIST ____ ADMINIST
Application handle: ______307_______42______41_____
Database name:      ___ ADMINIST ____ ADMINIST ____ ADMINIST

6.      Disconnect from the database:

db2 connect reset

7.      Connect to the SAMPLE database using a specific userID and password.

db2 connect to sample user   using
db2 connect to sample user db2admin  using xjyc

8.      View the tables that currently exist in the SAMPLE database

db2 list tables
or
db2 list tables for all

9.      From the output of list tables, you should see that a table called EMPLOYEE exists. Find out the structure of EMPLOYEE

db2 describe table employee

What is the maximum length of JOB column? _______________
What is the data type for the Salary Column? _______________

The above two questions are indented to show how data type lengths from the output of DESCRIBE TABLE are interpreted differently for numeric and character types.

10.  Attempt to stop the instance. You should get an error because connections still exist.

db2stop

What is the SQLCODE associated with the error message? _______________

11.  To stop the instance, we have to get rid of the current connections. In this step, forcibly disconnect all applications.

db2 force applications all
db2stop

note: you can also force applications by application handle using:
force application ()

12.  By using FORCE APPLICATION, you have terminated your own connection. Attempt to SELECT from the employee table.

db2 “select * from employee”

You should get the error code SQL1224N. Check your connection state.

db2 get connection state

13.  To solidify the concept of instances and databases, we’ll create a second instance and create a database in it. Create a new instance:

Windows: You need to be a user with administrative privileges

       db2icrt db2inst2 –u db2admin,

Note: There is no space after the comma

Linux: Switch to root user (su - ). You’ll also need to create a user to “own” the instance

       useradd db2inst2
       passwd db2inst2
       /opt/IBM/db2/V8.1/instance/db2icrt –u db2fenc1 db2inst2

14.  Verify that the new instance exists

db2ilist

15.  Your current instance is DB2 (on windows) or db2inst1 (on Linux). Change your context to the new instance, db2inst2.

Windows: set the DB2INSTANCE environment variable to db2inst2

set DB2INSTANCE=db2inst2

Linux: Switch to user db2inst2.

su – db2inst2


16.  Verify that your current instance is db2inst2

db2 get instance

17.  By default, a newly created instance will not be started. Start the new instance

db2start

18.  Create a new database called TEST in the db2inst2 instance

db2 create database TEST

Note: you can also use “db” in place of “database”

Take a look at the database directory again. How many entries do you see?  _______
What happened to the SAMPLE database we created earlier?

____________________________________________________________________

____________________________________________________________________



19.  The default database configuration is not optimal for any significant use. Use DB2’s AUTOCONFIGURE feature to tune the database:

db2 connect to test
db2 autoconfigure using mem_percent 25 workload_type simple tpm 10  is_populated no num_local_apps 10 num_remote_apps 10 apply db and dbm > autoconfig.txt

Use a text editor to review output in autoconfig.txt

Stop and restart the instance for all changes to take effect:
       db2 terminate
       db2stop
       db2start

20.  Once a database created, database global memory (most of which is buffer pool memory) is not allocated until the first connection or an explicit activation. If a database is not explicitly activated, the first connection incurs the full cost of resource allocation for the database and all resources are released when the last application disconnects. To prevent this, for production databases, we recommend that you explicitly activate the database. In this step, practice database activation and deactivation.

Optional: launch windows task manager (or vmstat on Linux) to watch global memory allocation in action.

db2 activate db test
db2 deactivate db test

21.  To clean up, drop the TEST database and stop the instance.

db2 drop database test
db2stop

Note: you can use “db” in place of “database”

22.  Drop the instance

Windows: You must a local administrator user
db2idrop db2inst2

Linux: switch to root user
/opt/IBM/db2/V8.1/instance/db2idrop db2inst2
userdel –r db2inst2

23.  The DB2 Admin server is a background process the facilitates remote database and instance administration and automatic task scheduling. Just for fun, stop and start the admin server.

Linux: You may have to source the db2admin user’s dasprofile file first:

. /home/dasusr1/das/dasprofile

db2admin stop
db2admin start

************************************************************************
以下是答案
************************************************************************
DB2 Migration Workshop
LAB 1 – Working with Instances and Databases (Solution)

Expected duration: 45 mins

This lab will help you solidify concepts of instances and databases. It is to be completed BEFORE the presentation “DB2 Instances and Databases”.

The following commands are demonstrated:


db2icrt

ACTIVATE DB

FORCE APPLICATION

db2idrop

DEACTIVATE DB

GET CONNECTION STATE

db2ilist

CONNECT

LIST APPLICATIONS

db2start

CONNECT RESET

LIST DB DIRECTORY

db2stop

CREATE DATABASE

LIST TABLES

db2admin start

DROP DATABASE



db2admin stop

DESCRIBE TABLE



Prerequisites:

DB2 Version 8 Server has been installed

Highly Recommended: Before each step in this lab, locate the demonstrated command in the CLP quick reference and mark it with an asterix (*). This will help you get familiar with the DB2 CLP cheat sheet.

Note: On Linux/UNIX, you can switch users (su) without logging out by using:

       su –                    .. to switch to root user (dash is important!)
       su –  [userid]        .. to switch to non-root user (dash is important!)
       exit               .. to exit switch back


1.      Windows: Launch the DB2 Command Window.

Start > Programs > IBM DB2 > Command Line Tools > Command Window
(or simply, db2cmd)

Linux: Login to the operating system as user db2inst1

2.      When DB2 is installed, no database exists. Create the SAMPLE database provided by DB2.

db2sampl

3.      Verify that a database now exists. Show the contents of the database directory:

db2 list db directory

Fill in the following information from the database directory:

Database alias        SAMPLE
Directory entry type Indirect

4.      DB2 does not have database users. Instead, DB2 uses userIDs and passwords defined at the operating system (or network authentication facilities) to authenticate users. Connect to the SAMPLE database using the userID logged into the operating system.

db2 connect to sample

5.      See all connections that currently exist for the current instance

db2 list applications

Fill in the following information:

Application name:      db2bp.exe
Application handle:     2 (your answer may vary)
Database name:      SAMPLE

6.      Disconnect from the database:

db2 connect reset

7.      Connect to the SAMPLE database using a specific userID and password.

db2 connect to sample user   using

8.      View the tables that currently exist in the SAMPLE database

db2 list tables
or
db2 list tables for all

9.      From the output of list tables, you should see that a table called EMPLOYEE exists. Find out the structure of EMPLOYEE

db2 describe table employee

What is the maximum length of JOB column?   8 characters
What is the data type for the Salary Column? DECIMAL(9,2)

The above two questions are indented to show how data type lengths from the output of DESCRIBE TABLE are interpreted differently for numeric and character types.

10.  Attempt to stop the instance. You should get an error because connections still exist.

db2stop

What is the SQLCODE associated with the error message? SQL1025N

11.  To stop the instance, we have to get rid of the current connections. In this step, forcibly disconnect all applications.

db2 force applications all
db2stop

note: you can also force applications by application handle using:
force application ()

12.  By using FORCE APPLICATION, you have terminated your own connection. Attempt to SELECT from the employee table.

db2 “select * from employee”

You should get the error code SQL1224N. Check your connection state.

db2 get connection state

13.  To solidify the concept of instances and databases, we’ll create a second instance and create a database in it. Create a new instance:

Windows: You need to be a user with administrative privileges

       db2icrt db2inst2 –u db2admin,

Note: There is no space after the comma

Linux: Switch to root user (su - ). You’ll also need to create a user to “own” the instance

       useradd db2inst2
       passwd db2inst2
       /opt/IBM/db2/V8.1/instance/db2icrt –u db2fenc1 db2inst2

14.  Verify that the new instance exists

db2ilist

15.  Your current instance is DB2 (on windows) or db2inst1 (on Linux). Change your context to the new instance.

Windows: set the DB2INSTANCE environment variable to db2inst2

set DB2INSTANCE=db2inst2

Linux: Switch to user db2inst2.

su – db2inst2


16.  Verify that your current instance is db2inst2

db2 get instance

17.  By default, a newly created instance will not be started. Start the new instance

db2start

18.  The default database configuration is not optimal for any significant use. Use DB2’s AUTOCONFIGURE feature to tune the database:

db2 connect to test
db2 autoconfigure using mem_percent 25 workload_type simple tpm 10  is_populated no num_local_apps 10 num_remote_apps 10 apply db and dbm > autoconfig.txt

Use a text editor to review output in autoconfig.txt

Stop and restart the instance for all changes to take effect:
       db2 terminate
       db2stop
       db2start

19.  Create a new database called TEST in the db2inst2 instance

db2 create database TEST

Note: you can also use “db” in place of “database”

Take a look at the database directory again. How many entries do you see?  one
What happened to the SAMPLE database we created earlier?

After creating a second instance and switching to it in step 16, we changed to a totally distinct database server context. The SAMPLE database exists in the first instance only. Therefore, after creating the TEST database, there still only the TEST database is listed in the database directory


20.  Once a database created, database global memory (most of which is buffer pool memory) is not allocated until the first connection or an explicit activation. If a database is not explicitly activated, the first connection incurs the full cost of resource allocation for the database and all resources are released when the last application disconnects. To prevent this, for production databases, we recommend that you explicitly activate the database. In this step, practice database activation and deactivation.

Optional: launch windows task manager (or vmstat on Linux) to watch global memory allocation in action.

db2 activate db test
db2 deactivate db test

21.  To clean up, drop the TEST database and stop the instance.

db2 drop database test
db2stop

Note: you can use “db” in place of “database”

22.  Drop the instance

Windows: You must a local administrator user
db2idrop db2inst2

Linux: switch to root user
/opt/IBM/db2/V8.1/instance/db2idrop db2inst2
userdel –r db2inst2

23.  The DB2 Admin server is a background process the facilitates remote database and instance administration and automatic task scheduling. Just for fun, stop and start the admin server.

Linux: You may have to source the db2admin user’s dasprofile file first:

. /home/dasusr1/das/dasprofile

db2admin stop
db2admin start


DB2 Migration Workshop
LAB 2 – Working with buffer pools and tablespaces

Expected duration: 45 mins

This lab will help you solidify concepts of buffer pools and tablespaces. It is to be completed following the presentation “DB2 Bufferpools and tablespaces”.

The following commands are demonstrated:

ALTER TABLESPACE        ALTER BUFFERPOOL
CREATE TABLESPACE        CREATE BUFFERPOOL
LIST TABLESPACES        DROP BUFFERPOOL
LIST TABLESPACE CONTAINERS       
DROP TABLESPACE        CREATE TABLE … IN

Prerequisites:
·        DB2 Version 8 Server has been installed
·        Completion of Lab1 (requires sample database created)

Highly Recommended: Before each step in this lab, locate the demonstrated command in the CLP quick reference and mark it with an asterix (*). This will help you get familiar with the DB2 CLP cheat sheet.

1.        Connect to the sample database and view the tablespaces that currently exist

db2 list tablespaces show detail

From the output of the above command, fill in the following chart

TS ID        TS Name        TS Type        TS Page Size
                       
                       
                       
                       
                       


2.        Now you’ll create your own table space which uses an 8K page size for data, and a separate 4K page size tablespace for indexes. Before an 8K tablespace can be created, however, an 8K buffer pool must first exist. Create an 8K buffer pool MYBP8K which uses 1000 pages (8MB).

db2 create bufferpool MYBP8K  size 1000 pagesize 8K

Next, create a tablespace called MYTS8K which uses the 8K bufferpool.

db2 create tablespace MYTS8K pagesize 8K /
managed by database using (                /
FILE ‘c:/myts8k.0’ 1000,                /
FILE ‘c:/myts8k.1’ 1000)                 /
extentsize 8                /
prefetchsize 16        /
bufferpool MYBP8K

What type of table space was just created? (DMS or SMS)        __________________
How many containers are in this table space?                        __________________
What is the purpose of using backslash ?                                __________________

How many kilobytes of data will be written to myts8k.0 before data is written to myts8k.1 (note: calculation required)?  Why?

____________________________________________________________________

____________________________________________________________________

In order to store indexes separately from data, DMS tablespaces are required. From step 1, you should have identified that all tablespaces in SAMPLE are SMS. Therefore, we need to create a 4K DMS tablespace to store index data.

db2 create tablespace MYTS4K_IDX  pagesize 4K /
managed by database using (FILE ‘c:/myts4k_idx.0’ 1000)        /
extentsize 8        /
prefetchsize 8        /
bufferpool IBMDEFAULTBP

Which buffer pool is being used by MYTS4K_IDX?                __________________
How many kilobytes of data per extent in this tablespace?        __________________

3.        Take a look at the bufferpools that currently exist (and their sizes)

db2 “select * from syscat.bufferpools”

4.        See all the tablespaces just created by repeating step 1. Complete the table by adding the additional information for the tablespaces just created. Show the containers for table space MYTS8K and MYTS4K_IDX

db2 list tablespace containers for

(substitute with the table space ID of MYTS8K and MYTS4K_IDX)

5.        How many data pages are free and used in MYTS8K and MYTS4K_IDX?

db2 list tablespaces show detail

Fill in the following information for MYTS8K

Used pages:        __________________
Free pages:        __________________
High watermark:        __________________

Fill in the following information for MYTS4K_IDX

Used pages:        __________________
Free pages:        __________________
High watermark:        __________________

6.        Create a table with the following DDL:

db2 create table T1          /
(id bigint not null primary key, /
data char(100),        /
insert_ts timestamp)         /
        IN MYTS8K                /
        INDEX IN MYTS4K_IDX

If you look at the output of list tablespaces show detail, you’ll see that additional pages have been used in both MYTS8K and MYTS4K_IDX. We expect that pages will be allocated in the data tablespace (overhead data pages for a table). But why are additional pages allocated MYTS4K_IDX?

____________________________________________________________________

____________________________________________________________________


7.        The bufferpool sized at 1000 pages is too small for any real use. In version 8, bufferpools, in most cases, can be dynamically changed (you will be given a warning if it is not dynamic) using ALTER BUFFERPOOL

Tip: You can use OS monitoring tools to see the memory use increase.

db2 alter bufferpool ibmdefaultbp size 5000
db2 alter bufferpool mybp8k size 5000

What are the new sizes of each bufferpool, in megabytes?

IBMDEFAULTBP        _________________
MYBP8K                        _________________

8.        Tablespace characteristics can also be altered using ALTER TABLESPACE. In this step, increase the aggressiveness of index prefetching

db2 alter tablespace MYTS4K_IDX prefetchsize 16

9.        Launch the DB2 Control Center to visually identify the objects you just created
·        Table
·        Index
·        Tablespaces
·        Tablespace Containers
·        Bufferpools

10.        Clean up objects created by this lab exercise:

db2 drop table T1
db2 drop tablespace MYTS8K
db2 drop tablespace MYTS4K_IDX
db2 drop bufferpool MYBP8K


××××××××××××××××××××××××××××××××××××××××××××××××
答案
×××××××××××××××××××××××××××××××××××××××××××××
DB2 Migration Workshop
LAB 2 – Working with buffer pools and tablespaces

Expected duration: 45 mins

This lab will help you solidify concepts of buffer pools and tablespaces. It is to be completed following the presentation “DB2 Bufferpools and tablespaces”.

The following commands are demonstrated:

ALTER TABLESPACE        ALTER BUFFERPOOL
CREATE TABLESPACE        CREATE BUFFERPOOL
LIST TABLESPACES        DROP BUFFERPOOL
LIST TABLESPACE CONTAINERS       
DROP TABLESPACE        CREATE TABLE … IN

Prerequisites:
·        DB2 Version 8 Server has been installed
·        Completion of Lab1 (requires sample database created)

Highly Recommended: Before each step in this lab, locate the demonstrated command in the CLP quick reference and mark it with an asterix (*). This will help you get familiar with the DB2 CLP cheat sheet.

1.        Connect to the sample database and view the tablespaces that currently exist

db2 list tablespaces show detail

From the output of the above command, fill in the following chart

TS ID        TS Name        TS Type        TS Page Size
0        SYSCATSPACE        SMS        4096 (4K)
1        TEMPSPACE1        SMS        4096 (4K)
2        USERSPACE1        SMS        4096 (4K)
                       
                       


2.        Now you’ll create your own table space which uses an 8K page size for data, and a separate 4K page size tablespace for indexes. Before an 8K tablespace can be created, however, an 8K buffer pool must first exist. Create an 8K buffer pool MYBP8K which uses 1000 pages (8MB).

db2 create bufferpool MYBP8K  size 1000 pagesize 8K

Next, create a tablespace called MYTS8K which uses the 8K bufferpool.

db2 create tablespace MYTS8K pagesize 8K /
managed by database using (                /
FILE ‘c:/myts8k.0’ 1000,                /
FILE ‘c:/myts8k.1’ 1000)                 /
extentsize 8                /
prefetchsize 16        /
bufferpool MYBP8K

What type of table space was just created? (DMS or SMS)        DMS
How many containers are in this table space?                        two
What is the purpose of using backslash ?                                to continue the statement on a separate line

How many kilobytes of data will be written to myts8k.0 before data is written to myts8k.1 (note: calculation required)?  Why?

DB2 writes one full extent (per table) to one container before writing to the next. 64K of data will be written to container 0 before writing to container 1. The extent size in this case is eight 8K pages (and 8x8=64).

In order to store indexes separately from data, DMS tablespaces are required. From step 1, you should have identified that all tablespaces in SAMPLE are SMS. Therefore, we need to create a 4K DMS tablespace to store index data.

db2 create tablespace MYTS4K_IDX  pagesize 4K /
managed by database using (FILE ‘c:/myts4k_idx.0’ 1000)        /
extentsize 8        /
prefetchsize 8        /
bufferpool IBMDEFAULTBP

Which buffer pool is being used by MYTS4K_IDX?                IBMDEFAULTBP
How many kilobytes of data per extent in this tablespace?        8 x 4K = 32K

3.        Take a look at the bufferpools that currently exist (and their sizes)

db2 “select * from syscat.bufferpools”

4.        See all the tablespaces just created by repeating step 1. Complete the table by adding the additional information for the tablespaces just created. Show the containers for table space MYTS8K and MYTS4K_IDX

db2 list tablespace containers for

(substitute with the table space ID of MYTS8K and MYTS4K_IDX)

5.        How many data pages are free and used in MYTS8K and MYTS4K_IDX?

db2 list tablespaces show detail

Fill in the following information for MYTS8K

Used pages:        24
Free pages:        1960
High watermark:        24

Fill in the following information for MYTS4K_IDX

Used pages:        24
Free pages:        968
High watermark:        24

6.        Create a table with the following DDL:

db2 create table T1          /
(id bigint not null primary key, /
data char(100),        /
insert_ts timestamp)         /
        IN MYTS8K                /
        INDEX IN MYTS4K_IDX

If you look at the output of list tablespaces show detail, you’ll see that additional pages have been used in both MYTS8K and MYTS4K_IDX. We expect that pages will be allocated in the data tablespace (overhead data pages for a table). But why are additional pages allocated MYTS4K_IDX?

To enforce the primary key on column id, DB2 will create a unique index for it. The index will be stored in MYTS4K_IDX as defined by the table DDL.


7.        The bufferpool sized at 1000 pages is too small for any real use. In version 8, bufferpools, in most cases, can be dynamically changed (you will be given a warning if it is not dynamic) using ALTER BUFFERPOOL

Tip: You can use OS monitoring tools to see the memory use increase.

db2 alter bufferpool ibmdefaultbp size 5000
db2 alter bufferpool mybp8k size 5000

What are the new sizes of each bufferpool, in megabytes?

IBMDEFAULTBP        20MB
MYBP8K                        40MB

8.        Tablespace characteristics can also be altered using ALTER TABLESPACE. In this step, increase the aggressiveness of index prefetching

db2 alter tablespace MYTS4K_IDX prefetchsize 16

9.        Launch the DB2 Control Center to visually identify the objects you just created
·        Table
·        Index
·        Tablespaces
·        Tablespace Containers
·        Bufferpools

10.        Clean up objects created by this lab exercise:

db2 drop table T1
db2 drop tablespace MYTS8K
db2 drop tablespace MYTS4K_IDX
db2 drop bufferpool MYBP8K


DB2 Migration Workshop
LAB 3 – Working with database objects

Estimated duration: 45 mins

This lab will help you solidify basic concepts of DB2 database objects. It is to be completed following the presentation “DB2 database objects”.

The following commands and concepts are demonstrated:

ALTER TABLE        CREATE SEQUENCE
CREATE TABLE        REORG TABLE
SELECT …. FETCH FIRST n ROWS ONLY        LIST TABLESPACES SHOW DETAIL
CREATE TABLESPACE        db2 –td! –f

你可能感兴趣的:(DB2)