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 <ts>

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 <n>

(substitute <n> 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 <ts>

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 <n>

(substitute <n> 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 <script>
CURRENT DATE        CHAR()
CURRENT TIME        TIMESTAMP()
CURRENT TIMESTAMP        DATE()
SET SCHEMA        USER
CURRENT SCHEMA       

Prerequisites:
·        DB2 Version 8 Server for Windows (any edition) has been installed
·        Completion of Lab2

Part 1: Database Objects

1.        For this lab, you will need to increase the DB2’s log file size. Record your current log file parameters:

db2 get db cfg for sample

Log file size (4K)                (LOGFILSIZ)                _____________
Number of Primary Logs         (LOGPRIMARY)        _____________
Number of Secondary Logs        (LOGSECOND)        _____________

2.        To increase the log file size in DB2, increase the parameter LOGFILSIZ, disconnect all applications (and ensure the database is deactivated).

db2 update db cfg for sample using logfilsiz 1000
db2 force application all
db2 deactivate db sample

There should be a slight delay for first connection (in the next step) because DB2 needs time to pre-allocate the space needed for the new, larger, primary logs.
       
3.        Connect to the sample database and create a new tablespace which uses a 4K page size.

db2 create tablespace MYTS4K pagesize 4K /
managed by database using (FILE 'c:/myts4k.0' 4000)  /
Bufferpool IBMDEFAULTBP

4.        Create a table in table space MYTS4K with the following DDL:

db2 create table T1          /
(id bigint,                 /
data char(100),        /
insert_ts timestamp)         /
        IN MYTS4K

5.        In the next few steps, you will be populating the table with data. To support this, a sequence called MYSEQ needs to be created to generate unique numbers. (FYI, DB2 supports identity columns as well)

db2 create sequence MYSEQ


6.        Using a standard text editor, create a file called T1data.db2 with the following text:

begin atomic
    declare cnt INT default 0;

    while (cnt < 20000) do
        insert into t1 values (
            nextval for MYSEQ,
            (select case
                when (rand() < 0.5) then null
                else space(int(rand()*100))
            end case from sysibm.sysdummy1),
            current timestamp);
        set cnt=cnt+1;
    end while;

end!

How do you get an incremental number from a sequence ?         __________________
What is the end-of-statement delimiter for this script?                __________________
How many rows will be inserted?                                        __________________

7.        Describe What the script does

____________________________________________________________________

____________________________________________________________________

____________________________________________________________________

8.        Execute the DB2 script T1data.db2 to populate table T1 using the following command:

db2 –td! –f T1data.db2

What is the significance of the following flags?

-td                __________________________________________________
-f                __________________________________________________

Why is it necessary to specify a delimiter (!) explicitly to run this script?

_________________________________________________________

9.        Take a look at some of the data just inserted. Since selecting all the data is not necessary, limit the number of rows returned:

db2 select * from t1 fetch first 10 rows only

10.        What do you notice about the timestamp data just inserted?

______________________________________________________________________

______________________________________________________________________

11.        How many data pages are being used to contain the data in table T1?
db2 list tablespaces show detail

Fill in the following information for MYTS4K

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

12.        Table T1 is sparely populated and therefore, would benefit from table compression. Enable T1 for value compression.

db2 alter table t1 activate value compression
db2 alter table t1 alter column id compress system default
db2 alter table t1 alter column data compress system default

13.        In order to realize the space savings for rows inserted before compression was enabled, the existing table data must be reorganized. REORG table T1:

db2 reorg table t1

14.        How much space has been saved?

db2 list tablespaces show detail

Fill in the following information for MYTS4K

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

15.        Clean up objects created by this lab exercise:

db2 drop table T1
db2 drop sequence MYSEQ restrict
db2 drop tablespace MYTS4K

Part 2: DB2 SQL Practice

This part is intended to help you get familiar with DB2 SQL usage.

1.        Connect to the database, and practice retrieving DB2 register values

db2 values CURRENT TIMESTAMP
db2 select current timestamp from sysibm.sysdummy1

2.        Do the same for these register values:

CURRENT DATE
CURRENT TIME
CURRENT SCHEMA
CURRENT FUNCTION PATH
USER

3.        Practice casting date to strings and strings to date types:

CHAR(CURRENT DATE)
CHAR(CURRENT TIMESTAMP)
DATE (‘01/01/2003’)
TIMESTAMP (‘2003-01-01-12.30.00.000000’)
TIMESTAMP (‘2003-01-01-12.30.00’)

4.        Change your schema, and create a table:

db2 SET SCHEMA db2rocks
db2 values current schema
db2 CREATE TABLE t1 (TS timestamp, dt date, tm time)

What is the full name of the table just created?        __________________

5.        Insert some values into table T1

db2 insert into t1 values (current timestamp, current date, current time)
db2 insert into t1 values (current timestamp + 1 day,  current date - 1 day,  
current time + 1 hour)

6.        Clean up

db2 drop table t1

×××××××××××××××××××××××××××××××××××××××
答案
××××××××××××××××××××××××××××××××××××××
DB2 Migration Workshop
LAB 3 – Working with database objects

Estimated duration: 30 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 <script>
CURRENT DATE        CHAR()
CURRENT TIME        TIMESTAMP()
CURRENT TIMESTAMP        DATE()
SET SCHEMA        USER
CURRENT SCHEMA       

Prerequisites:
·        DB2 Version 8 Server for Windows (any edition) has been installed
·        Completion of Lab2

Part 1: Database Objects

1.        For this lab, you will need to increase the DB2’s log file size. Record your current log file parameters:

db2 get db cfg for sample

Log file size (4K)                (LOGFILSIZ)                250
Number of Primary Logs         (LOGPRIMARY)        3
Number of Secondary Logs        (LOGSECOND)        2

2.        To increase the log file size in DB2, increase the parameter LOGFILSIZ, disconnect all applications (and ensure the database is deactivated).

db2 update db cfg for sample using logfilsiz 1000
db2 force application all
db2 deactivate db sample

3.        Connect to the sample database and create a new tablespace which uses a 4K page size.

db2 create tablespace MYTS4K pagesize 4K /
managed by database using (FILE 'c:/myts4k.0' 4000)  /
Bufferpool IBMDEFAULTBP

4.        Create a table in table space MYTS4K with the following DDL:

db2 create table T1          /
(id bigint,                 /
data char(100),        /
insert_ts timestamp)         /
        IN MYTS4K

5.        In the next few steps, you will be populating the table with data. To support this, a sequence called MYSEQ needs to be created to generate unique numbers. (FYI, DB2 supports identity columns as well)

db2 create sequence MYSEQ

6.        Using a standard text editor, create a file called T1data.db2 with the following text:

begin atomic
    declare cnt INT default 0;

    while (cnt < 20000) do
        insert into t1 values (
            nextval for MYSEQ,
            (select case
                when (rand() < 0.5) then null
                else space(int(rand()*100))
            end case from sysibm.sysdummy1),
            current timestamp);
        set cnt=cnt+1;
    end while;

end!

How do you get an incremental number from a sequence ?         nextval for myseq
What is the end-of-statement delimiter for this script?                ! character
How many rows will be inserted?                                        20000


7.        Describe What the script does

The script uses SQL PL to populate the T1 table created in step 2. There is a loop which repeats 20,000 times, inserting a unique id value using a sequence object, some blank data (or random length and about 50% of the time when data is inserted) and a timestamp value for the row.

8.        Execute the DB2 script T1data.db2 to populate table T1 using the following command:

db2 –td! –f T1data.db2

What is the significance of the following flags?

-td                Set statement termination character
-f                Input filename

Why is it necessary to specify a delimiter (!) explicitly to run this script?

If a dynamic compound SQL statement is used (BEGIN ATOMIC… END), statements within the compound statement already use the semicolon (;) as a statement delimiter. Overriding the default delimiters is required so that DB2 can differentiate where statements truly begin and end.

9.        Take a look at some of the data just inserted. Since selecting all the data is not necessary, limit the number of rows returned:

db2 select * from t1 fetch first 10 rows only

10.        What do you notice about the timestamp data just inserted?

The timestamp value inserted is all the same. This is because the values were generated within a BEGIN ATOMIC SQL block.

11.        How many data pages are being used to contain the data in table T1?
db2 list tablespaces show detail

Fill in the following information for MYTS4K

Used pages:        800
Free pages:        3168
High watermark:        800

12.        Table T1 is sparely populated and therefore, would benefit from table compression. Enable T1 for value compression.

db2 alter table t1 activate value compression
db2 alter table t1 alter column id compress system default
db2 alter table t1 alter column data compress system default

13.        In order to realize the space savings for rows inserted before compression was enabled, the existing table data must be reorganized. REORG table T1:

db2 reorg table t1

14.        How much space has been saved?

db2 list tablespaces show detail

Fill in the following information for MYTS4K

Used pages:        320
Free pages:        3648
High watermark:        1024

15.        Clean up objects created by this lab exercise:

db2 drop table T1
db2 drop sequence MYSEQ restrict
db2 drop tablespace MYTS4K

Part 2: DB2 SQL Practice

This part is intended to help you get familiar with DB2 SQL usage.

1.        Connect to the database, and practice retrieving DB2 register values

db2 values CURRENT TIMESTAMP
db2 select current timestamp from sysibm.sysdummy1

2.        Do the same for these register values:

CURRENT DATE
CURRENT TIME
CURRENT SCHEMA
CURRENT FUNCTION PATH
USER

3.        Practice casting date to strings and strings to date types:

CHAR(CURRENT DATE)
CHAR(CURRENT TIMESTAMP)
DATE (‘01/01/2003’)
TIMESTAMP (‘2003-01-01-12.30.00.000000’)
TIMESTAMP (‘2003-01-01-12.30.00’)

4.        Change your schema, and create a table:

db2 SET SCHEMA db2rocks
db2 values current schema
db2 CREATE TABLE t1 (TS timestamp, dt date, tm time)

What is the full name of the table just created?        __________________

5.        Insert some values into table T1

db2 insert into t1 values (current timestamp, current date, current time)
db2 insert into t1 values (current timestamp + 1 day,  current date - 1 day,  
current time + 1 hour)

6.        Clean up

db2 drop table t1


DB2 Migration Workshop
LAB 4 – Client Connectivity

Estimated duration: 30 mins

This lab will help you solidify basic concepts of DB2 client connectivity. It is to be completed following the presentation “DB2 Client Connectivity”.

The following commands and concepts are demonstrated:

CATALOG … NODE        LIST NODE DIRECTORY
CATALOG ADMIN … NODE        LIST ADMIN NODE DIRECTORY
CATALOG DATABASE        LIST DATABASE DIRECTORY
UNCATALOG NODE        ATTACH

Prerequisites:
·        DB2 Version 8 Server for Windows (any edition) has been installed
·        Sample database has been created
·        Remote DB2 Server is running

1.        Issue the following command:

db2 catalog tcpip node nodeX remote 192.168.9.99 server 54321

Identify the following information from the above command:
Local alias for remote instance         _______________
        Remote hostname/machine IP :        _______________
        Remote instance’s protocol:                _______________
        Remote instance’s port number:        _______________

View the node entry created:

db2 list node directory

Key Lesson: Network connections are considered unreliable. Therefore, DB2 allows you to catalog nodes that do not immediately exist
       
2.        In the previous step, we purposely cataloged a node that does not exist to demonstrate a VERY common error encountered by new DB2 users.  What is the error code message when you attempt the following command

db2 attach to nodeX user <userid> using <password>

SQLCODE:        ____________________

Error Message:       

___________________________________________________

___________________________________________________

Key Lesson: If you see this error, you’ve probably cataloged the remote NODE wrong, or the server has not been set up properly for remote client access.

What is the difference between ATTACH and CONNECT?

______________________________________________________

______________________________________________________

______________________________________________________

3.        Remove the invalid node entry:

db2 uncatalog node nodeX

4.        Verify that the requirements for remote connectivity for YOUR database server are met (since others may connect to your machine in the next few steps)

#1) Has the DB2COMM registry variable been set to TCPIP?
        db2set –all

If not, set it
        db2set DB2COMM=tcpip

#2) Has the service name parameter (SVCENAME) in the DBM CFG been set?
        db2 get dbm cfg

If not, set it
        db2 update dbm cfg using svcename 50000

If you had to make any configuration settings, you will need to restart the instance before continuing.

5.        As someone who wants to establish a connection to a remote database server, you will need to ask your neighbor (or instructor) for the following information:

(In most cases, you will simply catalog your neighbor’s SAMPLE database, but feel free to experiment with different configurations.)

Remote Database Info:
        (PR)        Protocol                        ____________
        (IP)         IP Address or hostname        ____________
        (PN)        Instance Port Number                ____________
        (DB)        Database Name                ____________


6.        Setup your client to connect remotely issuing the following commands:

db2 catalog admin PR  node <admin_node_alias> remote IP
db2 catalog PR node <node_alias> remote IP server PN
db2 catalog database DB as <db_alias> at node <node_alias>

A port number is not necessary for the admin node. What port number does it always assume?        __________

If a remote machine has two instances, each with their own database:
a)        How many node directory entries are required at the client?         ________
b)        How many admin node entries are required at the client?        ________

