1. 程式人生 > >Oracle匯入和匯出exp/imp/expdp/impdp

Oracle匯入和匯出exp/imp/expdp/impdp

匯出

exp

將資料庫完全匯出,使用者名稱system 密碼manager 匯出到exp_export.dmp中

exp system/manager@hostname:1521/ora11g file=exp_export.dmp full=y log=exp_export.log


將資料庫中system使用者與sys使用者的表匯出

exp system/manager@hostname:1521/ora11g file=exp_export.dmp owner=(system,sys) log=exp_export.log


將資料庫中的表inner_notify、notify_staff_relat匯出

exp aichannel/aichannel@hostname:1521/ora11g file=exp_export.dmp tables=inner_notify,notify_staff_relat log=exp_export.log


將資料庫中的表table1中的欄位filed1以"00"打頭的資料匯出

exp system/manager@hostname:1521/ora11g file=exp_export.dmp tables=(table1) query=\" where filed1 like '00%'\" log=exp_export.log


匯出密碼帶有特殊字元的,密碼雙引號,連線串單引號

exp 'testuser/"test/15/!&/57"@localhost:1521/ora11g'  tables=inner_notify file=exp_export.dmp log=exp_export.log


在上面命令後面 加上 compress=y  就可以對匯出的檔案進行壓縮了

其他

Oracle 11G在用EXP匯出時,空表不能匯出,出現“EXP-00003”錯誤,再用IMP匯入時就會出現“IMP-00003”和“ORA-00942: 表或檢視不存在”等錯誤,這是英文Oracle 11g 新增了一個引數“deferred_segment_creation”,含義是段延遲建立,預設是true。如果這個引數設定為true,你新建了一個表T1,並且沒有向其中插入資料,那麼這個表不會立即分配extent,也就是不佔資料空間,只有當你insert資料後才分配空間。

expdp

建立邏輯目錄

create directory DUMP_DIR as '/oracle/DUMP_DIR';


在伺服器上建立該目錄,因為Oracle並不會自動建立,如果目錄不存在匯出會報錯

mkdir -p /oracle/DUMP_DIR


給使用者授予在該目睹讀取的許可權

grant read,write on directory DUMP_DIR to scott;


按使用者導

expdp scott/tiger@localhost:1521/ora11g schemas=scott dumpfile=expdp_export.dmp DIRECTORY=DUMP_DIR;


並行程序parallel

expdp scott/tiger@localhost:1521/ora11g directory=DUMP_DIR dumpfile=expdp_export.dmp parallel=40 job_name=expdp40


按表名導

expdp scott/tiger@localhost:1521/ora11g TABLES=emp,dept dumpfile=expdp_export.dmp DIRECTORY=DUMP_DIR;


按查詢條件導

expdp scott/tiger@localhost:1521/ora11g directory=DUMP_DIR dumpfile=expdp_export.dmp tables=emp query='WHERE deptno=20';


按表空間導

expdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLESPACES=temp,example;


導整個資料庫

expdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp FULL=y;


匯入

imp

將exp_export.dmp 中的資料匯入 TEST資料庫中。

imp system/manager@hostname:1521/ora11g file=exp_export.dmp


上面可能有點問題,因為有的表已經存在,然後它就報錯,對該表就不進行匯入。
在後面加上 ignore=y 就可以了,但是這樣匯入的資料可能會出現重複現象
imp system/manager@hostname:1521/ora11g full=y file=exp_export.dmp ignore=y


將exp_export.dmp 中的表table1,table2匯入

imp system/manager@hostname:1521/ora11g file=將exp_export.dmp tables=table1,table2


impdp

導到指定使用者下

impdp scott/tiger DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp SCHEMAS=scott;


改變表的owner

impdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;


匯入表空間

impdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp TABLESPACES=example;


匯入資料庫

impdb system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp FULL=y;


追加資料

impdp system/manager DIRECTORY=DUMP_DIR DUMPFILE=expdp_export.dmp SCHEMAS=system TABLE_EXISTS_ACTION=append

使用impdp完成資料庫匯入時,若表已經存在,有四種的處理方式:

1)  skip:預設操作
2)  replace:先drop表,然後建立表,最後插入資料
3)  append:在原來資料的基礎上增加資料
4)  truncate:先truncate,然後再插入資料

總結

EXP和IMP是客戶端工具程式,它們既可以在客戶端使用,也可以在服務端使用。
EXPDP和IMPDP是服務端的工具程式,他們只能在ORACLE服務端使用,不能在客戶端使用。
IMP只適用於EXP匯出的檔案,不適用於EXPDP匯出檔案;IMPDP只適用於EXPDP匯出的檔案,而不適用於EXP匯出檔案。
EXP不能匯出分割槽表,而EXPDP可以