昨天,接到用户请求,能否将cache库导出excel文档、txt文档的数据导入到oracle库,之后将oracle导入的数据导出dmp文件,以前操作也做过,时间长了,也忘记了,今天我们重新来做一遍。
本次选用plsql developer工具来,版本:PL/SQL Developer Version 11.0.3.1770
以上描述其实就是将外部表如何导入到oracle数据库中,实现该目标有多种方法:
1、使用 PL/SQL Developer工具将execl、txt源文件导入到数据库,最后通过exp导出的方式导出外部表(dmp格式)
2、采用oracle自带的sql load方式导入外部表,在通过exp方式导出外部表数据
第一种 PL/SQL Developer:
(Oracle Release 11.2.0.4.0)
excel导入到oracle
1、编辑好excel数据
2、配置好本地odbc
3、配置plsql中工具,odbc导入器
4、导入数据
SQL> select count(*) from it;
COUNT(*)
----------
25
txt文件导入到oracle
1、编辑txt文件
王五,16,安徽省合肥市
王六,16,安徽省合肥市
王七,16,安徽省合肥市
王八,16,安徽省合肥市
王九,16,安徽省合肥市
王十,16,安徽省合肥市
王十一,16,安徽省合肥市
2、使用plsql导入oracle
工具-文本导入器
3、设置字段对应关系
4、导入数据
SQL> select * from it;
NAME AGE ADDRESS
-------------------- ---------- ----------------------------------------------------------------------------------------------------
王五 16 安徽省合肥市
王六 16 安徽省合肥市
王七 16 安徽省合肥市
王八 16 安徽省合肥市
王九 16 安徽省合肥市
王十 16 安徽省合肥市
王十一 16 安徽省合肥市
将导入到数据的表导出dmp文件
[oracle@db1 ~]$ exp scott/oracle@orcl file=scott_it.dmp log=scott_it.log tables=it
Export: Release 11.2.0.4.0 - Production on 星期三 8月 17 15:56:00 2022
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table IT 7 rows exported
Export terminated successfully without warnings.
[oracle@db1 ~]$ ls -lrth scott*
-rw-r--r-- 1 oracle oinstall 427 Aug 17 15:56 scott_it.log
-rw-r--r-- 1 oracle oinstall 16K Aug 17 15:56 scott_it.dmp
第二种,使用sqlload方式导入数据库
excel数据导入数据库:
1、建表
create table it (name varchar2(20),age number(3),adress varchar2(20));
2、建立.ctl文件
[oracle@db1 ~]$ cat import.ctl
load data
infile 'it12.csv'
append into table "IT"
fields terminated by ','
(
NAME
,
AGE
,
ADDRESS
)
3、上传 it12.csv文件(excel文件保存是采用另存为带分隔符的.csv文件)
4、通过sqlload命令导入数据库
sqlldr userid=scott/oracle control=/home/oracle/import.ctl
[oracle@db1 ~]$ sqlldr userid=scott/oracle control=/home/oracle/import.ctl
SQL*Loader: Release 11.2.0.4.0 - Production on 星期三 8月 17 19:25:02 2022
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 48
数据导入完成,检查数据完整性。
SQL> select count(*) from it;
COUNT(*)
----------
48
txt数据导入数据库:
1、建表
create table it (name varchar2(20),age number(3),adress varchar2(20));
2、建立.ctl文件
[oracle@db1 ~]$ cat import.ctl
load data
infile 'data.txt'
append into table "IT"
fields terminated by ','
(
NAME
,
AGE
,
ADDRESS
)
3、文本文件格式数据
[oracle@db1 ~]$ cat data.txt
王五,16,安徽省合肥市
王六,16,安徽省合肥市
王七,16,安徽省合肥市
王八,16,安徽省合肥市
王九,16,安徽省合肥市
王十,16,安徽省合肥市
王十一,16,安徽省合肥市
4、通过sqlload工具导入数据到表
[oracle@db1 ~]$ vi import.ctl
[oracle@db1 ~]$ sqlldr userid=scott/oracle control=/home/oracle/import.ctl
SQL*Loader: Release 11.2.0.4.0 - Production on 星期三 8月 17 19:43:23 2022
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 6
Commit point reached - logical record count 7
5、检查数据
SQL> select count(*) from it;
COUNT(*)
----------
7
备注:当我们导入excel或txt文件时,文件大小过大,如上百兆甚至更大时,在导入到目标数据库时,新建用户或已有用户,
新建表空间,设置为默认表空间等,以此来保证导入的数据表不影响数据库的正常运行。
当我们导出表时,使用exp或expdp方式导出数据库的某张表就可以了。
总结:外部表导入是否成功,不管是plsql或是sqlload方式,难点集中在数据表格式,字段长度及类型设置上,如果上面都设置
没有问题,如果导入出现乱码的情况,此时需要考虑字符集的问题了。此处列入几个例子,属于抛转引玉了,实际情况可能出现
其他问题,针对出现的问题,做具体的解决了。
Yicheng16
22.08.16
-- The End --