ORACLE 导入导出
1. 介绍
EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用
EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户端使用
IMP只适用于EXP导出的文件,不适用于EXPDP导出文件;IMPDP只适用于EXPDP导出的文件,而不适用于EXP导出文件
2. exp\impl形式导入导出
注意 exp形式不能直接导出空表,在使用exp前需要检查
2.1 查看空表
[root@ifp2 admin]# su oracle
[oracle@ifp2 admin]$ sqlplus / as sysdba
SQL> show parameter deferred_segment_creation;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
deferred_segment_creation boolean FALSE
数据库参数deferred_segment_creation=true将导致dmp备份无法导出空表结构
执行下面语句, 只对以后的表有效,之前的表还是没分配空间
SQL> alter system set deferred_segment_creation=false;
为空表分配表空间, 通过SQL拼接SQL语句, 并执行
SQL> select 'alter table ' || table_name || ' allocate extent;' from user_tables where segment_created = 'NO';
2.2 exp 导出
- 将数据库用户名HM_DEV 密码123456 导出到/u01中
[root@ifp2 admin]# exp HM_DEV/123456 owner=HM_DEV file=/u01/HM_DEV_2019_01_17.dmp full=y;
- 将数据库中的表inner_notify、notify_staff_relat导出
[root@ifp2 admin]# exp HM_DEV/123456@orcl owner=HM_DEV file=/u01/HM_DEV_2019_01_17.dmp tables=\(inner_notify, notify_staff_relat\);
- 将数据库中的表table1中的字段filed1以"00"打头的数据导出
[root@ifp2 admin]# exp HM_DEV/123456@orcl owner=HM_DEV file=/u01/HM_DEV_2019_01_17.dmp tables=\(table1\) query=/" where filed1 like 00%/";
上面是常用的导出,对于压缩,既用winzip把dmp文件可以很好的压缩。 也可以在上面命令后面 加上 compress=y 来实现
2.3 imp 导入
- 将文件file=/u01/HM_DEV_2019_01_17.dmp 导入数据库中
[root@ifp2 admin]# imp HM_DEV_1/123456@orcl file=HM_DEV_2019_01_17.dmp ignore=y FULL=Y;
- 将文件file=/u01/HM_DEV_2019_01_17.dmp 导入数据库中的表
[root@ifp2 admin]# imp HM_DEV_1/123456@orcl file=/u01/HM_DEV_2019_01_17.dmp tables=(table1);
3. expdp、 impdpd 导入导出
ORACLE目录
SQL> select * from dba_directories;
SQL> select * from dba_directories where DIRECTORY_NAME='DATA_PUMP_DIR';
/data/u01/app/oracle/admin/orcl/dpdump/
导出文件路径 /data/u01/app/oracle/admin/orcl/dpdump/
[root@ifp2 admin]# expdp HM_DEV/123456@orcl schemas=HM_DEVdirectory=data_pump_dir dumpfile=EIS_$(date +%Y%m%d)_DEV.dmp compression=all full=y cluster=n;
3.1 expdp导出
- 按用户导
[root@ifp2 admin]# expdp scott/tiger@orcl schemas=scott directory=data_pump_dir dumpfile=scott.dmp;
- 并行进程parallel
[root@ifp2 admin]# expdp scott/tiger@orcl schemas=scott directory=data_pump_dir dumpfile=scott.dmp parallel=40 job_name=scott3;
- 按表名导
[root@ifp2 admin]# expdp scott/tiger@orcl schemas=scott directory=data_pump_dir dumpfile=expdp.dmp TABLES=emp,dept;
- 按查询条件导
[root@ifp2 admin]# expdp scott/tiger@orcl schemas=scott directory=data_pump_dir dumpfile=expdp.dmp Tables=emp query='WHERE deptno=20';
- 按表空间导
[root@ifp2 admin]# expdp system/manager schemas=scott directory=data_pump_dir dumpfile=tablespace.dmp TABLESPACES=temp,example;
- 导整个数据库
[root@ifp2 admin]# expdp system/admin@orcl dumpfile=full.dmp full=y cluster=n;
有时候导出会出错,说找到不到日志之类的,这个时候在最后面加上cluster=n
impdp有exist_table_action参数,可以设置为replace。这样可以覆盖导入
3.2 impdpd导入
- 导到指定用户下
对于后面的scott2,系统中可以有也可以没有,如果没有系统会自动建立这个用户。
[root@ifp2 admin]# impdp system/admin@orcl DUMPFILE=scott.dmp REMAP_SCHEMA=scott:scott2; (加上一直出错?DIRECTORY=DATA_PUMP_DIR)
- 改变表的owner
[root@ifp2 admin]# impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;
- 导入表空间
[root@ifp2 admin]# impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=tablespace.dmp TABLESPACES=example;
- 导入数据库
[root@ifp2 admin]# impdb system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=full.dmp FULL=y;
- 追加数据
[root@ifp2 admin]# impdp system/manager DIRECTORY=DATA_PUMP_DIR DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION=append;
- 导入到普通数据库
[root@ifp2 admin]# impdp EIS_DEV/123456 directory=data_pump_dir dumpfile=xxx.dmp REMAP_SCHEMA=EIS_DEV:EIS_DEV full=y;