DB2數(shù)據(jù)庫部分日常實用操作
1、Load 方法裝入數(shù)據(jù):
export to tempfile of del select * from TABLENAME where not 清理條件;
load from tempfile of del modified by delprioritychar replace into TABLENAME nonrecoverable;
說明:
在不相關(guān)的數(shù)據(jù)表export數(shù)據(jù)時,可以采取并發(fā)的形式,以提高效率;
TABLENAME指待清理table的名稱;
modified by delprioritychar防止數(shù)據(jù)庫記錄中存在換行符,導(dǎo)致數(shù)據(jù)無法裝入的情況;
replace into對現(xiàn)數(shù)據(jù)庫中的內(nèi)容進行替換,即將現(xiàn)行的數(shù)據(jù)記錄清理,替換為數(shù)據(jù)文件內(nèi)容;
nonrecoverable無日志方式裝入;
2、查找當(dāng)前的應(yīng)用:DB2 list application |grep BTPDBS;
3、刪除當(dāng)前正在使用的application:
db2 "force application (Id1,Id2,Id3)"
Id1,Id2,Id3 是List顯示的應(yīng)用號;
4、查看當(dāng)前應(yīng)用號的執(zhí)行狀態(tài):
db2 get snapshot for application agentid 299 |grep Row
5、查看數(shù)據(jù)庫參數(shù):
db2 get db cfg for //當(dāng)前數(shù)據(jù)庫可以省略
6、修改數(shù)據(jù)庫的Log數(shù)據(jù):
db2 update db cfg using <參數(shù)名> <參數(shù)值>
7、Db2Stop Force的用法:
在進行Bind的時候出現(xiàn)如下錯誤:
SQL0082CAn error has occurred which has terminated processing.
SQL0092NNo package was created because of previous errors.
SQL0091NBinding was ended with "3" errors and "0" warnings.
主要是表文件被加鎖,不能繼續(xù)使用;
在進行stop的時候報錯:db2stop
8/03/2005 21:46:530 0 SQL1025NThe database manager was not stopped because databases are still active.
SQL1025NThe database manager was not stopped because databases are still active.
需要使用如下命令可以解決這個問題: db2stop force
08/03/2005 21:47:49 0 0 SQL1064NDB2STOP processing was successful.
SQL1064NDB2STOP processing was successful.
然后啟動數(shù)據(jù)庫db2start,連接數(shù)據(jù)庫db2s后,重新進行bind即可。
8、緩沖池參數(shù)修改:
db2 alter buffERPool ibmdefaultbp size 10240
查看本表的數(shù)據(jù)內(nèi)容如下:db2 "select * from syscat.bufferpools";
9、DB2 日志處理:
DB2日志是以文件的形式存放在文件系統(tǒng)中,分為兩種模式:循環(huán)日志和歸檔日志。當(dāng)創(chuàng)建新數(shù)據(jù)庫時,日志的缺省模式是循環(huán)日志。在這種模式下,只能實現(xiàn)數(shù)據(jù)庫的脫機備份和恢復(fù)。如果要實現(xiàn)聯(lián)機備份和恢復(fù),必須設(shè)為歸檔日志模式。
目前在綜合業(yè)務(wù)系統(tǒng)中,設(shè)置的均是歸檔日志模式;其它系統(tǒng)(如事后監(jiān)督、經(jīng)營決策、中間業(yè)務(wù)等)一般都設(shè)置為循環(huán)日志模式。至于采用何種模式,可以通過修改數(shù)據(jù)庫配置參數(shù)(LOGRETAIN)來實現(xiàn): 歸檔日志模式:db2 update db cfg for using logretain on 注:改為on后,查看數(shù)據(jù)庫配置參數(shù)logretain的值時,實際顯示的是recovery。改變此參數(shù)后,再次連接數(shù)據(jù)庫會顯示數(shù)據(jù)庫處于備份暫掛(BACKUP PENDING)狀態(tài)。這時,需要做一次對數(shù)據(jù)庫的脫機備份(db2 backup db ),才能使數(shù)據(jù)庫狀態(tài)變?yōu)檎!?/p>
循環(huán)日志模式:db2 update db cfg for using logretain off
10、Db2 日志處理
必須按照以下正確的步驟進行操作:
要求必須使用DB2命令PRUNE進行清理,不建議使用rm命令刪除。
刪除前應(yīng)保證應(yīng)用已停止(即聯(lián)機已下來)。查看當(dāng)前使用的日志文件目錄及第一活動日志文件
用 “db2 get db cfg for 命令查看日志文件目錄(Path to log files)參數(shù),確定數(shù)據(jù)庫當(dāng)前使用的日志文件目錄。 例如:Path to log files = /db2log/,說明DB2日志存放目錄是/db2log
用 “db2 get db cfg for 命令查看第一活動日志文件(First active log file)參數(shù),該參數(shù)對應(yīng)的日志文件之前的日志文件均為歸檔日志文件,如果確認沒有用,可以刪除。 例如:First active log file = S0015913.LOG,說明當(dāng)前第一活動日志文件是S0015913.LOG。
備份好要刪除的歸檔日志
刪除歸檔日志 以應(yīng)用用戶(如BTP)登錄,執(zhí)行:
$ db2 connect to
$ db2 prune logfile prior to S???????.LOG
注:S???????.LOG為查看到的第一活動日志文件。此命令可以將當(dāng)前第一活動日志文件之前的歸檔日志文件全部刪除。
11、如何清理DB2diag.log文件
db2diag.log,是用來記錄DB2數(shù)據(jù)庫運行中的信息的文件。可以通過此文件,查看記錄的有關(guān)DB2數(shù)據(jù)庫詳細的錯誤信息。此文件也是不斷增大的,需要定期進行清理。
可以通過查看實例的配置參數(shù)DIAGPATH,來確定db2diag.log文件是放在哪個目錄下:db2 get dbm cfg 如果Diagnostic data directory path(DIAGPATH) = /home/db2inst1/sqllib/db2dump,則此文件是放在/home/db2inst1/sqllib/db2dump目錄下。當(dāng)文件系統(tǒng)/home的使用率達到80%-90%左右時,應(yīng)及時刪除db2diag.log文件。
請按以下正確步驟操作:確認應(yīng)用(如BTP)、DB2已經(jīng)停止。
將原db2diag.log文件備份到其它文件系統(tǒng)下。
刪除db2diag.log文件。刪除后,DB2會自動創(chuàng)建一個新的文件。
12、Load 操作
在進行l(wèi)oad的時候
db2 "load from aCMMst.txt of del modified by coldel| replace into acmmst nonrecoverable
由于數(shù)據(jù)不規(guī)范出現(xiàn)錯誤,強行中斷以后,進行操作的時候出現(xiàn)如下錯誤:
SQL0668NOperation not allowed for reason code "3" on table "BTP.ACMMST".
SQLSTATE=57016
此時,進行反方向操作即可:db2 "load from /dev/null of del terminate into acmmst nonrecoverable"。
如果沒有使用參數(shù)nonrecoverable,則會出現(xiàn)數(shù)據(jù)庫狀態(tài)不正確的情況,使用:
db2 list tableSAPces show detail 查看狀態(tài),如果不是正常狀態(tài),則脫機狀態(tài)進行備份即可。
兩個表文件之間UPDATE的方法:
db2 "update cdmcrd set offset = (select cdmlsl.offset from cdmlsl where cdmlsl.crdno=cdmcrd.crdno) where cdmcrd.crdno in (select cdmlsl.crdno from cdmlsl)
13、多字段條件查詢和修改
表A中的字段有actno, cnlno,bal,pwd;表B中的字段為Actno,Cnlno,TxnAmt;目的是將A表中的bal修改為B表中的TxnAmt,命令:
db2 "update A set bal=(select txnamt from B where actno=A.actno and cnlno=A.Cnlno) where A.actno||A.cnlno in (select Actno||cnlno from B );
14、多條件匹配查詢
查詢某個表中條件是B?AAA的記錄:
db2 "select * from A where actno like 'B_AAA%'".
查詢數(shù)據(jù)中存在某些字符的記錄:
db2 "select * from A where actno like '%-AAA%".
15、數(shù)據(jù)庫恢復(fù)的處理
進行數(shù)據(jù)庫恢復(fù)的時候使用以下的命令:
以下是引用片段:
restore db db1 to /tstdb2/catalog into db newlogpath /tstdb2/db2log buffer 2048 replace existing redirect parallelism 16; set tablespace containers for 1 using (path '/tstdb2/db2tmp'); set tablespace containers for 2 using (device '/dev/rtstcontlv00' 2621440, device '/dev/rtstcontlv01' 2621440, device '/dev/rtstcontlv02' 2621440, device '/dev/rtstcontlv03' 2621440 ) ; restore db db1 continue;
恢復(fù)完成以后執(zhí)行命令db2s時報如下的錯誤以下是引用片段:
P570:>db2s SQL1117N A connection to or activation of database "DB" cannot be made because of ROLL-FORWARD PENDING. SQLSTATE=57019 DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL1024N A database connection does not exist. SQLSTATE=08003
解決辦法如下:
以下是引用片段:
P570:>db2 rollforward db db to end of logs and complete Rollforward Status Input database alias = db Number of nodes have returned status = 1 Node number = 0 Rollforward status = not pending Next log file to be read = Log files processed = - Last committed transaction = 2005-11-20-10.59.23.000000 DB20000I The ROLLFORWARD command completed successfully.
;
相關(guān)文章:
1. Oracle與DB2數(shù)據(jù)類型的分類對應(yīng)說明2. DB2 V9.5工作負載管理之閾值(THRESHOLD)3. DB2數(shù)據(jù)庫設(shè)計:取得最佳性能的準(zhǔn)則(1)4. IBM DB2事件監(jiān)視器及explain plan的使用5. 帶你深入了解IBM DB2數(shù)據(jù)庫的備份與恢復(fù)6. 解決db2 codepage導(dǎo)致連接不上數(shù)據(jù)庫問題7. 快速解決DB2創(chuàng)建存儲過程時所遇到的錯誤8. 全面解析DB2性能調(diào)優(yōu)方面的二十個疑難問題9. 關(guān)于DB2數(shù)據(jù)庫的系統(tǒng)信息集成(1)10. DB2中游標(biāo)的使用方法以及存儲過程的寫法