7.        Verify your client configuration:

db2 list admin node directory
db2 list node directory
db2 list db directory

8.        Verify that you can attach to the remote node and connect to the remote database

db2 attach to <node_alias> user <userid> using <password>
db2 connect to <db_alias> user <userid> using <password>

Why is the userid and password required (by default) for ATTACH and CONNECT for remote instances and remote databases?

__________________________________________________________________


9.        On AIX, to work around a 32-bit architecture limitation, a LOOPBACK node is sometimes needed to support a larger number of local database connections. Perform the following as an exercise, even if you are not using AIX at this time:

db2 catalog admin tcpip node loopadm remote 127.0.0.1
db2 catalog tcpip node loopback remote 127.0.0.1 server 50000
db2 catalog database sample as loopsamp at node loopback

db2 connect to loopsamp user <userid> using  <password>

Why is a userid and password required (by default) when a database uses a loopback node?

____________________________________________________________________

____________________________________________________________________

10.        Clean up your client node and database directories:
db2 uncatalog node loopadm
db2 uncatalog node loopback
db2 uncatalog db loopsamp

db2 uncatalog db <remote_db>
db2 uncatalog node <remote_node>
db2 uncatalog node <remote_admin_node>

11.        If you have free time, practice setting up remote connectivity by connecting to another remote database. You may also want to try using the DB2 Client Configuration Assistant to perform client configuration.

