环境准备
1.新建表
[email protected]:~> db2 "create table tb1(id int,dt timestamp,name varchar(100))";
DB20000I The SQL command completed successfully.
2.插入数据
[email protected]:~> db2 "insert into tb1 values(1,current timestamp,‘Jack‘)";
DB20000I The SQL command completed successfully.
[email protected]:~> db2 "insert into tb1 values(2,current timestamp,‘Numy‘)";
DB20000I The SQL command completed successfully.
3.创建新表(为Import贮备)
[email protected]:~> 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中对不同的数据导入导出方式,支持不同的文件类型:
文件类型 | Import | Export | Load |
定界 | 支持 | 支持 | 支持 |
非定界 | 支持 | 不支持 | 支持 |
ixf | 支持 | 支持 | 支持 |
wsf工表表 | 支持 | 支持 | 不支持 |
关于3种导入导出操作进行简单的介绍:
export:导出数据,支持IXF,DEL或WSF
import:导入数据,可以向表中导入数据,支持上面提到的4种文件类型。
load:导入数据,功能和import基本相同。支持以上说的几种文件类型。
Export与Import注意事项
<1>不同字符集、字段含有TIMESTAMP格式
导出
[email protected]:~> 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
导入
[email protected]:~> 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 表名