LINUX下用数据泵导入导出(IMPDP、EXPDP)

expdp和impdp

expdp
假设a用户的默认表空间是a,导出用户a所有数据:

如果是多实例

需要在命令行或终端手工指定实例

set ORACLE_SID=实例名 否则回报ORA-12560: TNS: 协议适配器错误

SQL> conn / as sysdba
SQL> create directory dir as '/home/oracle/';
SQL> grant read,write on directory dir to system;

$expdp system/oracle directory=dir dumpfile=data.dmp schemas=a logfile=data.log

impdp
导入a用户所有数据到b,并且转换表空间a为b:

SQL> conn sys / as sysdba
SQL> create directory dir as '/home/oracle/';
SQL> grant read,write on directory dir to system;

$impdp system/oracle directory=dir dumpfile=data.dmp remap_tablespace=a:b remap_schema=a:b logfile=data.log

解释:

remap_schema=a:b 将数据的schema从a 转换为b

remap_tablespace=a:b 将数据的tablespace 从a 转换为b

1.导入的数据库中已经有相同的用户名和老旧的表
可以在后边加上参数table_exists_action=replace

2.导入新的用户中
需要加入参数remap_schame=原用户名:新用户名
例如
impdp estate/estate@orcl schemas=estate directory=zhxt dumpfile=ESTATE20160812LAOLING.DMP logfile=estate.log table_exists_action=replace remap_schame=原用户名:新用户名

注意:如果oracle是10g的,要加参数 EXCLUDE=TABLE_STATISTICS 选项将 table_statistics 对象过滤。 否则会出现数据泵导入中table_statistics长时间等待、用impdp 导入,检查 table_statistics 时等待了N长时间

总结:执行impdp时无需创建b用户,在导入时会自动创建并改名用户a为b(拥有a的所有权限等),自动设置默认表空间为转换后的表空间b。如果有多个表空间需要转换,则使用多个remap_tablespace=源:目标字段。

--21.1 Data Pump工具
Data Pump
从oracledatabase 10g开始引入了data pump(数据泵)工具,它提供了一种基于服务器的数据导出导入使用程序。
所有的data pump都作为一个服务器进程,数据不再必须由一个客户程序处理。Data Pump工具的导出和导入实现
Oracle数据库之间数据的传输。
Data Pump工具中包含Data Pump Export和Data Pump Import,所使用的命令行客户程序为expdp和impdp。

【注意】
在10g之前,传统的导出导入分别使用exp工具和imp工具。从oracle database 10g开始,不仅保留了原有的
exp和imp工具,还提供了数据泵导出导入工具expdp和impdp。
从11g开始,在传统的export和import应用程序中可用的任何特性在data pump中都可用。
在使用expdp和impdp工具时,应该注意以下几点:
1 exp和imp是客户端工具程序,它们既可以在客户端使用,也可以在服务器端使用。
2 expdp和impdp是服务器工具程序,它们只能在oracle服务器端使用,不能再客户端使用。
3 imp只适用于exp导出的文件,不适用于expdp导出文件;impdp只适用与expdp导出的文
件,不适用于exp导出文件。data pump导出导入所得到的文件跟传统的import/export
应用程序导出导入的文件不兼容。

【pump特点】
与原有的export和import使用程序相比,oracle的data pump工具的功能特点如下:
1 在导出或者导入作业中,能够控制用于此作业的并行线程的数量。
2 支持在网络上进行导出导入,而不需要是使用转储文件集。
3 如果作业失败或者停止,能够重新启动一个data pump作业。并且能够挂起恢复导出导入作业。
4 通过一个客户端程序能够连接或者脱离一个运行的作业。
5 空间估算能力,而不需要实际执行导出。
6 可以指定导出导入对象的数据库版本。允许对导出导入对象进行版本控制,以便与低版本数据库兼容。

【pump数据字典】
数据字典 说明
dba_datapump_jobs 显示运行数据泵作业的信息,也可以使用user_datapump_jobs变量
dba_datapump_sessions 提供数据泵作业会话级别的信息
datapump_paths 提供一系列有效的对象类型,可以将其与export或者impdp的include或者exclude参数关联起来
dba_directories 提供一系列已定义的目录