×××××××××××××××××××××××××××××××××××××××××
答案
××××××××××××××××××××××××××××××××××××××××××
DB2 Migration Workshop
LAB 4 – Client Connectivity

Estimated duration: 30 mins

This lab will help you solidify basic concepts of DB2 client connectivity. It is to be completed following the presentation “DB2 Client Connectivity”.

The following commands and concepts are demonstrated:

CATALOG … NODE        LIST NODE DIRECTORY
CATALOG ADMIN … NODE        LIST ADMIN NODE DIRECTORY
CATALOG DATABASE        LIST DATABASE DIRECTORY
UNCATALOG NODE        ATTACH

Prerequisites:
·        DB2 Version 8 Server for Windows (any edition) has been installed
·        Sample database has been created
·        Remote DB2 Server is running

1.        Issue the following command:

db2 catalog tcpip node nodeX remote 192.168.9.99 server 54321

Identify the following information from the above command:
Local alias for remote instance         _______________
        Remote hostname/machine IP :        _______________
        Remote instance’s protocol:                _______________
        Remote instance’s port number:        _______________

View the node entry created:

db2 list node directory

Key Lesson: Network connections are considered unreliable. Therefore, DB2 allows you to catalog nodes that do not immediately exist
       
2.        In the previous step, we purposely cataloged a node that does not exist to demonstrate a VERY common error encountered by new DB2 users.  What is the error code message when you attempt the following command

