
Oracle 的导出导入是一个很常用的迁移工具。 从Oracle 10g开始,Oracle 推出了数据泵(expdp/impdp)。它可以通过使用并行,从而在效率上要比exp/imp 要高。
逻辑备份工具---数据泵
使用专用api导入导出数据,速度快
expdp只能用于服务器端,不能用户客户端
工具目录:$ORACLE_HOME/bin
使用数据泵之前必须创建目录对象 ,导入导出都在目录对象里
数据泵优点:
exclude ,include 和content 参数用于选择细粒度级对象和数据
version参数指定要移动的对象的数据库版本
parallel参数指定代表导出作业运行的活动执行服务器的最大线程数
estimate_only参数可估计导出作业占用的空间量(实际上并不执行导出)
使用网络模式可从远程数据库直接导出到转存文件集
在导入过程中,可以更改目标数据文件名、方案和表空间
使用compression参数可以指示是否应在导出转存文件中压缩元数据,以便占用更少的磁盘空间
此外还允许您指定在执行数据泵导出时,要从源数据库取样和卸载的数据百分比
1. 创建data pump 使用的一个表
conn system/oracle
create table dp_test as select * from all_users;
select count(*) from dp_test;
2. 创建oracle目录对象
mkdir u01/app/oracle/dp_dir
$ sqlplus system/oracle
SQL> create directory dp_dir as '/u01/app/oracle/dp_dir';
3. 授权目录对象
SQL> grant all on directory dp_dir to public;
可以通过dba_directories 查看
4. 导出
a. 导出dp_test表数据
expdp system/oracle dumpfile=expdp01.dmp directory=dp_dir tables=system.dp_test logfile=expdp01.log
b. 导出方案schemas(需要有DATAPUMP_EXP_FULL_DATABASE权限)
expdp system/oracle directory=dp_dir dumpfile=scott.dmp schemas=scott
导入方案
impdp system/oracle directory=dp_dir dumpfile=scott.dmp schemas=scott
c. 导出表空间(导出所有的表和表依赖的对象)
expdp system/oracle directory=dump_dir dumpfile=users.dmp tablespaces=users
到日表空间,导入前,必须手工去建立表空间
impdp system/oracle directory=dump_dir dumpfile=users.dmp tablespaces=users
5. 导入全库
expdp system/oracle directory=dump_dir dumpfile=full.dmp full=y
6. 并行导出,提高效率
设置parallel 可以并行导出,导出每个线程创建一个单独的导出文件,因此选项应该拥有和并行线程一样多的文件
通配符 %U 文件格式full_%U.dmp(full_01,full_02,...)
如:expdp system/oracle directory=dump_dir dumpfile=users_%U.dmp tablespaces=users parallel=3
并行是3 则会生成3个文件
Dump file set for SYSTEM.SYS_EXPORT_TABLESPACE_01 is:
/u01/backup/users_01.dmp
/u01/backup/users_02.dmp
/u01/backup/users_03.dmp
记住当我们在导入的时候,你有几个文件你才用并行度为几。这里导入users表空间有3个文件,所以导入的时候并行度设置为3 。

1. 导出和导入一张表
expdp system/oracle directory=dp_dir dumpfile=emp.dmp tables=scott.emp
drop table scott.emp purge ;
impdp scott/oracle directory=backup dumpfile=emp.dmp
2. 导出多张表,一次导入一张表
expdp scott/oracle directory=dump_dir dumpfile=ed.dmp tables=scott.emp,scott.dept
SQL> drop table scott.emp purge;
SQL> drop table scott.dept purge;
恢复:
先恢复dept表
impdp scott/oracle directory=dump_dir dumpfile=ed.dmp tables=scott.dept
再恢复emp
impdp scott/oracle directory=dump_dir dumpfile=ed.dmp tables=scott.emp
3. 只删除scott.emp的数据,然后通过emp.dmp还原数据
truncate table scott.emp ;
SQL> select * from scott.emp ;
no rows selected
恢复:表是存在的,只是没有数据
impdp scott/oracle DIRECTORY=dp_dir DUMPFILE=emp.dmp CONTENT=DATA_ONLY
SQL> select count(*) from scott.emp ;
COUNT(*)
----------
14
4. 删除表
drop table scott.emp purge;
分步操作:先导入元数据,再导入数据
先导入表结构,再导入数据
先导入表结构信息
impdp scott/oracle DIRECTORY=dp_dir DUMPFILE=emp.dmp CONTENT=METADATA_ONLY
SQL> select * from scott.emp ;
no rows selected
再导入表的数据
impdp scott/oracle DIRECTORY=dp_dir DUMPFILE=emp.dmp CONTENT=DATA_ONLY

1. 将emp表从scott用户导入到system用户下
impdp system/oracle directory=dp_dir dumpfile=emp.dmp tables=scott.emp remap_schema=scott:system
提示:外键建立不成功
解决办法:先导入主键表,再导入外键表
或者不建立外键,忽略约束错误
impdp system/oracle directory=dump_dir dumpfile=emp.dmp tables=scott.emp remap_schema=scott:system DATA_OPTIONS=SKIP_CONSTRAINT_ERRORS
2. 如果只导入表结构不导入数据
如:把scott.emp表的导入到hr用户下,只导入表结构(索引约束都在)
impdp system/oracle DIRECTORY=backup DUMPFILE=emp.dmp REMAP_SCHEMA=scott:hr CONTENT=METADATA_ONLY
3. 将soctt方案导入到system用户下
expdp scott/oracle directory=dump_dir dumpfile=scott.dmp schemas=scott
impdp system/oracle directory=dump_dir dumpfile=scott.dmp remap_schema=scott:system
4. 并行导入
create tablespace test datafile '+data' size 20M;
create table scott.test tablespace test as select * from scott.emp ;
create table hr.test2 tablespace test as select * from scott.emp ;
create table scott.biga tablespace test as select * from all_objects;
导出
expdp system/oracle directory=dump_dir dumpfile=ut_%U.dmp tablespaces=users,test parallel=2
Dump file set for SYSTEM.SYS_EXPORT_TABLESPACE_01 is:
/u01/backup/ut_01.dmp
/u01/backup/ut_02.dmp
导入
impdp system/oracle directory=dump_dir dumpfile=ut_%U.dmp tablespaces= test parallel=2





