Oracle-数据导出和导入

对数据库进行逻辑备份或数据转储,也就是对数据库实时导入、导出操作时,既可以使用常规的EXP/IMP客户端程序,也可以使用数据泵技术:IMPDP/EXPDP

使用数据泵导出或导入的优点:

1.数据泵导出与导入可以实现逻辑备份和逻辑恢复。

2.数据泵导出和导入可以在数据库用户之间移动对象

3.使用数据泵导入可以再数据库之间移动对象

4.数据泵可以实现表空间的转移,即将一个数据库的表空间转移到另一个数据库中

较之EMP/IMP速度较快

  EXPDP可以将数据库对象的元数据或数据导出的转储文件中,EXPDP可以导出表、用户模式、表空间和全数据库。EXPDP是服务器端工具,这意味着该工具只能在Oracle服务器端使用,而不能再Oracle客户端使用。

需要注意:EXPDP工具只能将导出的转储文件存放在DIRECTORY对象对应的OS目录中,而不能直接指定转储文件所在的OS目录

如:创建一个DIRECTORY对象,并为SCOTT用户授予使用该目录的权限

create directory dump_dir as 'd:orcl';    --此目录必须设置utl_file_dire

  grant read,write on directory dump_dir to scott;

导出表

  将一个或多个表的结构及其数据存储到转储文件中(其他模式,,有EXP_FULL_DATABASE|DBA角色)

在导出表中,每次只能导出一个模式中的表

如:导出SCOTT模式中的dept和emp表

expdp scott/tiger directory=dump_dir dumpfile=tab.dmp tables=emp,dept;

导出模式

  是指将一个或多个模式中的所有对象结构及数据存储到转储文件中(其他模式,,有EXP_FULL_DATABASE|DBA角色)。

如:导出scott和hr模式中的所有对象

expdp system/xcn258 directory=dump_dir dumpfile=schemas.dmp schemas=scott,hr

导出表空间

  导出表空间是指将一个或多个表空间中的所有对象及数据存储到转储文件(其他模式,,有EXP_FULL_DATABASE|DBA角色)。

expdp system/xcn258 directory=dump_dir dumpfile=tablespace.dmp tablespaces=tbsp_1;

导出全部数据库

  是指将数据库中的所有对象及数据存储到转储文件中(其他模式,,有EXP_FULL_DATABASE|DBA角色)。

需要注意的是,导出数据库时,不会导出SYS,ORDSYS,ORDPLUGINS,CTXSYS,MDSYS,LBACSYS和XDB等模式中的对象

如:导出整个数据库

expdp system/xcn258 directory=dump_dir dumpfile=fulldatabase.dmp full=y;

EXPDP命令参数

  在调用EXPDP工具导出数据时,可以为该工具附加多个命令行参数

--content

 =all| data_only| metadata_only

--query

 ='where...'

用来过滤导出数据的where

--directory

用来指定转储文件和日志文件所在的目录

--dumpfile

用于指定转储文件的名称默认名称expdat.dmp

--full

用于指定数据库模式导出(y|n 默认是n)

  数据库导出时,数据库用户必须具有EXP_FULL_DATABASE角色或DBA角色

--logfile

  该参数用于指定导出日志文件的名称,默认名称为export.log

--status

  该参数用于指定显示导出作业进程的详细状态,默认值为0

--tables

  指定表导出

--tablespaces

  指定导出的表空间列表

IMPDP导入数据

导入表 其他用户导入表,则要求该用户必须具有IMP_FULL_DATABASE或DBA角色

将表dept,emp导入到system模式

impdp system/xcn258 directory=dump_dir dumpfile=tab.dmp tables=scott.dept,scott.emp remap_schema=scott.system

导入模式 其他用户导入模式,则要求该用户必须具有IMP_FULL_DATABASE或DBA角色

impdp system/xcn258 directory=dump_dir dumpfile=schema.dmp schemas=scott remap_schema=scott.system;

导入表空间

导入全数据库

IMPDP命令参数

 --remap_schema

该参数用于将源模式中的所有对象转载到目标模式

remap_schema=sorce_schema:target_schema

--remap_tablespace

用于指定导入时更改表空间名称

remap_tablespace=source_tablespace:target_tablespace

--sqlfile

可以从dmp文件中提取DDL语句

--table_exists_action

该参数指定当表已经存在时导入作业要执行的操作

=skip| append| truncate| replace    默认是skip

--transport_datafiles

该参数用于指定移动空间时要被导入到目标数据库的数据文件

SQL*Loader工具

  可以实现将外部数据或其他数据库中的数据添加到Oracle数据库中

根据数据的存储格式,SQL*Loader所使用的数据文件可以分为两种,固定格式存储的数据和自由格式存储的数据。

固定格式:按一定规律排序,控制文件通过固定长度将数据分割

自由格式:由规定的分隔符来区分不同字段的数据

自由格式的例子:

创建一个student表,用以存储要加载的数据

create table student

(stuno number(4),

stuname varchar2(20),

sex varchar2(4),

old number(4)

);

制作一份student.txt,存储到student.txt

 编辑控制文件student.ctl,确定加载方式

load data
infile 'd:datastudent.txt'
into table student
(stuno position(01:04) integer external,
stuname position(09:12) char,
sex position(13:14) char,
old position(17:19) integer external
)

sqlldr system/xcn258 control=d:datastudent.ctl log=d:datastu_log

---出现错误,无法打开文件

固定格式的例子:

先在EXCEL中,建立persons.csv

创建一个与excel表中对应的表persons

create table persons

(code  number(4),

name varchar2(20),

sex varchar2(4),

old number(4)

);

编辑一个控制文件persons.ctl

load data
infile 'd:datapersons.csv'
append into table persons
fields terminated by ','
(code,name,sex,old)

   sqlldr system/xcn258 control=d:datapersons.ctl

---出现错误,无法打开文件

原文地址:https://www.cnblogs.com/xcnblog3035/p/5296693.html