db2 attach to nodeX user <userid> using <password>

SQLCODE:        ____________________

Error Message:       

___________________________________________________

___________________________________________________

Key Lesson: If you see this error, you’ve probably cataloged the remote NODE wrong, or the server has not been set up properly for remote client access.

What is the difference between ATTACH and CONNECT?

______________________________________________________

______________________________________________________

______________________________________________________

3.        Remove the invalid node entry:

db2 uncatalog node nodeX

4.        Verify that the requirements for remote connectivity for YOUR database server are met (since others may connect to your machine in the next few steps)

#1) Has the DB2COMM registry variable been set to TCPIP?
        db2set –all

If not, set it
        db2set DB2COMM=tcpip

#2) Has the service name parameter (SVCENAME) in the DBM CFG been set?
        db2 get dbm cfg

If not, set it
        db2 update dbm cfg using svcename 50000

If you had to make any configuration settings, you will need to restart the instance before continuing.

5.        As someone who wants to establish a connection to a remote database server, you will need to ask your neighbor (or instructor) for the following information:

(In most cases, you will simply catalog your neighbor’s SAMPLE database, but feel free to experiment with different configurations.)

Remote Database Info:
        (PR)        Protocol                        ____________
        (IP)         IP Address or hostname        ____________
        (PN)        Instance Port Number                ____________
        (DB)        Database Name                ____________