--21.2 使用datapump工具的准备工作
1 在环境变量中队bin目录进行配置。默认情况下,安装oracle数据库时,将自动配置相应的环境变量,
例如D:/oracle/product/10.2.0/db_1/BIN
2 在oracle安装路径的bin文件夹中,确定expdp.exe和impdp.exe文件的存在。
3 创建一个外部目录。
data pump要求为将要创建和读取的数据文件和日志文件创建目录,用来指向
使用的外部目录。在oracle中创建目录对象时,可以使用 create directory
语句。
【实例】
1,检查,高级环境变量-pathpath里面有无bin目录
2,检查expdp.exe、impdp.exe文件是否存在。
3,建立目录
c:/> sqlplus /nolog
sql> conn sys/sys as sysdba
sql> create directory mypump as 'd:/app/temp';
sql> grant read, write on directory mypump to scott;
sql> select * from dba_directories; 查询所有目录

--21.3 数据泵导出选项
参数 说明
help 显示用于导出的联机帮助,默认为n
compress 指定要压缩的数据,可选值有:all、data_only、metadata_only和none
content 筛选导出的内容,可选值有:all、data_only和metadata_only
directory 指定用于日志文件和转储文件集的目的目录
dumpfile 为转储文件指定名称和目录
encryption 输出的加密级别,可选值有:all、data_only、encrypted_columns_only、etadata_only和none
exclude 排除导出的对象和数据
flashback_sch 用于数据库在导出过程中闪回的系统更改号
flashback_time 用于数据库在导出过程中闪回的时间戳
include 规定用于导出对象和数据的标准
logfile 导出日志的名字和可选的目录名字
parfile 指定参数文件名
query 在导出过程中从表中筛选行
reuse_dmupfiles 覆盖已有的转储文件
status 显示data pump作业的详细状态
attach 将一个客户会话连接到一个当前运行的data pumpexport作业上
transportable 只为表模式导出而导出元数据
full 在一个full模式下通知data pump导出所有的数据和元数据
schemas 在一个schemas模式导出中命名将导出的模式
tables 列出将用于一个table模式导出而导出的表和分区
tablespaces 列出将导出的表空间
transport_tablespaces
指定一个transportable tablespace模式导出
transport_full_check
是否应该验证正在导出的表空间是一个自包含集

expdp交互模式中的命令列表
参数 说明
add_file 向转储文件集中添加转储文件
exit_client 退出客户机会话并使作业处于运行状态
kill_job 分离和删除作业
paraliel 改变用户data pump export作业的工作进程的数量
start_job 启动、恢复当前作业
status 显示data pump export的作业状态
reuse_dmpfiles 是否覆盖现有的转储文件。设置为y时,现有的转储文件将被覆盖;
当使用默认值n时,如果转储文件已经存在就会产生一个错误。
stop_job 依次关闭执行的作业并退出客户机。stop_job=immediate将立即关闭数据泵作业

【实例】
1,进入目录 D:/>cd D:/oracle/product/10.2.0/db_1/BIN
2,expdp help=y 列出所有参数
3,交互模式下
/BIN> expdp system/system
等待一会,按下ctrl c 进入export模式
Export>stop_job 停止作业
/BIN>

--21.4 实现数据导出
导出模式
模式 参数 说明 操作角色
full full 导出整个数据库 exp_full_database
schema schemas 导出一个或者多个用户模式中的数据和元数据 拥有exp_full_database角色,可以导出任何模式,
否则只能导出自己的模式
table tables 导出一组特定的表 拥有exp_full_database角色,可以导出任何模式的表
tablespace tablespaces 导出一个或者多个表空间的数据 exp_full_database
transportable transport_tablespaces
tablespace 导出表空间中对象的元数据 exp_full_database

