DB2資料庫基本操作,db2基本操作

來源:互聯網
上載者:User

DB2資料庫基本操作,db2基本操作
啟動DB2服務:db2start
關閉DB2服務: db2stop




一、載入資料:
1、 以預設分隔符號載入,預設為“,”號
    db2 "import from btpoper.txt of del insert into btpoper"
2、 以指定分隔字元“|”載入
    db2 "import from btpoper.txt of del modified by coldel| insert into btpoper"


二、卸載資料:
1、 卸載一個表中全部資料
    db2 "export to btpoper.txt of del select * from btpoper"
    db2 "export to btpoper.txt of del modified by coldel| select * from btpoper"


2、 帶條件卸載一個表中資料
    db2 "export to btpoper.txt of del select * from btpoper where brhid='907020000'"
    db2 "export to cmmcode.txt of del select * from cmmcode where codtp='01'"
    db2 "export to cmmcode.txt of del modified by coldel| select * from cmmcode where codtp='01'"


三、查詢資料結構及資料:
    db2 "select * from btpoper"
    db2 "select * from btpoper where brhid='907020000' and oprid='0001'"
    db2 "select oprid,oprnm,brhid,passwd from btpoper"


四、刪除表中資料:
    db2 "delete from btpoper"
    db2 "delete from btpoper where brhid='907020000' or brhid='907010000'"


五、修改表中資料:
    db2 "update svmmst set prtlines=0 where brhid='907010000' and jobtp='02'"
    db2 "update svmmst set prtlines=0 where jobtp='02' or jobtp='03'"


六、聯結資料庫
    db2 connect to btpdbs


七、清除資料庫聯結
    db2 connect reset    斷開資料庫連接
    db2 terminate        斷開資料庫連接
    db2 force applications all    斷開所有資料庫連接


八、備份資料庫
1、 db2 backup db btpdbs
2、 db2move btpdbs export
    db2look -d btpdbs -e -x [-a] -o crttbl.sql


九、恢複資料庫
1、 db2 restore db btpdbs without rolling forward
2、 db2 -tvf crtdb.sql
        crtdb.sql檔案內容:create db btpdbs on /db2catalog
    db2 -stvf crttbl.sql
    db2move btpdbs import


十、DB2協助命令:
    db2 ?
    db2 ? restroe
    db2 ? sqlcode (例:db2 ? sql0803) 註:code必須為4位元,不夠4位,前面補0




十一、bind命令:將應用程式與資料庫作一捆綁,每次恢複資料庫後,建議都要做一次bind
   (1) db2 bind br8200.bnd
   (2) /btp/bin/bndall /btp/bnd
       /btp/bin/bndall /btp/tran/bnd


十二、查看資料庫參數:
      db2 get dbm cfg
      db2 get db cfg for btpdbs


十三、修改資料庫參數:
      db2 update db cfg for btpdbs using LOGBUFSZ 20
      db2 update db cfg for btpdbs using LOGFILSIZ 5120
      改完後,應執行以下命令使其生效:
      db2 stop
      db2 start




補充:


   db2 set schema btp    修改當前模式為"btp"


   db2 list tablespaces show detail     查看當前資料庫資料表空間分配狀況
   db2 list tablespace containers for 2  show detail 查看tablespace id=2使用容器所在目錄
   db2 list application
   db2 list db directory               列出所有資料庫
   db2 list active databases           列出所有活動的資料庫
   db2 list tables for all             列出當前資料庫下所有的表
   db2 list tables for schema btp      列出當前資料庫中schema為btp的表
   db2 list tablespaces show detail    顯示資料庫空間使用方式
   db2 list packages for all




   db2 "import from tab76.ixf of ixf commitcount 5000 insert into achact"


   db2 "create table achact_t like achact"
  
   db2 "rename table achact_t to achact"
  
   db2 "insert into achact_t select * from achact where txndt>=(select lstpgdt from
      acmact where actno=achact.actno)"


  


   db2 get snapshot for dynaimic sql on jining


刪除一個執行個體:
   # cd /usr/lpp/db2_07_01/instance
   # ./db2idrop InstName


列出所有DB2執行個體:
   # cd /usr/lpp/db2_07_01/bin
   # ./db2ilist


為資料庫建立編目
  $ db2 catalog db btpdbs on /db2catalog
取消已編目的資料庫btpdbs
  $ db2 uncatalog db btpdbs
            
查看版本
   # db2level


顯示當前資料庫管理執行個體
   $ db2 get instance


設定執行個體系統啟動時是否自動啟動。
   $ db2iauto -on     自動啟動
   $ db2iauto -off    不自動啟動


資料庫最佳化命令:
   reorg、runstats
   當資料庫經過一段時間使用,資料空間會變得越來越龐大。一些delete掉
   的資料仍存放在資料庫中,佔用資料空間,影響系統效能。因此需要定期
   運行reorg、runstats命令,清除已delete的資料,最佳化資料結構。
   db2 reorg table 表名
   db2 runstats on table 表名 with distribution and indexes all
   因為要最佳化的表比較多,所以在/btp/bin目錄下提供了一個sh程式runsall,
   可在當天業務結束後,運行runsall,對資料庫進行最佳化   


DB2資料庫的匯入方法
關鍵字: db2 db2move


1、在新的db2中匯入資料庫
以徐礦的資料庫為例,匯出的資料庫檔案為:ncdata.tar.gz和iufodata.tar.gz,解壓後放到ncdata和iufodata目錄下
1.1  建立表結構
 在ncdata和iufodata檔案夾下有ncdb.sql和iufodb.sql,裡面是建庫用的sql指令碼....
 需要注意的是在執行指令碼之前需要檢查一下是不是需要修改資料表空間的名稱...  
 否則執行:db2cmd db2 -tvf  ncdb.sql;db2 -tvf iufodb.sql
1.2  匯入資料
     使用db2move  ncdb load將資料匯入到對應表中


1.3  檢查鎖表記錄
     db2 Select tabname from syscat.tables where status='C'  // 尋找出被鎖定的表
 然後將查詢出的表名每條都執行一下語句:
 db2 connect to <資料庫名> user <使用者> using <密碼>
 SET INTEGRITY FOR  TABLNAME ALL IMMEDIATE UNCHECKED
1.4  重建表結構
 db2 "reorgchk update statistics on table all"




2、更新現有的資料庫


db2cmd db2move ncdb import -io replace_create








---------------------------------------------------------------------
---------------------------------------------------------------------

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.