本筆記接在上一篇 <<Linux下安裝DB2資料庫步驟>> ,學習過程中使用下載的官網DB2資料庫(免費版本)測試的,由於工作中DB2是用在項目中的,而且是收費的資料庫,故我只在項目編程中使用它(即公司的DB2資料庫收費版本),沒用來作為自己測試用。嘿嘿,所以下面的大部分操作主要來自下載的官網DB2資料庫(免費版本)。
相關閱讀:Linux下卸載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
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.查看錶的空間
[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 ~]$
10.顯示資料庫裡有哪些表
[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中還沒有表