【实例】
1,进入bin目录
2,表模式导出
expdp scott/scott_2009 directory=mypump dumpfile=expdptab.dmp tables=dept,emp
(select * from dba_tablespaces; alter tablespace testspace online;)
3,全库模式导出
expdp system/system directory=mypump dumpfile=expdp.dmp full=y
4,schema模式导出
expdp system/system directory=mypump dumpfile=expdp.dmp schemas=scott nologfile=y
5,表空间数据导出
expdp system/system directory=mypump dumpfile=expdpspace.dmp tablespaces=users
6,可移动表空间导出
1 将user表空间设置成只读状态,然后执行可移动表空间元数据导出
sql> alter tablespace users read only;
BIN> expdp system/system directory=mypump dumpfile=exptrans.dmp transport_tablespaces=users;
sql> alter tablespace users online;

--21.5 使用导出参数
1 使用exclude参数
在expdp命令中可以指定参数exclude,用来实现从data pump export中排除对象,如果排除了一个对象,也将排除
所有与它相关的对象。
使用exclude的格式如下:
exclude=object_type[:name_clause][,...]
其中,object_type可以是任何oracle对象类型,包括权限、索引和表等;name_clause用来限制返回的值。
2 使用exclude参数
如果在data pump export中使用include,可以只导出符合要求的对象,其他所有对象均被排除。使用include
参数的格式如下:
include=object_type[:name_clause][,...]
3 使用query参数
对于满足exclude和include标准的对象,将会导出该对象的所有行。这时,可以使用query参数来限制返回的行,
使用query参数格式如下:
query=[schema.][table_name:]query_clause
其中,schema是指定表所属的用户名,或者所属的用户模式名称;
table_name指定表名;query_clause用来指定限制条件。

【实例】
1 exclude参数,注意表名要大写。
expdp system/system directory=mypump dumpfile=expdpexclude.dmp tablespaces=users exclude=table:"in('EMP')" exclude=table:"in('DEPT')"
2 include参数,注意表名要大写。导出users表空间的索引信息
expdp system/system directory=mypump dumpfile=expdpinclude.dmp tablespaces=users include=index
3 query参数
expdp system/system directory=mypump dumpfile=expdpquery.dmp tables=scott.dept query=/"where deptno=10/"


--21.6 数据泵导入选项
关键字 说明 (默认)
------------------------------------------------------------------------------
ATTACH 连接到现有作业, 例如 ATTACH [=作业名]。
CONTENT 指定要加载的数据, 其中有效关键字为:
(ALL), DATA_ONLY 和 METADATA_ONLY。
DIRECTORY 供转储文件, 日志文件和 sql 文件使用的目录对象。
DUMPFILE 要从 (expdat.dmp) 中导入的转储文件的列表,
例如 DUMPFILE=scott1.dmp, scott2.dmp, dmpdir:scott3.dmp。
ENCRYPTION_PASSWORD 用于访问加密列数据的口令关键字。
此参数对网络导入作业无效。
ESTIMATE 计算作业估计值, 其中有效关键字为:
(BLOCKS) 和 STATISTICS。
EXCLUDE 排除特定的对象类型, 例如 EXCLUDE=TABLE:EMP。
FLASHBACK_SCN 用于将会话快照设置回以前状态的 SCN。
FLASHBACK_TIME 用于获取最接近指定时间的 SCN 的时间。
FULL 从源导入全部对象 (Y)。
HELP 显示帮助消息 (N)。
INCLUDE 包括特定的对象类型, 例如 INCLUDE=TABLE_DATA。
JOB_NAME 要创建的导入作业的名称。
LOGFILE 日志文件名 (import.log)。
NETWORK_LINK 链接到源系统的远程数据库的名称。
NOLOGFILE 不写入日志文件。
PARALLEL 更改当前作业的活动 worker 的数目。
PARFILE 指定参数文件。
QUERY 用于导入表的子集的谓词子句。
REMAP_DATAFILE 在所有 DDL 语句中重新定义数据文件引用。
REMAP_SCHEMA 将一个方案中的对象加载到另一个方案。
REMAP_TABLESPACE 将表空间对象重新映射到另一个表空间。
REUSE_DATAFILES 如果表空间已存在, 则将其初始化 (N)。
SCHEMAS 要导入的方案的列表。
SKIP_UNUSABLE_INDEXES 跳过设置为无用索引状态的索引。
SQLFILE 将所有的 SQL DDL 写入指定的文件。
STATUS 在默认值 (0) 将显示可用时的新状态的情况下,
要监视的频率 (以秒计) 作业状态。
STREAMS_CONFIGURATION 启用流元数据的加载
TABLE_EXISTS_ACTION 导入对象已存在时执行的操作。
有效关键字: (SKIP), APPEND, REPLACE 和 TRUNCATE。
TABLES 标识要导入的表的列表。
TABLESPACES 标识要导入的表空间的列表。
TRANSFORM 要应用于适用对象的元数据转换。
有效的转换关键字: SEGMENT_ATTRIBUTES, STORAGE
OID 和 PCTSPACE。
TRANSPORT_DATAFILES 按可传输模式导入的数据文件的列表。
TRANSPORT_FULL_CHECK 验证所有表的存储段 (N)。
TRANSPORT_TABLESPACES 要从中加载元数据的表空间的列表。
仅在 NETWORK_LINK 模式导入操作中有效。
VERSION 要导出的对象的版本, 其中有效关键字为:
(COMPATIBLE), LATEST 或任何有效的数据库版本。
仅对 NETWORK_LINK 和 SQLFILE 有效。

下列命令在交互模式下有效。
注: 允许使用缩写
命令 说明 (默认)
------------------------------------------------------------------------------
CONTINUE_CLIENT 返回到记录模式。如果处于空闲状态, 将重新启动作业。
EXIT_CLIENT 退出客户机会话并使作业处于运行状态。
HELP 总结交互命令。
KILL_JOB 分离和删除作业。
PARALLEL 更改当前作业的活动 worker 的数目。
PARALLEL=<worker 的数目>。
START_JOB 启动/恢复当前作业。
START_JOB=SKIP_CURRENT 在开始作业之前将跳过
作业停止时执行的任意操作。
STATUS 在默认值 (0) 将显示可用时的新状态的情况下,
要监视的频率 (以秒计) 作业状态。
STATUS[=interval]
STOP_JOB 顺序关闭执行的作业并退出客户机。
STOP_JOB=IMMEDIATE 将立即关闭
数据泵作业。

【实例】
查看命令:impdp help=y
1,表模式导入
impdp scott/scott_2009 directory=mypump dumpfile=expdptab.dmp tables=dept,emp table_exists_action=replace
2,全库模式导入
impdp system/system directory=mypump dumpfile=expdp.dmp full=y
3,schema模式导入
impdp system/system directory=mypump dumpfile=expdp.dmp schemas=scott nologfile=y
4,表空间数据导入
impdp system/system directory=mypump dumpfile=expdpspace.dmp tablespaces=users
5,可移动表空间导入
1 将user表空间设置成只读状态,然后执行可移动表空间元数据导出


sql> alter tablespace users read only;
BIN> impdp system/system directory=mypump dumpfile=exptrans.dmp transport_tablespaces=users;
sql> alter tablespace users online;

目的:指导项目侧自行进行简单的数据泵迁移工作。

