本筆記接在上一篇 <<Linux下安裝DB2數據庫步驟>> http://blog.csdn.net/sunrier/article/details/7826233 ,學習過程中使用下載的官網DB2數據庫(免費版本)測試的,由於工作中DB2是用在項目中的,而且是收費的數據庫,故我只在項目編程中使用它(即公司的DB2數據庫收費版本),沒用來作爲自己測試用。嘿嘿,所以下面的大部分操作主要來自下載的官網DB2數據庫(免費版本)。
DB2相關數據庫命令
1.數據庫實例的啓動
首先要啓動數據庫的實例,即切換到db2inst1用戶(注:db2inst1用戶爲當前數據庫的實例),然後執行db2start啓動數據庫的實例
[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2start
SQL1063N DB2START processing was successful.
[db2inst1@localhost ~]$
2.數據庫實例的關閉
首先在db2inst1用戶下強制關閉實例上的所有應用程序,然後再關閉數據庫實例
[db2inst1@localhost ~]$ db2 force application all
DB20000I The FORCE APPLICATION command completed successfully.
DB21024I This command is asynchronous and may not be effective immediately.
[db2inst1@localhost ~]$ db2stop
SQL1064N DB2STOP processing was successful.
[db2inst1@localhost ~]$
強制停止
[db2inst1@localhost DB2]$ db2stop force
SQL1064N DB2STOP processing was successful.
[db2inst1@localhost DB2]$
3.顯示所有的實例
[db2inst1@localhost ~]$ db2ilist
db2inst1
[db2inst1@localhost ~]$
4.顯示當前的實例
[db2inst1@localhost ~]$ db2 get instance
The current database manager instance is: db2inst1
[db2inst1@localhost ~]$
5.刪除一個實例(注:需切換到root用戶權限下)
[root@localhost ~]# cd /opt/ibm/db2/V9.7/instance
[root@localhost instance]# pwd
/opt/ibm/db2/V9.7/instance
[root@localhost instance]# ./db2idrop db2inst1
DBI1070I Program db2idrop completed successfully.
[root@localhost instance]#
6.列出當前實例中有哪些數據庫
[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2 list db directory
SQL1031N The database directory cannot be found on the indicated file system.
SQLSTATE=58031
[db2inst1@localhost ~]$
注:上面信息說明實例中沒有數據庫
7.創建數據庫
[root@localhost instance]# su - db2inst1
[db2inst1@localhost ~]$ db2 create database test
SQL1032N No start database manager command was issued. SQLSTATE=57019
[db2inst1@localhost ~]$
[db2inst1@localhost ~]$ db2start
SQL5043N Support for one or more communications protocols failed to start successfully. However, core database manager functionality started successfully.
[db2inst1@localhost ~]$ db2 create database test
DB20000I The CREATE DATABASE command completed successfully.
[db2inst1@localhost ~]$ db2 list db directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = TEST
Database name = TEST
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
[db2inst1@localhost ~]$
注:上面通過創建一個test的數據庫,並說明了創建數據庫時,要先啓動數據庫,然後創建數據庫test成功後並列出了當前實例中的所有數據庫,只存在數據庫test
使用UTF-8編碼
db2 create database test on '/home/db2inst1' using codeset UTF-8 territory CN
on '/home/db2inst1' 表示數據庫路徑
一般情況下'/home/db2inst1'爲默認數據庫路徑
8.連接數據庫
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$
注:用密碼情況下格式[db2inst1@localhost ~]$ db2 connect to test user username using password
db2 connect to <database> user <username> using <password>
9.列出當前實例中所有激活的數據庫
[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2 list active databases
SQL1032N No start database manager command was issued. SQLSTATE=57019
[db2inst1@localhost ~]$ db2 list db directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = TEST
Database name = TEST
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
[db2inst1@localhost ~]$ db2start
SQL1026N The database manager is already active.
[db2inst1@localhost ~]$ db2 list active databases
SQL1611W No data was returned by Database System Monitor.
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list active databases
Active Databases
Database name = TEST
Applications connected currently = 1
Database path = /home/db2inst1/db2inst1/NODE0000/SQL00001/
[db2inst1@localhost ~]$
注:可以看出查看當前激活的數據庫爲已經啓動後連接的數據庫
10.查看錶的空間
[db2inst1@localhost ~]$ db2 list tablespaces [ show detail ]
Tablespaces for Current Database
Tablespace ID = 0
Name = SYSCATSPACE
Type = Database managed space
Contents = All permanent data. Regular table space.
State = 0x0000
Detailed explanation:
Normal
Tablespace ID = 1
Name = TEMPSPACE1
Type = System managed space
Contents = System Temporary data
State = 0x0000
Detailed explanation:
Normal
Tablespace ID = 2
Name = USERSPACE1
Type = Database managed space
Contents = All permanent data. Large table space.
State = 0x0000
Detailed explanation:
Normal
[db2inst1@localhost ~]$
注: show detail爲可選項,顯示更詳細信息
或者 db2pd -tablespaces -db 數據庫名
[db2inst1@localhost ~]$ db2pd -tablespaces -db test
Database Partition 0 -- Database TEST -- Active -- Up 0 days 00:03:11
Tablespace Configuration:
Address Id Type Content PageSz ExtentSz Auto Prefetch BufID BufIDDisk FSC NumCntrs MaxStripe LastConsecPg Name
0x9DC2A060 0 DMS Regular 4096 4 Yes 4 1 1 Off 1 0 3 SYSCATSPACE
0x9DC2B4B0 1 SMS SysTmp 4096 32 Yes 32 1 1 On 1 0 31 TEMPSPACE1
0x9DC30940 2 DMS Large 4096 32 Yes 32 1 1 Off 1 0 31 USERSPACE1
Tablespace Statistics:
Address Id TotalPgs UsablePgs UsedPgs PndFreePgs FreePgs HWM Max HWM State MinRecTime NQuiescers PathsDropped
0x9DC2A060 0 24576 24572 18712 0 5860 18712 18712 0x00000000 0 0 No
0x9DC2B4B0 1 1 1 1 0 0 0 0 0x00000000 0 0 No
0x9DC30940 2 8192 8160 96 0 8064 96 96 0x00000000 0 0 No
Tablespace Autoresize Statistics:
Address Id AS AR InitSize IncSize IIP MaxSize LastResize LRF
0x9DC2A060 0 Yes Yes 33554432 -1 No None None No
0x9DC2B4B0 1 Yes No 0 0 No 0 None No
0x9DC30940 2 Yes Yes 33554432 -1 No None None No
Containers:
Address TspId ContainNum Type TotalPgs UseablePgs PathID StripeSet Container
0x9B218F00 0 0 File 24576 24572 0 0 /home/db2inst1/db2inst1/NODE0000/TEST/T0000000/C0000000.CAT
0x9B219120 1 0 Path 1 1 0 0 /home/db2inst1/db2inst1/NODE0000/TEST/T0000001/C0000000.TMP
0x9B219390 2 0 File 8192 8160 0 0 /home/db2inst1/db2inst1/NODE0000/TEST/T0000002/C0000000.LRG
[db2inst1@localhost ~]$
11.列出數據庫中所有用戶表
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
0 record(s) selected.
[db2inst1@localhost ~]$
注:上面信息說明數據庫test中還沒有表
12.在數據庫test中創建表student
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 "create table student (id int,fname varchar(30),age int)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
STUDENT DB2INST1 T 2012-08-06-14.38.33.456768
1 record(s) selected.
[db2inst1@localhost ~]$
13.向表student中添加數據信息
[db2inst1@localhost ~]$ db2 "insert into student values (1,'Tom',22)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "insert into student values (2,'Jack',21)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "insert into student values (3,'Sunrier',25)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$
14.顯示錶student所有的信息
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$
15.更改表student中的數據(如將Sunrier的年齡改爲22)
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "update student set age=22 where fname='Sunrier'"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 22
3 record(s) selected.
[db2inst1@localhost ~]$
16.查看錶student結構
[db2inst1@localhost ~]$ db2 describe table student
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$
或
[db2inst1@localhost ~]$ db2 "describe select * from student"
Column Information
Number of columns: 3
SQL type Type length Column name Name length
-------------------- ----------- ------------------------------ -----------
497 INTEGER 4 ID 2
449 VARCHAR 30 FNAME 5
497 INTEGER 4 AGE 3
[db2inst1@localhost ~]$
或
[db2inst1@localhost ~]$ db2 "describe select * from db2inst1.student"
Column Information
Number of columns: 3
SQL type Type length Column name Name length
-------------------- ----------- ------------------------------ -----------
497 INTEGER 4 ID 2
449 VARCHAR 30 FNAME 5
497 INTEGER 4 AGE 3
[db2inst1@localhost ~]$
17.創建一個新表(如people)與數據庫中某個表(如student)結構相同
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
1 record(s) selected.
[db2inst1@localhost ~]$ db2 describe table student
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$ db2 create table people like student
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
2 record(s) selected.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$
18.兩個結構相同的表,將原來數據庫中某個表(如student)的數據導入與它相同結構的新表(如people)中
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
2 record(s) selected.
[db2inst1@localhost ~]$ db2 describe table student
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE
----------- ------------------------------ -----------
0 record(s) selected.
[db2inst1@localhost ~]$ db2 "insert into people select * from student"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$
19.修改一個表的字段類型(如表people中的fname字段把varchar(30)改爲varchar(28))
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "alter table people alter column fname set data type varchar(28)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 28 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE
----------- ---------------------------- -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$
格式:db2 "alter table <tablename> alter column <columnname> set data type <datatype>"
tablename:表名
columnname:字段名
datatype:字段類型
注:
一般更改字段類型是有操作限制的. 將字段改爲比之前類型長度大的可以;如果要改小,必須先drop掉原來的column,然後再重新添加.
雖然我上面的執行成功了,可能是因爲我用的官方免費版本的,如果遇到把長度大的改爲小的無法執行,先用drop,再重新添加
20.向一個表添加字段(如向表people中添加備註信息字段notes;向表people中添加分數字段score)
格式:db2 "alter table <tablename> add <columnname> <datatype>"
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 30 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
3 Sunrier 25
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "alter table people add notes varchar(100)"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
4 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES
----------- ------------------------------------ ----------- ----------------------------------------------------------------------------------------------------
1 Tom 22 -
2 Jack 21 -
3 Sunrier 25 -
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "alter table people add score integer"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
SCORE SYSIBM INTEGER 4 0 Yes
5 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES SCORE
----------- ----------------- ----------- --------------
1 Tom 22 - -
2 Jack 21 - -
3 Sunrier 25 - -
3 record(s) selected.
[db2inst1@localhost ~]$
21.刪除表中的某個字段(如刪除表people中的字段score)
格式:db2 "alter table <tablename> drop column <columnname>"
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
SCORE SYSIBM INTEGER 4 0 Yes
5 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES SCORE
----------- ----------------- ----------- --------------
1 Tom 22 - -
2 Jack 21 - -
3 Sunrier 25 - -
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "alter table people drop column score"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
4 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES
----------- -------------- ----------- --------------------
1 Tom 22 -
2 Jack 21 -
3 Sunrier 25 -
3 record(s) selected.
[db2inst1@localhost ~]$
注:如果drop掉字段之後,可能會導致表查詢/插入操作不能執行,則需要執行一下reorg命令,優化數據結構,
格式如db2 reorg table <tablename>
[db2inst1@localhost ~]$ db2 reorg table people
DB20000I The REORG command completed successfully.
[db2inst1@localhost ~]$
22.給表中添加帶默認值的字段(如向表people中添加分數字段score,默認設置爲90)
格式:db2 "alter table <tablename> add column <columnname> <datatype> not null with default <value> "
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
4 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES
----------- -------------- ----------- --------------------
1 Tom 22 -
2 Jack 21 -
3 Sunrier 25 -
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "alter table people add column score interger not null with default 90"
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table people
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 Yes
FNAME SYSIBM VARCHAR 36 0 Yes
AGE SYSIBM INTEGER 4 0 Yes
NOTES SYSIBM VARCHAR 100 0 Yes
SCORE SYSIBM INTEGER 4 0 No
5 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from people"
ID FNAME AGE NOTES SCORE
----------- ------------------------------------ ----------- ---------------------------------------------------------------------------------------------------- -----------
1 Tom 22 - 90
2 Jack 21 - 90
3 Sunrier 25 - 90
3 record(s) selected.
[db2inst1@localhost ~]$
例1.向表people中添加地址字段address默認設置爲shanghai
db2 "alter table people add column address varchar(30) not null with default 'ShangHai'"
例2.將表people中地址字段address默認設置改爲當前時間
db2 "alter table people alter column address set default current date"
格式:db2 "alter table <tablename> alter column <columnname> set default <value>"
23.列出數據庫中用戶表
[db2inst1@localhost ~]$ db2 list tables for user
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
1 record(s) selected.
[db2inst1@localhost ~]$
24.列出數據庫中所有系統表
[db2inst1@localhost ~]$ db2 list tables for system
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
ATTRIBUTES SYSCAT V 2012-08-06-14.20.49.182036
AUDITPOLICIES SYSCAT V 2012-08-06-14.20.49.237474
AUDITUSE SYSCAT V 2012-08-06-14.20.49.243535
BUFFERPOOLDBPARTITIONS SYSCAT V 2012-08-06-14.20.49.264336
BUFFERPOOLNODES SYSCAT V 2012-08-06-14.20.49.277662
BUFFERPOOLS SYSCAT V 2012-08-06-14.20.49.281563
CASTFUNCTIONS SYSCAT V 2012-08-06-14.20.49.309007
CHECKS SYSCAT V 2012-08-06-14.20.49.314078
...............................................................................
...............................................................................
...............................................................................
...............................................................................
...............................................................................
...............................................................................
ROUTINES SYSSTAT V 2012-08-06-14.20.52.502569
TABLES SYSSTAT V 2012-08-06-14.20.52.510027
HMON_ATM_INFO SYSTOOLS T 2012-08-06-14.35.41.029633
HMON_COLLECTION SYSTOOLS T 2012-08-06-14.35.41.208925
POLICY SYSTOOLS T 2012-08-06-14.35.40.156347
396 record(s) selected.
[db2inst1@localhost ~]$
25.列出數據庫中所有表
[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2start
SQL1026N The database manager is already active.
[db2inst1@localhost ~]$ db2 list active databases
SQL1611W No data was returned by Database System Monitor.
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list tables for all
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
ATTRIBUTES SYSCAT V 2012-08-06-15.21.20.819408
AUDITPOLICIES SYSCAT V 2012-08-06-15.21.20.886143
AUDITUSE SYSCAT V 2012-08-06-15.21.20.911042
BUFFERPOOLDBPARTITIONS SYSCAT V 2012-08-06-15.21.20.936300
BUFFERPOOLNODES SYSCAT V 2012-08-06-15.21.20.957929
BUFFERPOOLS SYSCAT V 2012-08-06-15.21.20.978954
CASTFUNCTIONS SYSCAT V 2012-08-06-15.21.21.011517
CHECKS SYSCAT V 2012-08-06-15.21.21.036428
COLAUTH SYSCAT V 2012-08-06-15.21.21.061589
COLCHECKS SYSCAT V 2012-08-06-15.21.21.094771
COLDIST SYSCAT V 2012-08-06-15.21.21.114126
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
FUNCTIONS SYSSTAT V 2012-08-06-15.21.25.272699
INDEXES SYSSTAT V 2012-08-06-15.21.25.289851
ROUTINES SYSSTAT V 2012-08-06-15.21.25.315173
TABLES SYSSTAT V 2012-08-06-15.21.25.320565
HMON_ATM_INFO SYSTOOLS T 2012-08-06-15.30.39.352789
HMON_COLLECTION SYSTOOLS T 2012-08-06-15.30.39.498061
POLICY SYSTOOLS T 2012-08-06-15.30.38.749121
397 record(s) selected.
[db2inst1@localhost ~]$
26.列出數據庫中特定用戶表
[root@localhost ~]# su - db2inst1
[db2inst1@localhost ~]$ db2start
SQL1026N The database manager is already active.
[db2inst1@localhost ~]$ db2 list active databases
SQL1611W No data was returned by Database System Monitor.
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list tables for schema db2inst1
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
1 record(s) selected.
[db2inst1@localhost ~]$ db2 list tables for schema db2inst2
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
0 record(s) selected.
[db2inst1@localhost ~]$
格式: db2 list tables for schema <username>
注:符號<>表示必選項
27.刪除表中的數據 :
[db2inst1@localhost ~]$ db2 delete from student where id=3
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 "select * from student"
ID FNAME AGE
----------- ------------------------------ -----------
1 Tom 22
2 Jack 21
2 record(s) selected.
[db2inst1@localhost ~]$
28.刪除一個數據庫中的某個表
[db2inst1@localhost ~]$ db2 drop table student
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
0 record(s) selected.
[db2inst1@localhost ~]$
29.刪除一個數據庫test
[db2inst1@localhost ~]$ db2 list db directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = TEST
Database name = TEST
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
[db2inst1@localhost ~]$ db2 drop db test
SQL1035N The database is currently in use. SQLSTATE=57019
SQL1025N The database manager was not stopped because databases are still active.
[db2inst1@localhost ~]$ db2 connect reset
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 drop db test
DB20000I The DROP DATABASE command completed successfully.
[db2inst1@localhost ~]$ db2 list db directory
SQL1057W The system database directory is empty. SQLSTATE=01606
[db2inst1@localhost ~]$
注:刪除數據庫首先要斷開數據庫的連接
30.顯示當前數據庫連接有哪些應用程序
[root@localhost ~]# su - db2inst1
[db2inst1@localhost DB2]$ db2 list application
SQL1611W No data was returned by Database System Monitor.
[db2inst1@localhost DB2]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost DB2]$ db2 list application
Auth Id Application Appl. Application Id DB # of
Name Handle Name Agents
-------- -------------- ---------- -------------------------------------------------------------- -------- -----
DB2INST1 db2bp 35 *LOCAL.db2inst1.120807014245 TEST 1
[db2inst1@localhost DB2]$
31.查看DB2全部受支持的註冊表變量列表
[db2inst1@localhost DB2]$ db2set -lr
DB2_OVERRIDE_BPF
DB2_PARALLEL_IO
DB2ACCOUNT
DB2ADMINSERVER
DB2BQTIME
DB2BQTRY
.........................................
.........................................
.........................................
.........................................
.........................................
.........................................
DB2TCP_CLIENT_KEEPALIVE_TIMEOUT
DB2_PMODEL_SETTINGS
DB2_PMAP_COMPATIBILITY
DB2_HADR_ROS
DB2_STANDBY_ISO
[db2inst1@localhost DB2]$
32.更改DB2 UDB註冊表變量的值
db2set registry_variable_name=new_value
[db2inst1@localhost ~]$ db2set DB2COMM=TCPIP
[db2inst1@localhost ~]$
33.查看在服務器上已經設置的所有DB2概要文件註冊表
[db2inst1@localhost DB2]$ db2set -all
[i] DB2PROCESSORS=0
[i] DB2COMM=TCPIP
[i] DB2AUTOSTART=YES
[g] DB2SYSTEM=localhost.localdomain
[g] DB2INSTDEF=db2inst1
[g] DB2ADMINSERVER=db2dasusr1
[db2inst1@localhost DB2]$
34.導出表中的數據
以DEL格式導出
db2 "export to teacher.txt of del select * from teacher"
db2 "export to teacher_bak.txt of del modified by coldel| select * from teacher"
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher.sql TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ db2 "export to teacher.txt of del select * from teacher"
SQL3104N The Export utility is beginning to export data to file
"teacher.txt".
SQL3105N The Export utility has finished exporting "2" rows.
Number of rows exported: 2
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ cat teacher.txt
1,"Lory","上海徐匯中學",19780806
2,"Sunrier","田林中學",19880627
[db2inst1@localhost ~]$
字段之間默認分隔符號爲逗號,下面使用'|'分割
[db2inst1@localhost ~]$ db2 "export to teacher_bak.txt of del modified by coldel| select * from teacher"
SQL3104N The Export utility is beginning to export data to file
"teacher_bak.txt".
SQL3105N The Export utility has finished exporting "2" rows.
Number of rows exported: 2
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher_bak.txt teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ cat teacher_bak.txt
1|"Lory"|"上海徐匯中學"|19780806
2|"Sunrier"|"田林中學"|19880627
[db2inst1@localhost ~]$
以IXF格式導出
[db2inst1@localhost ~]$ pwd
/home/db2inst1
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher_bak.txt teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ ls /home/db2inst1/Sunrier
TEST.0.db2inst1.NODE0000.CATN0000.20120817150317.001
[db2inst1@localhost ~]$ db2 "export to /home/db2inst1/Sunrier/teacher.ixf of ixf select * from teacher"
SQL3104N The Export utility is beginning to export data to file
"/home/db2inst1/Sunrier/teacher.ixf".
SQL3105N The Export utility has finished exporting "4" rows.
Number of rows exported: 4
[db2inst1@localhost ~]$ ls /home/db2inst1/Sunrier
teacher.ixf TEST.0.db2inst1.NODE0000.CATN0000.20120817150317.001
[db2inst1@localhost ~]$
如需要導出記錄過程的message
[db2inst1@localhost ~]$ db2 "export to /home/db2inst1/Sunrier/teacher_bak.ixf of ixf messages /home/db2inst1/Sunrier/teacher.msg select * from teacher"
Number of rows exported: 4
[db2inst1@localhost ~]$ ls /home/db2inst1/Sunrier
teacher_bak.ixf teacher.ixf teacher.msg TEST.0.db2inst1.NODE0000.CATN0000.20120817150317.001
[db2inst1@localhost ~]$ cat /home/db2inst1/Sunrier/teacher.msg
SQL3104N The Export utility is beginning to export data to file
"/home/db2inst1/Sunrier/teacher_bak.ixf".
SQL3105N The Export utility has finished exporting "4" rows.
[db2inst1@localhost ~]$
35.導入數據到一個表中
db2 "import from teacher.txt of del insert into teacher"
db2 "import from teacher_bak.txt of del modified by coldel| insert into teacher"
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ cat teacher.txt
3,"Jerry","上海徐匯中學",19710306
4,"Tim","田林中學",19820627
[db2inst1@localhost ~]$ db2 "select * from teacher"
ID FNAME ADDRESS BIRTH
----------- -------------------- -------------------------------------------------------------------------------- ----------
1 Lory 上海徐匯中學 1978-08-06
2 Sunrier 田林中學 1988-06-27
2 record(s) selected.
[db2inst1@localhost ~]$ db2 "import from teacher.txt of del insert into teacher"
SQL3109N The utility is beginning to load data from file "teacher.txt".
SQL3110N The utility has completed processing. "2" rows were read from the
input file.
SQL3221W ...Begin COMMIT WORK. Input Record Count = "2".
SQL3222W ...COMMIT of any database changes was successful.
SQL3149N "2" rows were processed from the input file. "2" rows were
successfully inserted into the table. "0" rows were rejected.
Number of rows read = 2
Number of rows skipped = 0
Number of rows inserted = 2
Number of rows updated = 0
Number of rows rejected = 0
Number of rows committed = 2
[db2inst1@localhost ~]$ db2 "select * from teacher"
ID FNAME ADDRESS BIRTH
----------- -------------------- -------------------------------------------------------------------------------- ----------
1 Lory 上海徐匯中學 1978-08-06
2 Sunrier 田林中學 1988-06-27
3 Jerry 上海徐匯中學 1971-03-06
4 Tim 田林中學 1982-06-27
4 record(s) selected.
[db2inst1@localhost ~]$
36.利用腳本創建表
格式:db2 -tvf scriptName.sql
teacher.sql爲以下內容
----建立表teacher
create table teacher
(
id integer not null ,
fname varchar(20) not null,
address varchar(80) default '上海',
birth date,
primary key(id)
);
--建表結束
--以下爲插入數據字段
insert into teacher values(1,'Lory','上海徐匯中學','1978-08-06');
insert into teacher values(2,'Sunrier','田林中學','1988-06-27');
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
2 record(s) selected.
[db2inst1@localhost ~]$ db2 -tvf teacher.sql
create table teacher ( id integer not null , fname varchar(20) not null, address varchar(80) default '上海', birth date, primary key(id) )
DB20000I The SQL command completed successfully.
insert into teacher values(1,'Lory','上海徐匯中學','1978-08-06')
DB20000I The SQL command completed successfully.
insert into teacher values(2,'Sunrier','田林中學','1988-06-27')
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 describe table teacher
Data type Column
Column name schema Data type name Length Scale Nulls
------------------------------- --------- ------------------- ---------- ----- ------
ID SYSIBM INTEGER 4 0 No
FNAME SYSIBM VARCHAR 20 0 No
ADDRESS SYSIBM VARCHAR 80 0 Yes
BIRTH SYSIBM DATE 4 0 Yes
4 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from teacher"
ID FNAME ADDRESS BIRTH
----------- -------------------- -------------------------------------------------------------------------------- ----------
1 Lory 上海徐匯中學 1978-08-06
2 Sunrier 田林中學 1988-06-27
2 record(s) selected.
[db2inst1@localhost ~]$
37.備份數據庫(如防止表誤操作)
格式:db2 backup db <database name> [ to <dir name> ]
database name:表示數據庫
to <dir name>:表示爲備份到的目錄路徑,爲可選項,默認在當前目錄下
[db2inst1@localhost ~]$ ls
db2inst1 sqllib teacher.sql
[db2inst1@localhost ~]$ db2 list db directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = TEST
Database name = TEST
Local database directory = /home/db2inst1
Database release level = d.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
[db2inst1@localhost ~]$ db2 backup db test
Backup successful. The timestamp for this backup image is : 20120817103306
[db2inst1@localhost ~]$ ls
db2inst1 sqllib teacher.sql TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ db2 list active databases
SQL1611W No data was returned by Database System Monitor.
[db2inst1@localhost ~]$
注:TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001即爲備份的數據庫節點文件;
執行備份命令時,如果出現無法執行,則先斷開數據庫的連接再執行備份命令.
我使用的免費版本的DB2數據庫測試時,從上面可以看出沒有斷開也可以執行,但執行完,發現數據庫
處於斷開狀態了
如: db2 force application all
強制關閉實例上的所有應用程序
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher_bak.txt teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ db2 backup db test to /home/db2inst1/Sunrier
Backup successful. The timestamp for this backup image is : 20120817150317
[db2inst1@localhost ~]$ ls
db2inst1 sqllib Sunrier teacher_bak.txt teacher.sql teacher.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
[db2inst1@localhost ~]$ cd Sunrier/
[db2inst1@localhost Sunrier]$ ls
TEST.0.db2inst1.NODE0000.CATN0000.20120817150317.001
[db2inst1@localhost Sunrier]$
說明:上面的方法爲脫機備份(也稱爲離線備份或者冷備份),此方法必須斷開所有與數據庫連接的應用後才能進行,備份時數據庫不能提供給用戶使用.
38.恢復數據庫(如將一個表刪除後,通過刪除前的備份文件恢復)
格式:db2 restore db <database name> [ from <dir name> ]
database name:表示恢復的數據庫名
from <dir name>:表示爲從哪個目錄路徑下恢復,爲可選項,默認在當前目錄下
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
TEACHER DB2INST1 T 2012-08-17-10.18.18.245263
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from teacher"
ID FNAME ADDRESS BIRTH
----------- -------------------- -------------------------------------------- ----------
1 Lory 上海徐匯中學 1978-08-06
2 Sunrier 田林中學 1988-06-27
3 Jerry 上海徐匯中學 1971-03-06
4 Tim 田林中學 1982-06-27
4 record(s) selected.
[db2inst1@localhost ~]$ db2 drop table teacher
DB20000I The SQL command completed successfully.
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
2 record(s) selected.
[db2inst1@localhost ~]$ db2 connect reset
DB20000I The SQL command completed successfully.
[db2inst1@localhost Sunrier]$ ls /home/db2inst1/Sunrier
TEST.0.db2inst1.NODE0000.CATN0000.20120817150317.001
[db2inst1@localhost ~]$ db2 restore db test from /home/db2inst1/Sunrier
SQL2539W Warning! Restoring to an existing database that is the same as the
backup image database. The database files will be deleted.
Do you want to continue ? (y/n) y
DB20000I The RESTORE DATABASE command completed successfully.
[db2inst1@localhost ~]$ db2 list tables
SQL1024N A database connection does not exist. SQLSTATE=08003
[db2inst1@localhost ~]$ db2 connect to test
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
[db2inst1@localhost ~]$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
PEOPLE DB2INST1 T 2012-08-16-15.13.49.396370
STUDENT DB2INST1 T 2012-08-06-15.26.17.189538
TEACHER DB2INST1 T 2012-08-17-10.18.18.245263
3 record(s) selected.
[db2inst1@localhost ~]$ db2 "select * from teacher"
ID FNAME ADDRESS BIRTH
----------- -------------------- -------------------------------------------- ----------
1 Lory 上海徐匯中學 1978-08-06
2 Sunrier 田林中學 1988-06-27
3 Jerry 上海徐匯中學 1971-03-06
4 Tim 田林中學 1982-06-27
4 record(s) selected.
[db2inst1@localhost ~]$
注:如果想把恢復的數據庫更改爲新的數據庫名,則格式如下
db2 restore db <database name> [ from <dir name> into <new database name> ]
例:db2 restore db test from /home/db2inst1/Sunrier into testdb
或者db2 restore db test from "/home/db2inst1/Sunrier" into testdb
39.db2move命令的使用
db2move是一個集成式的數據移動工具,它具有導入(import),導出(export),裝入(load)三種操作方法.
db2move導出的數據文件格式是IXF(Integration Exchange Format)集成交換格式.
格式:db2move <database name> [ export -tc <username> ] -u <username> -p <password>
參數: -tc 創建表的用戶名
-tn 用戶的表名
-sn 模式名,即導出該模式下的所有表
查看db2move命令幫助db2move -help
導出test數據庫中的全部數據
[db2inst1@localhost ~]$ ls
db2inst1 Sunrier teacher.sql TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
sqllib teacher_bak.txt teacher.txt
[db2inst1@localhost ~]$ db2move test export -u db2inst1 -p Sunrier
Application code page not determined, using ANSI codepage 1208
***** DB2MOVE *****
Action: EXPORT
Start time: Fri Aug 17 15:58:34 2012
Connecting to database TEST ... successful! Server : DB2 Common Server V9.7.1
Binding package automatically ... /home/db2inst1/sqllib/bnd/db2common.bnd ... successful!
Binding package automatically ... /home/db2inst1/sqllib/bnd/db2move.bnd ... successful!
EXPORT: 136 rows from table "SYSTOOLS"."HMON_ATM_INFO"
EXPORT: 0 rows from table "SYSTOOLS"."HMON_COLLECTION"
EXPORT: 3 rows from table "DB2INST1"."PEOPLE"
EXPORT: 5 rows from table "SYSTOOLS"."POLICY"
EXPORT: 3 rows from table "DB2INST1"."STUDENT"
EXPORT: 4 rows from table "DB2INST1"."TEACHER"
Disconnecting from database ... successful!
End time: Fri Aug 17 15:58:35 2012
[db2inst1@localhost ~]$ ls
db2inst1 sqllib tab1.msg tab3.ixf tab4.ixf tab5.msg teacher_bak.txt TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
db2move.lst Sunrier tab2.ixf tab3.msg tab4.msg tab6.ixf teacher.sql
EXPORT.out tab1.ixf tab2.msg tab4a.001.lob tab5.ixf tab6.msg teacher.txt
[db2inst1@localhost ~]$
注:
執行上面命令後會把數據庫test中全部數據提取到當前目錄(/home/db2inst1),每個表的內容都存儲在一個.ixf文件中,
每個.ixf文件都有一個與之相對應的.msg文件,.msg文件是描述從表中導出數據時的信息.例外還有兩個文件,db2move.lst用來
記錄.ixf文件,.msg文件與表一一對應.EXPORT.out記錄的是導出數據時的屏幕輸出.
導出test數據庫中的teacher表中的信息
[db2inst1@localhost ~]$ ls
db2inst1 Sunrier teacher.sql TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
sqllib teacher_bak.txt teacher.txt
[db2inst1@localhost ~]$ db2move test export -tn teacher -u db2inst1 -p Sunrier
Application code page not determined, using ANSI codepage 1208
***** DB2MOVE *****
Action: EXPORT
Start time: Fri Aug 17 16:33:24 2012
All table names matching: TEACHER;
Connecting to database TEST ... successful! Server : DB2 Common Server V9.7.1
EXPORT: 4 rows from table "DB2INST1"."TEACHER"
Disconnecting from database ... successful!
End time: Fri Aug 17 16:33:24 2012
[db2inst1@localhost ~]$ ls
db2inst1 EXPORT.out Sunrier tab1.msg teacher.sql TEST.0.db2inst1.NODE0000.CATN0000.20120817103306.001
db2move.lst sqllib tab1.ixf teacher_bak.txt teacher.txt
[db2inst1@localhost ~]$
40.查看test數據庫備份的歷史記錄
格式:db2 list history backup all for <database name>
[db2inst1@localhost ~]$ db2 list history backup all for test
List History File for test
Number of matching file entries = 4
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
B D 20120817103306001 F D S0000000.LOG S0000000.LOG
----------------------------------------------------------------------------
Contains 3 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: DB2 BACKUP TEST OFFLINE
Start Time: 20120817103306
End Time: 20120817103315
Status: A
----------------------------------------------------------------------------
EID: 4 Location: /home/db2inst1
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
B D 20120817150248000 F S0000000.LOG
----------------------------------------------------------------------------
Contains 3 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: DB2 BACKUP TEST OFFLINE
Start Time: 20120817150248
End Time: 20120817150249
Status: A
----------------------------------------------------------------------------
EID: 5 Location:
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
B D 20120817150317001 F D S0000000.LOG S0000000.LOG
----------------------------------------------------------------------------
Contains 3 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: DB2 BACKUP TEST OFFLINE
Start Time: 20120817150317
End Time: 20120817150323
Status: A
----------------------------------------------------------------------------
EID: 6 Location: /home/db2inst1/Sunrier
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
B D 20120817150825001 F D S0000000.LOG S0000000.LOG
----------------------------------------------------------------------------
Contains 3 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: DB2 BACKUP TEST OFFLINE
Start Time: 20120817150825
End Time: 20120817150832
Status: I
----------------------------------------------------------------------------
EID: 7 Location: /home/db2inst1/Sunrier
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
R D 20120817151620001 F S0000000.LOG S0000000.LOG 20120817150317
----------------------------------------------------------------------------
Contains 3 tablespace(s):
00001 SYSCATSPACE
00002 USERSPACE1
00003 SYSTOOLSPACE
----------------------------------------------------------------------------
Comment: RESTORE TEST NO RF
Start Time: 20120817151620
End Time: 20120817151629
Status: A
----------------------------------------------------------------------------
EID: 8 Location:
[db2inst1@localhost ~]$
41.讀數據庫管理程序配置
db2 get dbm cfg
42.寫數據庫管理程序配置
db2 update dbm cfg using 參數名 參數值
43.查看數據庫的配置
db2 connect to <database> user <username> using <password>
db2 get db cfg [ for <database> ]
database:數據庫名
username:用戶名
password:表示密碼
44.設置數據庫的配置
db2 connect to <database> user <username> using <password>
db2 update db cfg for <database> using 參數名 參數值
45.添加DB2服務端口50000
切換到root用戶下su - root
[root@localhost etc]# vi /etc/services
在/etc/services文件中加入db2inst1 50000/tcp
/etc/services內容格式:
# service-name port/protocol [aliases ...] [# comment]
46.斷開與數據庫的連接
db2 connect reset 或 db2 terminate
db2 disconnect <database>
47.查看命令幫助
[db2inst1@localhost ~]$ db2 ? db2start
{START DATABASE MANAGER | DB2START} [REMOTE [INSTANCE] instance-name
{ADMINNODE node-name | HOSTNAME hostname} USER username USING password]
[ADMIN MODE {USER username | GROUP groupname }] [PROFILE profile]
[DBPARTITIONNUM db-partition-number] [ADD DBPARTITIONNUM HOSTNAME hostname
PORT logical-port [COMPUTER computer-name] [USER username] [PASSWORD password]
[NETNAME netname] [LIKE DBPARTITIONNUM db-partition-number |
WITHOUT TABLESPACES]] | STANDALONE | RESTART [HOSTNAME hostname]
[PORT logical-port] [COMPUTER computer-name] [USER username] [PASSWORD password]
[NETNAME netname] ] ]
NOTE: From the operating system prompt, prefix commands with 'db2'.
Special characters MAY require an escape sequence (\), for example:
db2 \? change database
db2 ? change database xxx comment with \"text\"
[db2inst1@localhost ~]$
格式:db2 ? <command>
48.查看錯誤碼信息
[db2inst1@localhost ~]$ db2 ? 22003
SQLSTATE 22003: A numeric value is out of range.
[db2inst1@localhost ~]$
格式:db2 ? <ERRCODE>
49.待定,工作過程中遇到相關問題再補充......
實例程序
//db2dbproc.sqc
/************************************************************
FileName : db2dbproc.sqc
FileFunc : C語言接口訪問本地DB2數據庫
Version : V0.1
Author : Sunrier
Date : 2012-08-06
Descp : Linux下使用C語言訪問DB2函數
*************************************************************/
//(嵌入SQL語句的關鍵字不區分大小寫)
#include <stdio.h>
#include <string.h>
#include <sqlca.h>
#define PARAERR 0x04
#define DATABASEERR 0x08
EXEC SQL include sqlca;
int check_error(char szMessage[])
{
if( sqlca.sqlcode )
{
printf("Check error report : \n");
printf("Error occured : %s ,sqlcode = [%d] \n",szMessage,sqlca.sqlcode);
return 1;
}
return 0;
}
int db2_login(int iArgcFlag,char *pUserName,char *pPassword,char *pDataBase)
{
int iRetCode = 1;
EXEC SQL BEGIN DECLARE SECTION;
char szUserName[50];
char szPassword[50];
char szDataBase[50];
EXEC SQL END DECLARE SECTION;
memset(szUserName,0,sizeof(szUserName));
memset(szPassword,0,sizeof(szPassword));
memset(szDataBase,0,sizeof(szDataBase));
strcpy(szDataBase,pDataBase);
if( 1==iArgcFlag )
{
EXEC SQL CONNECT TO:szDataBase ;
}
else
{
if( 3==iArgcFlag )
{
strcpy(szUserName,pUserName);
strcpy(szPassword,pPassword);
EXEC SQL CONNECT TO:szDataBase USER:szUserName USING:szPassword;
}
else
{
printf("Usage : argc found error !\n");
return PARAERR;
}
}
iRetCode = check_error("CONNECT TO DATABASE");
return iRetCode;
}
void db2_logout( void )
{
EXEC SQL CONNECT RESET;
check_error("CONNECT RESET");
}
//demo.c
/*************************************************************
FileName : demo.c
FileFunc : 測試登陸和關閉DB2數據庫
Version : V0.1
Author : Sunrier
Date : 2012-08-06
Descp : Linux下使用C語言訪問DB2函數
*************************************************************/
#include <stdio.h>
int main(int argc,char *argv[])
{
int iRetCode = 0;
int iArgcFlag;
char szUserName[20] = "Sunrier";
char szPassword[20] = "redhat";
char szDatabase[20] = "test";
iArgcFlag = 1;
iRetCode = db2_login(iArgcFlag,szUserName,szPassword,szDatabase);
printf("iRetCode = %d \n",iRetCode);
if( iRetCode )
{
printf("DB2 Connection failure !\n");
return iRetCode;
}
else
{
printf("DB2 Connection success !\n");
}
db2_logout();
return 0;
}
//makefile
OBJS = demo
all:$(OBJS)
DB2INCPATH=/home/db2inst1/sqllib/include
DB2LIBPATH=/home/db2inst1/sqllib/lib
demo:demo.c db2dbproc.sqc
@db2 connect to test
@db2 prep db2dbproc.sqc
@gcc -I$(DB2INCPATH) -o demo demo.c db2dbproc.c -L$(DB2LIBPATH) -ldb2
@rm -rf db2dbproc.c
clean:
@ls | grep -v ^makefile$$ | grep -v [.]c$$ | grep -v [.]h$$ | grep -v [.]sqc$$ | grep -v [.]sql$$ | xargs rm -rf
[db2inst1@localhost DB2]$ ls
dbproc.sqc demo.c makefile
[db2inst1@localhost DB2]$ make
Database Connection Information
Database server = DB2/LINUX 9.7.1
SQL authorization ID = DB2INST1
Local database alias = TEST
LINE MESSAGES FOR dbproc.sqc
------ --------------------------------------------------------------------
SQL0060W The "C" precompiler is in progress.
SQL0091W Precompilation or binding was ended with "0"
errors and "0" warnings.
[db2inst1@localhost DB2]$ ls
dbproc.sqc demo demo.c makefile
[db2inst1@localhost DB2]$ ./demo
iRetCode = 0
DB2 Connection success !
[db2inst1@localhost DB2]$