6.        Setup your client to connect remotely issuing the following commands:

db2 catalog admin PR  node <admin_node_alias> remote IP
db2 catalog PR node <node_alias> remote IP server PN
db2 catalog database DB as <db_alias> at node <node_alias>

A port number is not necessary for the admin node. What port number does it always assume?        __________

If a remote machine has two instances, each with their own database:
a)        How many node directory entries are required at the client?         ________
b)        How many admin node entries are required at the client?        ________

7.        Verify your client configuration:

db2 list admin node directory
db2 list node directory
db2 list db directory

8.        Verify that you can attach to the remote node and connect to the remote database

db2 attach to <node_alias> user <userid> using <password>
db2 connect to <db_alias> user <userid> using <password>

Why is the userid and password required (by default) for ATTACH and CONNECT for remote instances and remote databases?

__________________________________________________________________


9.        On AIX, to work around a 32-bit architecture limitation, a LOOPBACK node is sometimes needed to support a larger number of local database connections. Perform the following as an exercise, even if you are not using AIX at this time:

db2 catalog admin tcpip node loopadm remote 127.0.0.1
db2 catalog tcpip node loopback remote 127.0.0.1 server 50000
db2 catalog database sample as loopsamp at node loopback

db2 connect to loopsamp user <userid> using  <password>

Why is a userid and password required (by default) when a database uses a loopback node?

____________________________________________________________________

____________________________________________________________________

10.        Clean up your client node and database directories:
db2 uncatalog node loopadm
db2 uncatalog node loopback
db2 uncatalog db loopsamp

db2 uncatalog db <remote_db>
db2 uncatalog node <remote_node>
db2 uncatalog node <remote_admin_node>

11.        If you have free time, practice setting up remote connectivity by connecting to another remote database. You may also want to try using the DB2 Client Configuration Assistant to perform client configuration.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章