1. 程式人生 > 其它 >expdp/impdp資料泵匯出/匯入資料庫

expdp/impdp資料泵匯出/匯入資料庫

expdp/impdp資料泵匯出/匯入資料庫

1 建立directory

1.1 建立作業系統目錄

mkdir /home/oracle/dir1

1.2 建立資料庫目錄

create directory dir1 as '/home/oracle/dir1';
grant read,write on directory dir1 to public;

1.3 檢視資料庫目錄

col directory_name for a25
col DIRECTORY_PATH for a80
select directory_name,directory_path from dba_directories;

2 檢視匯出/匯入工具幫助

expdp help=y
impdp help=y

3 【實戰一】匯出資料庫使用者下的表

3.1 匯出scott的emp和dept表

expdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_dept.dmp tables=emp,dept logfile=expdp_scott_emp_dept.log

3.2 模擬scott下emp和dept表被誤刪除

drop table emp purge;
drop table dept purge;

3.3 匯入emp和dept表

impdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_dept.dmp logfile=impdp_scott_emp_dept.log

3.4 驗證資料

select * from emp;
select * from dept;

4 【實戰二】匯出資料庫使用者下的emp表的資料和結構

content引數:

  • data_only:只匯入表資料,表資料是指表中的所有資料
  • metadata_only:只匯入元資料,元資料指的是指表結構
  • all:預設引數直接匯入表(包括metadata和data,即預設content=all),則不會檢查關係和約束,因為是先匯入資料,再匯入相關表約束關係

注意:若先匯入metadata,再匯入data_only,那麼在匯入data_only時,則會檢查表的相關約束、triger關係,

4.1 匯出emp表的表資料

expdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_data.dmp tables=emp logfile=expdp_scott_emp_data.log content=data_only reuse_dumpfiles=y

4.2 匯出emp的元資料

expdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_metadata.dmp tables=emp logfile=expdp_scott_emp_metadate.log content=metadata_only reuse_dumpfiles=y

4.3 模擬scott下emp和dept表被誤刪除

drop table emp purge;
drop table dept purge;

4.4 匯入emp的元資料

impdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_metadata.dmp logfile=impdp_scott_emp_metadate.log

驗證表結構:

select * from emp;
desc emp;

4.5 匯入emp表資料

impdp scott/tiger directory=dir1 dumpfile=expdp_scott_emp_data.dmp logfile=impdp_scott_emp_data.log

4.6 驗證資料

select * from emp;

5 【實戰三】匯出表中的部分行

5.1 匯出scott使用者emp表中

expdp scott/tiger directory=dir1 dumpfile=expdp_emp_dept_10.dmp tables=emp logfile=expdp_emp_dept_10 query="'where deptno=10'"

5.2 模擬scott表使用者下emp表中部門編號為10的資料被刪除

delete from emp where deptno=10;
select * from emp;

5.3 匯入emp表中部門編號為10的資料為其它表名

impdp scott/tiger directory=dir1 dumpfile=expdp_emp_dept_10.dmp logfile=impdp_emp_dept_10.log remap_table=emp:emp10

5.4 驗證資料

select * from emp;
select * from emp10;

可以把建立的emp10表中的資料重新插入到emp表中:

insert into emp select * from emp10;
select * from emp;

6 【實戰四】匯出資料庫中的某個使用者

6.1 匯出scott使用者

expdp system/oracle directory=dir1 dumpfile=user_scott.dmp logfile=user_scott.log schemas=scott

6.2 模擬sys使用者誤操作刪除scott使用者

drop user scott cascade;

6.3使用remap將scott物件匯入為其他使用者名稱

impdp system/oracle directory=dir1 dumpfile=user_scott.dmp logfile=impdp_user_scott.log remap_schema=scott:scott10

6.4 驗證資料

conn scott10/tiger
select table_name from user_tables;