本文实验环境:Oracle 11.2.0.4,利用数据库自带的scott示例用户进行试验测试。
根据《[简单常用的数据泵导出导入(expdp/impdp)命令举例(上)](http://www.cnblogs.com/jyzhao/p/4522868.html)》的expdp导出的二进制文件进行impdp导入。

[1.首先需要创建Directory](https://www.cnblogs.com/jyzhao/p/4530575.html#1)

[2.创建表空间和用户及赋权](https://www.cnblogs.com/jyzhao/p/4530575.html#2)

[3. 使用impdp导入用户数据](https://www.cnblogs.com/jyzhao/p/4530575.html#3)

* [3.1 导入scott用户的元数据,且不包含统计信息;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.1)
* [3.2 导入scott用户的数据;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.2)
* [3.3 导入scott用户下的emp,dept表及数据;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.3)
* [3.4 导入scott用户下的emp,dept表结构;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.4)
* [3.5 导入scott用户下所有的内容;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.5)
* [3.6 并行导入scott用户下所有的内容;](https://www.cnblogs.com/jyzhao/p/4530575.html#3.6)

[4. 特殊需求](https://www.cnblogs.com/jyzhao/p/4530575.html#4)

* [4.1 如果导入环境的用户不同;](https://www.cnblogs.com/jyzhao/p/4530575.html#4.1)
* [4.2 如果导入环境的表空间也不同;](https://www.cnblogs.com/jyzhao/p/4530575.html#4.2)

由于我这里的实验环境还是导出的那台主机,所以需要先模拟出一个导入的环境,你实际导入并不需要。

1.删除scott用户;
`drop user scott cascade;`
2.删除users表空间。
`drop tablespace users including contents and datafiles;`
可能users表空间是默认的数据库表空间,导致删除失败,只需要更改下再执行删除即可。

```
select a.property_name, a.property_value from database_properties a where a.property_name like '%DEFAULT%';
alter database default tablespace DBS_D_XXX;
```

这样就模拟出了一个没有users表空间和scott用户的崭新环境。

# 1. 首先需要创建Directory

这里目录名字定义为"jy",
若是windows平台,对应系统目录为"E:jingyu";

```
create or replace directory jy as 'E:jingyu';
```

若是Unix/Linux平台,对应系统目录为"/tmp/jingyu".

```
create or replace directory jy as '/tmp/jingyu';
```

注意:目录在系统上需要真实存在(mkdir -p /tmp/jingyu),且有访问的权限。
drwxr-xr-x. 2 oracle oinstall 4.0K May 22 16:48 jingyu

# 2. 创建表空间和用户及赋权

```
create tablespace users datafile '+data1' size 10M autoextend on maxsize 30G;
create user scott identified by tiger default tablespace users;
grant connect, resource to scott;
grant read, write on directory jy to scott;
```

# 3. 使用impdp导入用户数据

初始化环境:
初始1:得到删除当前用户下表的SQL:`select 'drop table '||table_name||' purge;' from user_tables;`
初始2:得到查询当前用户下表的数据量:`select 'select count(1) from '||table_name||';' from user_tables;`

## 3.1 导入scott用户的元数据,且不包含统计信息;

impdp system directory=jy dumpfile=scott_meta.dmp logfile=impdp_scott_meta.log

```
$ impdp system directory=jy dumpfile=scott_meta.dmp logfile=impdp_scott_meta.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 13:36:41 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** directory=jy dumpfile=scott_meta.dmp logfile=impdp_scott_meta.log
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Job "SYSTEM"."SYS_IMPORT_FULL_01" completed with 1 error(s) at Tue May 26 13:36:49 2015 elapsed 0 00:00:03
```

## 3.2 导入scott用户的数据;

在3.1导入元数据后才可以导入数据。
impdp system directory=jy dumpfile=scott_data.dmp logfile=impdp_scott_data.log

```
$ impdp system directory=jy dumpfile=scott_data.dmp logfile=impdp_scott_data.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 13:39:15 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_FULL_01": system/******** directory=jy dumpfile=scott_data.dmp logfile=impdp_scott_data.log
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT"."EMP" 8.484 KB 12 rows
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Job "SYSTEM"."SYS_IMPORT_FULL_01" successfully completed at Tue May 26 13:39:26 2015 elapsed 0 00:00:07
```

## 3.3 只导入scott用户下的emp表及数据;

这里为了演示导入,先初始化删除scott用户下的所有表。

impdp scott directory=jy tables=emp dumpfile=scott_emp_dept.dmp logfile=impdp_scott_emp.log

```
$ impdp scott directory=jy tables=emp dumpfile=scott_emp_dept.dmp logfile=impdp_scott_emp.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 13:50:51 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SCOTT"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_TABLE_01": scott/******** directory=jy tables=emp dumpfile=scott_emp_dept.dmp logfile=impdp_scott_emp.log
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."EMP" 8.484 KB 12 rows
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SCOTT"."SYS_IMPORT_TABLE_01" successfully completed at Tue May 26 13:50:59 2015 elapsed 0 00:00:03
```

## 3.4 只导入scott用户下的emp,dept表结构;

这里为了演示导入,先初始化删除scott用户下的所有表。

impdp scott directory=jy tables=emp,dept dumpfile=scott_emp_dept_meta.dmp logfile=impdp_scott_emp_dept_meta.log

```
$ impdp scott directory=jy tables=emp,dept dumpfile=scott_emp_dept_meta.dmp logfile=impdp_scott_emp_dept_meta.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 13:54:38 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SCOTT"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_TABLE_01": scott/******** directory=jy tables=emp,dept dumpfile=scott_emp_dept_meta.dmp logfile=impdp_scott_emp_dept_meta.log
Processing object type TABLE_EXPORT/TABLE/TABLE
Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SCOTT"."SYS_IMPORT_TABLE_01" successfully completed at Tue May 26 13:54:45 2015 elapsed 0 00:00:03
```

由于导出就是emp,dept两张表,所以也可以不指定tables,以下两种写法在这里都是可以的:
impdp scott directory=jy dumpfile=scott_emp_dept_meta.dmp logfile=impdp_scott_emp_dept_meta.log

impdp scott directory=jy dumpfile=scott_emp_dept_meta.dmp logfile=impdp_scott_emp_dept_meta.log full=y

## 3.5 导入scott用户下所有的内容;

impdp system directory=jy schemas=scott dumpfile=scott_all.dmp logfile=impdp_scott_all.log

如果是在2.4基础上直接导入,会因为emp,dept表已经存在导致导入过程中会由于table_exists_action参数的默认选项是skip,从而跳过emp,dept表数据的导入,如下:

```
$ impdp system directory=jy schemas=scott dumpfile=scott_all.dmp logfile=impdp_scott_all.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:22:50 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott dumpfile=scott_all.dmp logfile=impdp_scott_all.log
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
ORA-39151: Table "SCOTT"."DEPT" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
ORA-39151: Table "SCOTT"."EMP" exists. All dependent metadata and data will be skipped due to table_exists_action of skip
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 3 error(s) at Tue May 26 14:22:57 2015 elapsed 0 00:00:03
```

所以这时我们想导入这些数据,可以加参数 table_exists_action,指定想要的选项。
TABLE_EXISTS_ACTION
Action to take if imported object already exists.
Valid keywords are: APPEND, REPLACE, [SKIP] and TRUNCATE.

这里选择truncate,即如果表存在,那么处理方式是truncate此表后导入文件中包含的数据。
impdp system directory=jy schemas=scott table_exists_action=truncate dumpfile=scott_all.dmp logfile=impdp_scott_all.log

```
$ impdp system directory=jy schemas=scott table_exists_action=truncate dumpfile=scott_all.dmp logfile=impdp_scott_all.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:26:09 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott table_exists_action=truncate dumpfile=scott_all.dmp logfile=impdp_scott_all.log
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Table "SCOTT"."DEPT" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Table "SCOTT"."BONUS" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Table "SCOTT"."SALGRADE" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Table "SCOTT"."EMP" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT"."EMP" 8.484 KB 12 rows
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 1 error(s) at Tue May 26 14:26:17 2015 elapsed 0 00:00:04
```

注意:如果这里选用append选项,那么如果原表有数据,且没有合理的约束条件,则可能导致数据的重复导入,所以,**生产环境实际导入过程中一定要弄清楚数据的实际情况才能准确决定如何选用此参数的选项**。
如下所示,SALGRADE表会出现5条重复数据:
impdp system directory=jy schemas=scott table_exists_action=append dumpfile=scott_all.dmp logfile=impdp_scott_all.log

```
$ impdp system directory=jy schemas=scott table_exists_action=append dumpfile=scott_all.dmp logfile=impdp_scott_all.log

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:28:27 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott table_exists_action=append dumpfile=scott_all.dmp logfile=impdp_scott_all.log
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Table "SCOTT"."DEPT" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Table "SCOTT"."BONUS" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Table "SCOTT"."SALGRADE" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Table "SCOTT"."EMP" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
ORA-31693: Table data object "SCOTT"."DEPT" failed to load/unload and is being skipped due to error:
ORA-00001: unique constraint (SCOTT.PK_DEPT) violated
ORA-31693: Table data object "SCOTT"."EMP" failed to load/unload and is being skipped due to error:
ORA-00001: unique constraint (SCOTT.BIN$ESfmGQ7ZSsLgU58JqMBQqw==$0) violated
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 3 error(s) at Tue May 26 14:28:34 2015 elapsed 0 00:00:03
```

## 3.6 并行导入scott用户下所有的内容;

impdp system directory=jy schemas=scott table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

这里对于已经存在的对象直接replace掉。

```
$ impdp system directory=jy schemas=scott table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:15:37 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT"."EMP" 8.484 KB 12 rows
. . imported "SCOTT"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 1 error(s) at Tue May 26 14:15:46 2015 elapsed 0 00:00:05
```

# 4. 特殊需求

特殊需求环境准备:
创建表空间users2:
`create tablespace users2 datafile '+data1' size 10M autoextend on maxsize 30G;`
创建用户scott2:
`create user scott2 identified by tiger default tablespace users2;`
赋权用户scott2:
`grant connect, resource to scott2;`

## 4.1 如果导入环境的用户不同;

需求:将原scott用户的数据导入到现在的scott2用户。
impdp system directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

```
$ impdp system directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:55:02 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT2" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT2"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT2"."EMP" 8.484 KB 12 rows
. . imported "SCOTT2"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT2"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 1 error(s) at Tue May 26 14:55:09 2015 elapsed 0 00:00:04
```

## 4.2 如果导入环境的表空间也不同;

需求:将原users表空间的对象重定向到users2表空间。
impdp system directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 REMAP_TABLESPACE=users:users2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

```
$ impdp system directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 REMAP_TABLESPACE=users:users2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

Import: Release 11.2.0.4.0 - Production on Tue May 26 14:57:20 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 REMAP_TABLESPACE=users:users2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"SCOTT2" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT2"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT2"."EMP" 8.484 KB 12 rows
. . imported "SCOTT2"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT2"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" completed with 1 error(s) at Tue May 26 14:57:29 2015 elapsed 0 00:00:05
```

细心的朋友,会发现导入的日志最后都提示有一个错误,往上查发现是报错ORA-31684用户已存在,这是因为我们习惯在导入前建立好对应的用户,避免一些其他的权限错误,所以这个错误是可以忽略的。
当然其实如果我们已经建立了对应的表空间,用户也是可以不事先建立的,比如:
我们这里的情景,如果只事先建立users2表空间,不建立scott2用户,也是可以成功导入且不会有任何报错提示。

```
$ impdp system directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 REMAP_TABLESPACE=users:users2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2

Import: Release 11.2.0.4.0 - Production on Tue May 26 15:03:33 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Password:

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_SCHEMA_01": system/******** directory=jy schemas=scott REMAP_SCHEMA=scott:scott2 REMAP_TABLESPACE=users:users2 table_exists_action=replace dumpfile=scott_all%U.dmp logfile=impdp_scott_all.log parallel=2
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT2"."DEPT" 5.929 KB 4 rows
. . imported "SCOTT2"."EMP" 8.484 KB 12 rows
. . imported "SCOTT2"."SALGRADE" 5.859 KB 5 rows
. . imported "SCOTT2"."BONUS" 0 KB 0 rows
Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully completed at Tue May 26 15:03:42 2015 elapsed 0 00:00:05
```

原文地址:https://www.cnblogs.com/liutoliu/p/11818719.html