1. 程式人生 > >【DB2】db2命令Export與Import

【DB2】db2命令Export與Import

phrase bsp log 導入數據 process border 比較 字符集 port

環境準備

1.新建表

qinys@Linux:~> db2 "create table tb1(id int,dt timestamp,name varchar(100))";
DB20000I The SQL command completed successfully.

2.插入數據

qinys@Linux:~> db2 "insert into tb1 values(1,current timestamp,‘Jack‘)";
DB20000I The SQL command completed successfully.
qinys@Linux:~> db2 "insert into tb1 values(2,current timestamp,‘Numy‘)";
DB20000I The SQL command completed successfully.

3.創建新表(為Import貯備)

qinys@Linux:~> db2 "create table tb1_tmp like tb1";
DB20000I The SQL command completed successfully.


DB2中的數據導入導出分別為:Import與Export

DEL:界定的ASCII文件,行分隔符與列分隔符將數據分開

ASC:定長的ASCII文件,行按照行分隔符分開,列定長

PC/IXF:只能用在DB2之間導數據,根據類型數字值被打包成十進制或者二進制,字符被保存為ASCII,只保存變量已經使用的長度,文件中包括表的定義和表的數據

WSF:工作表方式導入導出,這種格式的文件類型用的比較少

DB2中對不同的數據導入導出方式,支持不同的文件類型:

文件類型ImportExport Load
定界 支持 支持 支持
非定界 支持 不支持 支持
ixf 支持 支持 支持

wsf工表表

支持 支持 不支持


關於3種導入導出操作進行簡單的介紹:
export:導出數據,支持IXF,DEL或WSF
import:導入數據,可以向表中導入數據,支持上面提到的4種文件類型。
load:導入數據,功能和import基本相同。支持以上說的幾種文件類型。

Export與Import註意事項

<1>不同字符集、字段含有TIMESTAMP格式

導出

qinys@Linux:~> db2 "export to Exp.dat of del MODIFIED BY CODEPAGE=1386 TIMESTAMPFORMAT=\"yyyy-mm-dd hh:mm:ss tt\" SELECT * FROM tb1
" SQL3104N The Export utility is beginning to export data to file "Exp.dat". SQL3105N The Export utility has finished exporting "2" rows. Number of rows exported: 2


CODEPAGE=1386 是指在數據導出時,做一個數據庫代碼頁的轉換

1208→GBK

1386→UTF-8

導入

qinys@Linux:~> db2 "import from Exp.dat of del MODIFIED BY CODEPAGE=1386 TIMESTAMPFORMAT=\"yyyy-mm-dd hh:mm:ss tt\"  INSERT INTO tb1_tmp";
SQL3109N  The utility is beginning to load data from file "Exp.dat".

SQL3110N  The utility has completed processing.  "2" rows were read from the
input file.

SQL3221W  ...Begin COMMIT WORK. Input Record Count = "2".

SQL3222W  ...COMMIT of any database changes was successful.

SQL3149N  "2" rows were processed from the input file.  "2" rows were
successfully inserted into the table.  "0" rows were rejected.


Number of rows read         = 2
Number of rows skipped      = 0
Number of rows inserted     = 2
Number of rows updated      = 0
Number of rows rejected     = 0
Number of rows committed    = 2

Import導入大量數


db2 import from 數據文件名 of ixf modified by compound=100 insert into 表名

上面的命令中IMPORT會在每100條記錄而不是每條記錄插入後等待返回的SQL執行結果。

設置commitcount 參數加快導入

db2 import from 數據文件名 of ixf modified by compound=100 commitcount 10000 insert into 表名

【DB2】db2命令Export與Import