expdp/impdp数据泵导出/导入数据库

expdp/impdp数据泵导出/导入数据库

注:在导出和导入过程中加入logfile日志,有助于后期排错

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 【实战二】导出数据库用户下的表的数据和结构

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

reuse_dumpfiles=y参数:如果目录下有重名的dump文件直接替换掉

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表被误删除

drop table emp 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'"

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

remap_table=emp:emp10参数:将导入的数据的表名由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

schemas=scott参数:schema原译为模式,在此指的是数据库的用户

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

remap_schema=scott:scott10参数:将scott对象导入为其他名字scott10,若不想要改名字,将scott10改为scott即可

6.4 验证数据

conn scott10/tiger
select table_name from user_tables;

posted @ 2021-07-12 15:44  chchcharlie、  阅读(391)  评论(0编辑  收藏  举报