1. 啟動數據庫
db2start
2. 停止數據庫
db2stop
3. 連接數據庫
db2 connect to databasename user db2 using pwd
4. 獲得數據庫管理程序配置
db2 get dbm cfg
5. 更改數據庫管理程序配置
db2 update dbm cfg using 參數名 參數值
6. 獲得數據庫的配置
db2 connect to databasename user db2 using pwd
db2 get db cfg for databasename
7. 更改數據庫的配置
db2 connect to databasename user db2 using pwd
db2 update db cfg for databasename using 參數名 參數值
8. 關閉所有應用連接
db2 force application all
db2 force application ID1,ID2,,,Idn MODE ASYNC
(db2 list application for db databasename show detail)
9. 備份數據庫
db2 force application all
db2 backup db databasename to d:
(db2 initialize tape on \\.\tape0)
(db2 rewind tape on \\.\tape0)
db2 backup db databasename to \\.\tape0
10. 恢復數據庫
db2 restore db databasename from d: to d:
db2 restore db databasename from \\.\tape0 to d:
11. 綁定存儲過程
db2 connect to databasename user db2 using pwd
db2 bind c:\dfplus.bnd
拷貝存儲過程到服務器上的C:\sqllib\function目錄中
12. 重組表
db2 connect to databasename user db2 using pwd
db2 reorg table ydd
db2 runstats on table ydd with distribution and indexes all
13. 導出表數據
db2 export to c:\dftz.txt of del select * from dftz
db2 export to c:\dftz.ixf of ixf select * from dftz
14. 導入表數據
import from c:\123.txt of del insert into ylbx.czyxx
db2 import to c:\dftz.txt of del commitcount 5000 messages c:\dftz.msg insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 messages c:\dftz.msg insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 insert_update into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace into dftz
db2 import to c:\dftz.ixf of ixf commitcount 5000 create into dftz (僅IXF)
db2 import to c:\dftz.ixf of ixf commitcount 5000 replace_create into dftz (僅IXF)
15. 執行一個批處理文件
db2 -tf 批處理文件名
(默認文件中每一條命令用 ;結束)
16. 自動生成批處理文件
建文本文件:temp.sql,內容如下:
select 'runstats on table DB2.' || tabname || ' with distribution and detailed indexes all;' from syscat.tables where tabschema='DB2' and type='T';
db2 -tf temp.sql>runstats.sql
17. 自動生成建表(視圖)語句
在服務器上:C:\sqllib\misc目錄中
db2 connect to databasename user db2 using pwd
db2look -d databasename -u db2 -e -p -c c:\databasename .txt
18. 授權命令
grant dbadm on database to user bb
19. 列出所有的系統表
list tables for system
20. 查看表結構
db2 describe select * from user.tables
db2 describe table user.tables
21. Catalog:編目數據庫或節點
db2 catalog tcpip node nodename remote ip server port
db2 catalog db dbname as dbalias at node nodename
22. Uncatalog:功能與上相反,刪除已經編目的數據庫或節點
注:執行完catalog或者uncatalog后,需要執行terminate,實現刷新目錄列表的作用。Terminate的功能是終止clp的后臺進程,在此clp下的連接將全部斷開,同時本連接下做的數據庫更改做一個內部提交。
23. Attach:
建立一個到數據庫實例的連接,執行實例級別的命令,比如create database,force applications等,需要實例級的用戶權限。
24. List:
顯示一些數據庫對象或者設置的相關信息
25. Load:導入數據
注意自動生成列數據導入問題:需要加MODIFIED BY GENERATEDMISSING(可空下保持空), GENERATEDIGNORE(非空將添入默認,可空將保持空狀態), GENERATEDOVERRIDELOAD(非空下導入原來表中的值,比較有用)