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
```


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM