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/aml@amldb 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/aml@amldb 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/aml@amldb 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/aml@amldb 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/aml@amldb 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/aml@amldb 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/aml@amldb 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/aml@amldb 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/dbo_pwcdb@BSY2-NQQX-IP50 file=d:\daochu_noData.dmp tables=
(PRNBSN_CONTENTTEMPLATE,PRNBSN_RELINFOCONTENT) rows=n;
2  导入 dbo_smydb 账户
imp dbo_smydb/dbo_smydb@BSY2-SMY-IP11 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/aml@amldb登录数据库

 (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/zftang@fgisdb  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/zftang@fgisdb  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/zftang@fgisdb 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/zftang@fgisdb dumpfile =full.dmp full=y logfile=full.log directory=dir_dp job_name=my_job


(6)按用户导出

expdp zftang/zftang@fgisdb 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/gwm@fgisdb  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/gwm@fgisdb 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,所以这样的导出会报错,你可以选择授权或者使用有权限的用户

[oracle@asm11g ~]$ 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


[oracle@asm11g ~]$ 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对象指定的目录
[oracle@asm11g ~]$ 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,如果定义了这个目录对象,并授予权限,在用户导出没有指定

具体的目录参数的情况下,导出文件会到这个目录

[oracle@asm11g ~]$ mkdir sdir
[oracle@asm11g ~]$ 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

[oracle@asm11g ~]$ 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的话,环境变量会覆盖缺省设置
[oracle@asm11g ~]$ mkdir udir
[oracle@asm11g ~]$ 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

注意,在定义环境变量的时候,后面的值要用大写
[oracle@asm11g ~]$ export DATA_PUMP_DIR=udir
[oracle@asm11g ~]$ 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


[oracle@asm11g ~]$ export DATA_PUMP_DIR=UDIR
[oracle@asm11g ~]$ 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,会使用参数指定的目录
[oracle@asm11g ~]$ 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

四、如果每个文件都指定了具体的目录的话,会以文件定义的目录为准
[oracle@asm11g ~]$ mkdir userdir
[oracle@asm11g ~]$ 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

[oracle@asm11g ~]$ 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/$password@$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

猜你喜欢

转载自blog.csdn.net/z453588/article/details/83795172