首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> DB2 資料庫 >> DB2 教學

DB2 實戰必備:31 個常用操作技巧與指令速查表

     編輯:DB2 教學

節點與資料庫編目管理

#操作指令範例
1檢視本地節點目錄db2 list node directory
2編目 TCP/IP 節點db2 catalog tcpip node <node> remote <server> server <port> ostype <type>
3取消節點編目db2 uncatalog node <node>
4檢視系統資料庫目錄db2 list database directory
5檢視本地資料庫目錄db2 list database directory on <磁碟代號>
6編目資料庫db2 catalog database <db> as <alias> at node <node>
7取消資料庫編目db2 uncatalog database <alias>
8測試遠端連線db2 connect to <db> user <user> using <pwd>

提示:本地目錄有而系統目錄無的資料庫無法存取,可在控制中心右鍵選「新增」或「重新整理」加入。

Schema、Codepage 與遷移

#操作指令/說明
9設定當前 Schemaset schema = <schema名>(互動式或應用程式皆可;Dynamicrules BIND 封裝時無效)
10建庫時指定字集create database <db> using codeset <cs> territory <ter> 例:codeset IBM-437 territory US
環境變數設定 CodepageWin: 系統環境變數 DB2CODEPAGE=1386 或 CLI 下 db2set DB2CODEPAGE=1386(需重啟 DB2)
11低版本遷移高版本備份低版本資料 → 高版本 restore → db2 migrate database <db>

查詢與 DDL 技巧

#操作指令
12Schema/Table 名含引號時查詢db2 select * from "<schema>"."<table>"
13匯出表結構產生 DDLdb2look -d <db> -e -c -o <檔案>
14執行腳本檔db2 -tvf <檔案>
16查看 DB2 版本select * from sysibm.sysversions
17修改 VARCHAR 長度僅能增加:alter table <t> alter column <c> set data type varchar(<新長度>)
18檢視表結構describe table <t> 或 describe select * from <t>
19快速清空大表alter table <t> activate not logged initially with empty table
26多欄位 IN / NOT INwhere (colA, colB, colC) [not] in (values (vA1,vB1,vC1), (vA2,vB2,vC2), ...)

系統目錄查詢

#查詢內容SQL
20預存程序SELECT * FROM SYSCAT.PROCEDURES
21Check 約束SELECT * FROM SYSCAT.CHECKS WHERE TABNAME = '<t>'
22Referential Integrity 約束SELECT * FROM SYSCAT.REFERENCES WHERE TABNAME = '<t>'
23Bufferpool 狀態select * from SYSCAT.BUFFERPOOLS

配置參數管理

操作指令
檢視實例配置db2 get dbm cfg
修改實例配置db2 update dbm cfg using <參數> <新值>
檢視資料庫配置db2 get db cfg for <db>
修改資料庫配置db2 update db cfg for <db> using <參數> <新值>

Bufferpool 操作

  • 建立:create bufferpool <bp> size <頁數> [pagesize 4096] {[not] EXTENDED STORAGE}
  • 修改:alter bufferpool <bp> size <頁數> {[not] EXTENDED STORAGE}
  • 刪除:drop bufferpool <bp>
  • Size = -1 時由 buffpage 參數決定;buffpage 僅對 size=-1 的 bufferpool 生效。

應用監控與一致性檢查

#操作指令
27檢視目前連線應用db2 list application [show detail]
28資料庫一致性檢查db2dart <db> /DB(/DB 表全庫檢查)
29SQL 效能測試db2batch -d <db> -f <sql檔> [-a user/pwd] [-r <輸出檔>]

資料匯出入

  • Export:export to <檔案> of <格式> select * from <table> 例:export to c:\user.ixf of ixf select * from user
  • Import:import from <檔案> of <格式> [create/insert into/update] <table> 可自動建表或寫入既有表。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved