1. 程式人生 > >Oracle利用exp和imp,資料泵impdp和expdp匯入匯出資料,spool匯出資料

Oracle利用exp和imp,資料泵impdp和expdp匯入匯出資料,spool匯出資料

Oracle利用exp和imp,資料泵impdp和expdp匯入匯出資料,spool匯出資料

注意:
(1)imp只能匯入exp匯出的檔案 ,imp,exp是客戶端程式,操作少量資料

 (2)impdp只能匯入expdp匯出的檔案,impdp,expdp是伺服器端程式,操作適量大的資料,常常用於資料的備份和恢復,資料遷移

目錄

Oracle利用exp和imp,資料泵impdp和expdp匯入匯出資料,spool匯出資料

一、exp和imp的匯出匯入

二、資料泵expdp和impdp工具匯出的步驟:

三、spool匯出資料


 


一、
exp和imp的匯出匯入


1.exp匯出模式(三種模式)及命令格式


(1) 全庫模式
exp 使用者名稱/密碼@網路服務名 full=y file=路徑\檔名.dmp log=路徑\檔名.log

例子:exp aml/[email protected] full=y file=/home/oracle/dump_dir/exp.dmp log=/home/oracle/log/exp.log


(2) 使用者模式(一般情況下采用此模式)
exp 使用者名稱/密碼@網路服務名 owners=(使用者1,使用者2,使用者3,…) file=路徑\檔名.dmp log=路徑\檔名.log

例子:exp aml/

[email protected] owners=aml file=/home/oracle/dump_dir/exp_aml.dmp log=/home/oracle/log/exp_aml.log


(3)表模式
exp 使用者名稱/密碼@網路服務名 tables=(表名1,表名2,表名3,…)file=路徑\檔名.dmp log=路徑\檔名.log

例子:exp aml/[email protected] tables=t00_user file=/home/oracle/dump_dir/exp_user.dmp log=/home/oracle/log/exp_user.log


(4)另外,還可以只匯出某個表空間
exp 使用者名稱/密碼@網路服務名 tablespaces=(表空間1,表空間2,表空間3,…) file=路徑\檔名.dmp log=路徑\檔名.log

例子:exp aml/[email protected] tablespaces=TSDAT01 file=/home/oracle/dump_dir/exp_tablespace.dmp log=/home/oracle/log/exp__tablespace.log


2. imp匯入模式(三種模式)及命令格式


(1) 全庫模式
imp 使用者名稱/密碼@網路服務名 full=y file=路徑\檔名.dmp log=路徑\ds110.log

例子:imp aml/[email protected] full=y file=/home/oracle/dump_dir/imp.dmp log=/home/oracle/log/imp.log


(2)使用者模式(一般情況下采用此模式)
imp 使用者名稱/密碼@網路服務名 file=路徑\檔名.dmp fromuser=匯出的使用者名稱 touser=匯入的使用者名稱 log=路徑\ds110.log

例子:imp aml/[email protected] file=/home/oracle/dump_dir/imp_s.dmp fromuser=aml touser=aml log=/home/oracle/log/imp_s.log


(3)表模式
imp 使用者名稱/密碼@網路服務名 tables=(表名1,表名2,表名3,…)file=路徑\檔名.dmp fromuser=匯出的使用者名稱 touser=匯入的使用者名稱 log=路徑\ds110.log

例子:imp aml/[email protected] tables=t00_user file=/home/oracle/dump_dir/imp_tab.dmp fromuser=aml touser=aml log=/home/oracle/log/imp_tab.log


(4)另外,還可以只匯入某個表空間
imp 使用者名稱/密碼@網路服務名 tablespaces=(表空間1,表空間2,表空間3,…) file=路徑\檔名.dmp fromuser=匯出的使用者名稱 touser=匯入的使用者名稱 log=路徑\ds110.log

例子:imp aml/[email protected] tablespaces=TSDAT01 file=/home/oracle/dump_dir/exp_tablespace.dmp fromuser=aml touser=aml log=/home/oracle/log/exp__tablespace.log


3. 匯入的命令引數說明


USERID 使用者名稱/密碼
FULL 是否全庫匯入 (Y 或 N),預設為 N
BUFFER 資料緩衝的大小
FROMUSER 匯出的使用者列表,即匯入檔案是從哪個使用者匯出的
TOUSER 匯入的使用者列表,即要到哪個使用者中
FILE 匯入檔案,是dmp檔案
SHOW 只列出檔案內容 (Y 或 N) ,預設為 N
TABLES 要匯入的表名列表
IGNORE 在匯入過程中是否忽略錯誤 , 預設為 N
RECORDLENGTH 記錄的長度,預設為operating system-dependent
GRANTS 匯入許可權 (Y 或 N),預設為Y
INCTYPE 匯入的型別是否為遞增(Y 或 N),預設為 N
INDEXES 匯入索引 (Y 或 N) ,預設為Y
COMMIT 在匯入過程中,是否匯入一行資料,提交一行 (Y 或 N)預設為N,即在每個表匯入後,進行提交
ROWS 匯入行資料 (Y 或 N) ,預設為 Y
PARFILE 引數檔名
LOG 輸出的日誌檔案
DESTROY 是否覆蓋表空間上的資料檔案 (Y 或 N) 預設為N
INDEXFILE 將表或索引資訊寫到指定的檔案中
CHARSET 匯出檔案的字符集,預設為 NLS_LANG
ANALYZE 在匯入時執行ANALYZE語句 (Y 或 N) 預設為 Y
FEEDBACK 在匯入時每個幾行顯示進度,預設為0


4. 匯出的命令引數說明


USERID 使用者名稱/密碼
FULL 是否全庫匯入 (Y 或 N),預設為 N
BUFFER 資料緩衝的大小
OWNER 匯出的使用者列表
FILE 匯出檔案,是dmp檔案
TABLES 要匯出的表名列表
COMPRESS 是否壓縮(Y 或 N), 預設為Y
RECORDLENGTH 輸入輸出記錄的長度,預設為operating system-dependent
GRANTS 匯出許可權 (Y 或 N),預設為Y
INCTYPE 匯出的型別是否為遞增(Y 或 N),預設為 N
INDEXES 匯出索引 (Y 或 N) ,預設為Y
RECORD 是否在系統SYS.INCEXP, SYS.INCFIL中記錄一個遞增或累計的匯出(Y 或 N) , 預設為Y
ROWS 匯出行資料 (Y 或 N) ,預設為 Y
PARFILE 引數檔名
CONSTRAINTS 是否匯出約束,(Y 或 N) ,預設為 Y
CONSISTENT 在執行匯出操作時是否加事務(Y 或 N),預設為 N
LOG 輸出的日誌檔案
STATISTICS 在匯出時執行ANALYZE語句 (Y 或 N) 預設為 Y
DIRECT direct path (N)
FEEDBACK 在匯出時每個幾行顯示進度,預設為0


5.匯出的物件


1、 全庫模組
可以匯出除sys使用者擁有的所有資料庫物件,包括:表空間的定義、配置檔案、使用者定義、角色、系統許可權授權、角色授權、預設角色、resource costs、會滾段的定義、資料庫連結、虛列、所有目錄別名、所有外部函式庫、所有物件型別、所有簇定義;對於每個表,還可匯出表使用的物件型別定義、表定義、表資料、巢狀表資料、表索引、表約束、表授權、分析表、列和表註釋、稽核資訊、所有引用的完整性約束、所有同義詞、所有檢視、所有儲存過程、包、函式、所有觸發器、分析簇、快照、job、all refresh groups and children
2、 使用者模組
物件型別、資料庫鏈路、序列、簇定義,對於每個表,還可匯出表使用的物件型別定義、表定義、表資料、巢狀表資料、表索引、表約束、表授權、分析表、列和表註釋、稽核資訊、屬於當前使用者的表所引用的完整性約束、同義詞、檢視、儲存過程、包、函式、觸發器、分析簇、快照、job、refresh groups
3、 表模式
可匯出表使用的物件型別定義、表定義、表資料、巢狀表資料、表索引、表約束、表授權、分析表、列和表註釋、稽核資訊、表引用的完整性約束、觸發器,還可匯出其他使用者擁有的觸發器、其他使用者使用者的索引


6.匯入時的順序
1、 字符集
2、 表定義
3、 表資料
4、 表索引
5、 完整性約束、觸發器、bitmap索引
6、 檢視、函式、過程
7、 包


7.高階應用
1、 當把資料載入到一張已存在的表中,必須使用引數IGNORE = Y
2、 如何在匯出表時加入條件
Exp 使用者名稱/密碼@網路服務名 file=路徑\檔名.dmp tables=(表名) query = \”條件\”

8.全庫匯入時包含的系統物件
配置檔案profiles
公共資料庫連結
同義詞
角色
會滾段的定義
system audit options
系統許可權
表空間定義
tablespace quotas
使用者定義
目錄別名


9.匯入工具imp可能出現的問題
1、資料庫物件已經存在
一般情況, 匯入資料前應該徹底刪除目標資料下的表, 序列, 函式/過程,觸發器等;資料庫物件已經存在, 按預設的imp引數, 則會匯入失敗;如果用了引數ignore=y, 會把exp檔案內的資料內容匯入;如果表有唯一關鍵字的約束條件, 不合條件將不被匯入,如果表沒有唯一關鍵字的約束條件, 將引起記錄重複
2、 資料庫物件有主外來鍵約束 這發生在將幾個包含表的DMP檔案分別匯入資料庫時,不符合主外來鍵約束, 資料便會匯入失敗;解決辦法: 先匯入主表, 再匯入依存表;disable目標匯入物件的主外來鍵約束, 匯入資料後, 再enable它們3、 許可權不夠          如果要把A使用者的資料匯入B使用者下, 做匯入操作的使用者需要有imp_full_database許可權4、 匯入大表( 大於80M ) 時, 儲存分配失敗預設的EXP時, compress = Y, 也就是把所有的資料壓縮在一個數據塊上,匯入時, 如果不存在連續一個大資料塊, 則會匯入失敗,匯出80M以上的大表時, 記得compress= N, 則不會引起這種錯誤.5、 imp和exp使用的字符集不同如果字符集不同, 匯入會失敗, 可以改變unix環境變數或者NT登錄檔裡NLS_LANG相關資訊,匯入完成後再改回來.6、 imp和exp版本不能往上相容
imp可以成功匯入低版本exp生成的檔案, 不能匯入高版本exp生成的檔案
 
10.從一個賬戶匯出,另一個賬戶匯入
匯出表模式示例:
1 從dbo_pwcdb 賬號匯出 PRNBSN_CONTENTTEMPLATE,PRNBSN_RELINFOCONTENT 兩個表,不帶資料
exp dbo_pwcdb/[email protected] file=d:\daochu_noData.dmp tables=
(PRNBSN_CONTENTTEMPLATE,PRNBSN_RELINFOCONTENT) rows=n;
2  匯入 dbo_smydb 賬戶
imp dbo_smydb/[email protected] full=y file=d:\daochu_noData.dmp  touser=dbo_smydb ignore=y;
 
11. 藉助PL/SQL  Developer 工具 匯出匯入 觸發器,儲存過程,序列,索引等物件:


1   A賬戶登入PL/SQL Developer 工具,在選單中選擇 工具--->匯出使用者物件
2 以匯出儲存過程為示例,選擇如下圖:

在procedure 物件中,選擇需要匯出的儲存過程物件,設定輸出檔名字,點選匯出。
3 如果在需要匯入的oracle庫中,是同一個名字的賬戶,可以直接新建一個命令視窗,然後複製剛才匯出fun_proc.sql的內容,貼上到 命令視窗中,就自動執行了;相當於通過sql指令碼進行匯入;
  如果需要匯入的oracle庫中,是另外一個名字的賬戶,比如B賬戶。那麼需要開啟fun_proc.sql 檔案,查詢所有A賬戶名全部替換為B賬戶,然後再用B賬戶登入PL/SQL Developer 工具,新建命令視窗,貼上檔案內容,執行。


    注意:生成的儲存過程名字會如下形式 Create procedure   賬戶A.儲存過程名                    

所以需要替換為 Create procedure   賬戶B.儲存過程名. 開啟看下就明白了。
   其他物件的匯入參照儲存過程物件匯入方式。

 

二、資料泵expdp和impdp工具匯出的步驟:

1.expdp匯出的準備工作

 (1)先sqlplus aml/[email protected]登入資料庫

 (2)建立DIRECTORY
create directory dir_dp as 'D:/oracle/dir_dp'; 
(3)授權(自己建立自己使用不用授權,可省略該步驟)
Grant read,write on directory dir_dp to zftang;
--檢視目錄及許可權
SELECT privilege, directory_name, DIRECTORY_PATH FROM user_tab_privs t, all_directories d
 WHERE t.table_name = d.directory_name ORDER BY 2, 1;
 

 

 

2.expdp匯出的各種模式和命令格式:

(1) 按表模式匯出:
expdp zftang/[email protected]  tables=zftang.b$i_exch_info,zftang.b$i_manhole_info dumpfile =expdp_test2.dmp logfile=expdp_test2.log directory=dir_dp job_name=my_job

(2)按查詢條件匯出:
expdp zftang/[email protected]  tables=zftang.b$i_exch_info dumpfile =expdp_test3.dmp logfile=expdp_test3.log directory=dir_dp job_name=my_job query='"where rownum<11"'

(3)按表空間匯出:
Expdp zftang/[email protected] dumpfile=expdp_tablespace.dmp tablespaces=GCOMM.DBF logfile=expdp_tablespace.log directory=dir_dp job_name=my_job

(4)匯出方案
Expdp zftang/zftang DIRECTORY=dir_dp DUMPFILE=schema.dmp SCHEMAS=zftang,gwm

(5)匯出整個資料庫:
expdp zftang/[email protected] dumpfile =full.dmp full=y logfile=full.log directory=dir_dp job_name=my_job


(6)按使用者匯出

expdp zftang/[email protected] schemas=zftang directory=dir_dp dumpfile =expdp_test1.dmp logfile=expdp_test1.log;

3.impdp匯入模式和命令格式:

 (1)按表匯入
p_street_area.dmp檔案中的表,此檔案是以gwm使用者按schemas=gwm匯出的:
impdp gwm/[email protected]  dumpfile =p_street_area.dmp logfile=imp_p_street_area.log directory=dir_dp tables=p_street_area job_name=my_job

(2)按使用者匯入(可以將使用者資訊直接匯入,即如果使用者資訊不存在的情況下也可以直接匯入)
impdp gwm/[email protected] schemas=gwm dumpfile =expdp_test.dmp logfile=expdp_test.log directory=dir_dp job_name=my_job

(3)不通過expdp的步驟生成dmp檔案而直接匯入的方法:
--從源資料庫中向目標資料庫匯入表p_street_area
impdp gwm/gwm directory=dir_dp NETWORK_LINK=igisdb tables=p_street_area logfile=p_street_area.log  job_name=my_job
igisdb是目的資料庫與源資料的連結名,dir_dp是目的資料庫上的目錄

(4)更換表空間
  採用remap_tablespace引數 
  --匯出gwm使用者下的所有資料
expdp system/orcl directory=data_pump_dir dumpfile=gwm.dmp SCHEMAS=gwm
注:如果是用sys使用者匯出的使用者資料,包括使用者建立、授權部分,用自身使用者匯出則不含這些內容
--以下是將gwm使用者下的資料全部匯入到表空間gcomm(原來為gmapdata表空間下)下
impdp system/orcl directory=data_pump_dir dumpfile=gwm.dmp remap_tablespace=gmapdata:gcomm

4.資料泵檔案存放的位置詳析

資料泵是伺服器端工具,匯出的檔案是放在資料庫所在的伺服器上,當然我們知道可以通過directory目錄物件來控制。目錄物件預設有四個級別,當然是有優先順序順序的,優先順序從上往下

1.每個檔案單獨的指定具體的目錄
2.expdp匯出時,指定的目錄引數
3.使用者定義的環境變數DATA_PUMP_DIR指定的目錄
4.預設的目錄物件DATA_PUMP_DIR

當然了對於oracle11g R2來說,又引入了一個可選項,我們就當是5
5.DATA_PUMP_DIR_SCHEMA_NAME目錄


一、預設的目錄物件DATA_PUMP_DIR測試

SQL>  desc dba_directories
 Name                                                              Null?    Type
 ----------------------------------------------------------------- -------- ---------------------- OWNER                                                             NOT NULL VARCHAR2(30)
 DIRECTORY_NAME                                                    NOT NULL VARCHAR2(30)
 DIRECTORY_PATH                                                             VARCHAR2(4000)

 

SQL> set linesize 120 pagesize 100
SQL> col OWNER for a5    
SQL> col DIRECTORY_NAME for a22
SQL> col DIRECTORY_PATH for a80


SQL> select * from dba_directories;

OWNER DIRECTORY_NAME         DIRECTORY_PATH
----- ---------------------- ----------------------------------------------------------

SYS   SUBDIR                 /u01/app/oracle/product/11.2.0/db/demo/schema/order_entry//2002/Sep
SYS   SS_OE_XMLDIR           /u01/app/oracle/product/11.2.0/db/demo/schema/order_entry/
SYS   LOG_FILE_DIR           /u01/app/oracle/product/11.2.0/db/demo/schema/log/
SYS   MEDIA_DIR              /u01/app/oracle/product/11.2.0/db/demo/schema/product_media/
SYS   XMLDIR                 /u01/app/oracle/product/11.2.0/db/rdbms/xml
SYS   DATA_FILE_DIR          /u01/app/oracle/product/11.2.0/db/demo/schema/sales_history/
SYS   DATA_PUMP_DIR          /u01/app/oracle/admin/tj01/dpdump/
SYS   ORACLE_OCM_CONFIG_DIR  /u01/app/oracle/product/11.2.0/db/ccr/state

 

通過查詢我們看到,所有的目錄都屬於SYS使用者,而不管是哪個使用者建立的,在資料庫裡已經提前建好了這個目錄物件DATA_PUMP_DIR。如果在使用expdp匯出時,不指定目錄物件引數,Oracle會使用資料庫預設的目錄DATA_PUMP_DIR,不過如果想使用這個目錄的話,使用者需要具有exp_full_database的許可權才行

 

SQL> conn scott/tiger
Connected.

SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
BONUS                          TABLE
DEPT                           TABLE
EMP                            TABLE
SALGRADE                       TABLE


SQL> create table demo as select empno,ename,sal,deptno from emp;
Table created.


SQL> exit          
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

由於沒有exp_full_database,所以這樣的匯出會報錯,你可以選擇授權或者使用有許可權的使用者

[[email protected] ~]$ expdp scott/tiger dumpfile=emp.dmp tables=emp
Export: Release 11.2.0.3.0 - Production on Fri Nov 16 13:48:19 2012
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-39145: directory object parameter must be specified and non-null


[[email protected] ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Fri Nov 16 13:58:14 2012
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

SQL> grant exp_full_database to scott;
Grant succeeded.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

授權之後,匯出成功,並且匯出的檔案和日誌會到DATA_PUMP_DIR物件指定的目錄
[[email protected] ~]$ expdp scott/tiger dumpfile=emp.dmp tables=emp

Export: Release 11.2.0.3.0 - Production on Fri Nov 16 13:58:52 2012
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** dumpfile=emp.dmp tables=emp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."EMP"                               8.562 KB      14 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
  /u01/app/oracle/admin/tj01/dpdump/emp.dmp
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at 13:59:05


對於oracle11g來說,我們還可以定義一個新的目錄物件,物件名稱是DATA_PUMP_DIR_SCHEMA_NAME,如果定義了這個目錄物件,並授予許可權,在使用者匯出沒有指定

具體的目錄引數的情況下,匯出檔案會到這個目錄

[[email protected] ~]$ mkdir sdir
[[email protected] ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Fri Nov 16 14:13:06 2012
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

SQL> create directory data_pump_dir_scott as '/home/oracle/sdir';
Directory created.

SQL> grant read,write on  directory data_pump_dir_scott to scott;
Grant succeeded.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

[[email protected] ~]$ expdp scott/tiger dumpfile=emp1.dmp tables=emp

 

Export: Release 11.2.0.3.0 - Production on Fri Nov 16 14:14:29 2012

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Database Directory Object has defaulted to: "DATA_PUMP_DIR_SCOTT".
Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** dumpfile=emp5.dmp tables=emp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."EMP"                               8.562 KB      14 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
  /home/oracle/sdir/emp1.dmp
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at 14:14:34

 

二、如果設定了環境變數DATA_PUMP_DIR的話,環境變數會覆蓋預設設定
[[email protected] ~]$ mkdir udir
[[email protected] ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Fri Nov 16 13:59:15 2012
Copyright (c) 1982, 2011, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

SQL> create directory udir as '/home/oracle/udir';
Directory created.

SQL> grant write ,read on directory udir to scott;
Grant succeeded.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

注意,在定義環境變數的時候,後面的值要用大寫
[[email protected] ~]$ export DATA_PUMP_DIR=udir
[[email protected] ~]$ expdp scott/tiger dumpfile=emp2.dmp tables=emp

Export: Release 11.2.0.3.0 - Production on Fri Nov 16 14:05:07 2012

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

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
ORA-39002: invalid operation
ORA-39070: Unable to open the log file.
ORA-39087: directory name udir is invalid


[[email protected] ~]$ export DATA_PUMP_DIR=UDIR
[[email protected] ~]$ expdp scott/tiger dumpfile=emp2.dmp tables=emp
Export: Release 11.2.0.3.0 - Production on Fri Nov 16 14:05:23 2012
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Database Directory Object has defaulted to: "UDIR".
Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** dumpfile=emp3.dmp tables=emp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."EMP"                               8.562 KB      14 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
  /home/oracle/udir/emp2.dmp
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at 14:05:29

 

三、如果匯出時定義了引數directory,會使用引數指定的目錄
[[email protected] ~]$ expdp scott/tiger directory=udir tables=emp

Export: Release 11.2.0.3.0 - Production on Fri Nov 16 14:07:33 2012
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** directory=udir tables=emp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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/CONSTRAINT/REF_CONSTRAINT
Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "SCOTT"."EMP"                               8.562 KB      14 rows
Master table "SCOTT"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_TABLE_01 is:
  /home/oracle/udir/expdat.dmp
Job "SCOTT"."SYS_EXPORT_TABLE_01" successfully completed at 14:07:41

 

四、如果每個檔案都指定了具體的目錄的話,會以檔案定義的目錄為準
[[email protected] ~]$ mkdir userdir
[[email protected] ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Fri Nov 16 14:09:58 2012
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

SQL>  create or replace directory userdir as '/home/oracle/userdir';
Directory created.

SQL>  grant read,write on directory userdir to scott;
Grant succeeded.

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options

 

[[email protected] ~]$ expdp scott/tiger dumpfile=userdir:emp3.dmp logfile=userdir:test.log tables=emp

Export: Release 11.2.0.3.0 - Production on Fri Nov 16 14:12:38 2012
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Data Mining
and Real Application Testing options
Database Directory Object has defaulted to: "UDIR".
Starting "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** dumpfile=userdir:emp4.dmp logfile=userdir:test.log tables=emp
Estimate in progress using BLOCKS method...
Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 64 KB
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

三、spool匯出資料

SPOOL是SQLPLUS的命令,直接可以在sqlplus中使用,可以自定義格式

(1)語法:

set arraysize 5000;  //此引數可提高SPOOL解除安裝的速度,最大可以設定為5000
set autotrace on;    //設定允許對執行的sql進行分析
set colsep ',';   //域輸出分隔符
set echo off;    //顯示start啟動的指令碼中的每個sql命令,預設為on
set feedback off;  //回顯本次sql命令處理的記錄條數,預設為on,設定顯示“已選擇XX行”
set heading off;   //輸出域標題,欄位的名稱,預設為on
SET LINESIZE 2500;   //每行允許的最大字元數,設定大些,免得資料被截斷,但不宜過大,太大會大大降低匯出的速度(注意必須與trimspool結合使用防止匯出的文字有太多的尾部空格)
set newpage 1;       //設定頁與頁之間的分隔{1|n|NONE};當值為0時在每頁開頭有一個小的黑方框;當值為n時在頁和頁之間隔著n個空行;當為none時,會在頁和頁之間沒有任何間隔;
set newp none;       //設定查詢出來的資料分多少頁顯示,如果需要連續的資料,中間不要出現空行就把newp設定為none,這樣輸出的資料行都是連續的,中間沒有空行之類的
set num 18;          //設定數字的長度,如果不夠大,則用科學記數法顯示
set numwidth 12;    //輸出number型別域長度,預設為10
SET NULL text;       //顯示時,用text值代替NULL值
set pagesize 2000;  //輸出每頁行數,頁面大小,預設為24,為了避免分頁,可設定為0
set serveroutput on; //設定允許顯示輸出類似dbms_output;--編寫儲存過程時,大多會將必要的資訊輸出;
SET SPACE 0;
set term off;        //不在螢幕上輸出執行結果
set termout off;   //顯示指令碼中的命令的執行結果,預設為on
set timing on;       //顯示每個sql語句花費的執行時間,設定顯示“已用時間:XXXX”
set trimout on;   //去除標準輸出每行的拖尾空格,預設為off
set trimspool on;  //去除重定向(spool)輸出每行的拖尾空格,預設為off
set verify off       //是否顯示替代變數被替代前後的語句
SET wrap on;         //輸出行長度大於設定行長度時(用set linesize n命令設定);值為on時,多餘的字元另起一行顯示,否則多餘的字元將被切除,不予顯示;
--------------------- 

注:LINESIZE要稍微設定大些,免得資料被截斷,它應和相應的TRIMSPOOL結合使用防止匯出的文字有太多的尾部空格。
--------------------- 
(2)例子:

sqlplus -s $user_name/[email protected]$service_name  //-s 引數遮蔽列印到螢幕上的其他資訊
set colsep '    '
set echo off
set trimout on
set trimspool on
set linesize 1000
set pagesize 0
set heading off
set termout off
set feedback off

spool 路徑+生成檔名
select id ||'     '||name  ||'     '|from t_user;  //執行的SQL,以tab鍵分隔
spool off
EOF
--------------------- 
也可以把以上內容放到指令碼sh中,直接執行指令碼,可以自定義自己想要的資料機構的檔案格式,可以匯出txt,csv等

 

參考資料來源:

(1)https://blog.csdn.net/Cow_cz/article/details/70229929

(2)https://blog.csdn.net/Zhangxichao100/article/details/69384769

(3)expdp/impdp 資料庫遷移詳細過程:

   https://blog.csdn.net/shiyu1157758655/article/details/79570983

(4)資料庫匯入匯出exp和expdp以及imp和impdp的區別 轉載:https://blog.csdn.net/fighting_tobe_better/article/details/80169086