DB2必須了解的常用命令及技巧(轉)


1.db2里面的字符串連接可用"||"這個進行連接

2.如何快速刪除大批量的數據表(test為數據庫表)

最常用也是最多人用的語句:delete from test,但這種做法,效率比較低,花費時間太長,因為在刪除數據時,要記數據庫日志。

import from /dev/null of del replace into test//先清空,再導入數據(由於導入的文件為空,故相當於清空表數據),這種刪除的速度較快

ALTER TABLE test activate NOT LOGGED initially WITH EMPTY TABLE ;刪除表中的數據,不記日志,這種處理最快

3.導入、導出數據,支持的文件有ixf,del文件

db2 "export to fileName.del of del select * from tableName" //db2中把表中的數據導入到文件

db2 "import from fileName.del of del insert into tableName " //db2中把文件中的數據導入到表

4.DB2中檢查表是否已存在

select * from "SYSIBM"."SYSTABLES" where lower(name) ='afa_maintransdtl'

5.處理db2鎖表問題:

db2 “connect to afa”

db2 “get snapshot for locks on 實例名”

db2 “terminate”

然后查看相關信息,找到被鎖定的表,執行以下語句:

db2 “force application(application handle) 注:application handle對應的是一個整數

6.在服務器上創建存儲過程時:

應為存儲過程指定特定的換行符,然后執行下面的語句:

db2 -td@ -vf fileName.sql (其中@為存儲過程中指定的換行符)

7. 查看索引是否起作用

runstats on table afa.yj_jywtk with distribution and detailed indexes all;

8.截取數據庫快照:

db2 "connect to 實例名"

db2 "update monitor switches using lock ON sort ON bufferpool ON uow ON table ON statement ON"

db2 "get snapshot for all on 實例名"

9. 查看存儲過程

select procname,text from sysibm.sysprocedures;

10.list tables 查看數據庫表

11.查找字符在字串中的位置

locate('y','xyz')

查找'y'在'xyz'中的位置。

12. 計算兩個日期的相差天數

days(date('2007-03-01'))-days(date('2007-02-28'))

days 返回的是從 0001-01-01 開始計算的天數

13. 為一個用戶訪問另一個實例創建表映射

create alias tableName for 實例名. tableName

14.如何查看表結構

describe table tableName

or

describe select * from table_name

15.如何重新啟動數據庫?

restart database database_name

16.如何激活數據庫?

activate database database_name

17.如何停止數據庫?

deactivate database database_name

18.如何重命名表?

rename table_old to table_new

19.如何定義序列?

create sequence orderseq start with 1 increment by 1 no maxvalue no cycle cache24

20.如何查看一個表的索引

describe indexes for table tableName show detail


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM