Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  397 随笔 :: 33 文章 :: 29 评论 :: 0 Trackbacks
Oracle imp和exp的使用
 
 
    入行太晚,没接触过早期的Oracle版本,所以一直都是用PLSQL Developer做数据的exp和imp。现在要写定制脚本了才发现,自己居然对命令行模式下的这两个命令很不了解,于是只要再来学一遍基础。了解了一遍之后发现这两个工具的有些细节还是很值得注意一下的,下面看一下详细的用法:
 
 
一、exp工具的使用
 
    按照惯例,首先先来看一下自带的help文档:
 
[oracle@misdwh db_1]$ exp help=y
 
Export: Release 10.2.0.1.0 - Production on 星期三 6月 24 15:59:14 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
You can let Export prompt you for parameters by entering the EXP
command followed by your username/password:
 
     Example: EXP SCOTT/TIGER
 
Or, you can control how Export runs by entering the EXP command followed
by various arguments. To specify parameters, you use keywords:
 
     Format:  EXP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
     Example: EXP SCOTT/TIGER GRANTS=Y TABLES=(EMP,DEPT,MGR)
               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
 
USERID must be the first parameter on the command line.
 
Keyword    Description (Default)      Keyword      Description (Default)
--------------------------------------------------------------------------
USERID     username/password          FULL         export entire file (N)
BUFFER     size of data buffer        OWNER        list of owner usernames
FILE       output files (EXPDAT.DMP)  TABLES       list of table names
COMPRESS   import into one extent (Y) RECORDLENGTH length of IO record
GRANTS     export grants (Y)          INCTYPE      incremental export type
INDEXES    export indexes (Y)         RECORD       track incr. export (Y)
DIRECT     direct path (N)            TRIGGERS     export triggers (Y)
LOG        log file of screen output  STATISTICS   analyze objects (ESTIMATE)
ROWS       export data rows (Y)       PARFILE      parameter filename
CONSISTENT cross-table consistency(N) CONSTRAINTS  export constraints (Y)
 
OBJECT_CONSISTENT    transaction set to read only during object export (N)
FEEDBACK             display progress every x rows (0)
FILESIZE             maximum size of each dump file
FLASHBACK_SCN        SCN used to set session snapshot back to
FLASHBACK_TIME       time used to get the SCN closest to the specified time
QUERY                select clause used to export a subset of a table
RESUMABLE            suspend when a space related error is encountered(N)
RESUMABLE_NAME       text string used to identify resumable statement
RESUMABLE_TIMEOUT    wait time for RESUMABLE
TTS_FULL_CHECK       perform full or partial dependency check for TTS
VOLSIZE              number of bytes to write to each tape volume
TABLESPACES          list of tablespaces to export
TRANSPORT_TABLESPACE export transportable tablespace metadata (N)
TEMPLATE             template name which invokes iAS mode export
 
Export terminated successfully without warnings.
 
    发现参数还挺多的,但是平常用到的就没有这么多了,也就那么5、6个参数比较重要,下面随便举几个例子看一下就可以了:
 
1、导出全库备份数据
 
$ exp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/full_db.dmp full=y
 
注意:如果服务端和客户端之间的字符集不同,会有报错,提示如下:
EXP-00091: Exporting questionable statistics.

对实际的数据影响不大,如果看着不舒服的话,可以通过修改客户端的字符集来消除这个错误:
WINNT> set NLS_LANG=AMERICAN_AMERICA.UTF8
LINUX> export NLS_LANG=AMERICAN_AMERICA.UTF8
 
2、导出某用户下的所有objects
 
$ exp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/rman_db.dmp owner=rman,wangxiaoqi log=/u01/oracle/devMISowb/exp/exp.log
 
3、导出某几张表
 
$ exp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/tables_db.dmp tables=rman.bp,wangxiaoqi.t1_k
 
注:很奇怪的事情,按照help里说的,几个列举的用户/表名应该是要放在括号里的,但是我每次加了括号就报错,没有括号就对了,非常奇怪,错误提示如下:
-bash: syntax error near unexpected token `('
 
4、导出某个tablespace中的内容
 
$ exp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/tbs_db.dmp tablespaces=RECOVERY_TBS
 
5、使用SQL导出table的一个子集
 
$ exp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/sub_query.dmp tables=rman.bp query=\"where bp_key='266'\"
 
注:query中不是完整的sql,仅是子句,且“"”符号前需要加入“\”转义
 
6、使用提示模式进行导出
 
$ exp wangxiaoqi/wangxiaoqi
 
在这个模式下,exp会提示你输入所有的参数进行导出。
 
 
 
二、imp工具的使用
 
    同样先看一下help文档,与exp非常相似:
 
[oracle@misdwh exp]$ imp help=y
 
Import: Release 10.2.0.1.0 - Production on Wed Jun 24 16:51:50 2009
 
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
 
 
You can let Import prompt you for parameters by entering the IMP
command followed by your username/password:
 
     Example: IMP SCOTT/TIGER
 
Or, you can control how Import runs by entering the IMP command followed
by various arguments. To specify parameters, you use keywords:
 
     Format:  IMP KEYWORD=value or KEYWORD=(value1,value2,...,valueN)
     Example: IMP SCOTT/TIGER IGNORE=Y TABLES=(EMP,DEPT) FULL=N
               or TABLES=(T1:P1,T1:P2), if T1 is partitioned table
 
USERID must be the first parameter on the command line.
 
Keyword  Description (Default)       Keyword      Description (Default)
--------------------------------------------------------------------------
USERID   username/password           FULL         import entire file (N)
BUFFER   size of data buffer         FROMUSER     list of owner usernames
FILE     input files (EXPDAT.DMP)    TOUSER       list of usernames
SHOW     just list file contents (N) TABLES       list of table names
IGNORE   ignore create errors (N)    RECORDLENGTH length of IO record
GRANTS   import grants (Y)           INCTYPE      incremental import type
INDEXES  import indexes (Y)          COMMIT       commit array insert (N)
ROWS     import data rows (Y)        PARFILE      parameter filename
LOG      log file of screen output   CONSTRAINTS  import constraints (Y)
DESTROY                overwrite tablespace data file (N)
INDEXFILE              write table/index info to specified file
SKIP_UNUSABLE_INDEXES  skip maintenance of unusable indexes (N)
FEEDBACK               display progress every x rows(0)
TOID_NOVALIDATE        skip validation of specified type ids
FILESIZE               maximum size of each dump file
STATISTICS             import precomputed statistics (always)
RESUMABLE              suspend when a space related error is encountered(N)
RESUMABLE_NAME         text string used to identify resumable statement
RESUMABLE_TIMEOUT      wait time for RESUMABLE
COMPILE                compile procedures, packages, and functions (Y)
STREAMS_CONFIGURATION  import streams general metadata (Y)
STREAMS_INSTANTIATION  import streams instantiation metadata (N)
VOLSIZE                number of bytes in file on each volume of a file on tape
 
The following keywords only apply to transportable tablespaces
TRANSPORT_TABLESPACE import transportable tablespace metadata (N)
TABLESPACES tablespaces to be transported into database
DATAFILES datafiles to be transported into database
TTS_OWNERS users that own data in the transportable tablespace set
 
Import terminated successfully without warnings.
    再来举几个imp的用法例子:
 
1、简单的全库导入
 
$ imp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/full_db.dmp full=y
 
注:数据导入时有可能会报错。主要原因有以下几种:
    A. 导入的BOJECTS原不属于当前连接的用户的
    B. 导入的BOJECTS已经存在
    C. 原用户未找到
    D. 导入库与导入文件的字符集不同
 
2、导入到指定的用户下:
 
$ imp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/rman_db.dmp fromuser=rmantouser=wangxiaoqi
 
注:即将rman用户下的所有对象均导入到wangxiaoqi中
 
3、 忽略/插入数据
 
$ imp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/rman_db.dmp ignore=y
 
注:ignore=y的意思是不管原objects是否存在,均将直接插入到相应对象(并且如果导入的对象里面有其他的对象,如约束,索引等,会在数据插入后被创建)
 
4、导入指定的表
 
$ imp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/rman_db.dmp tables=bp,bs
 
5、导入时忽略约束、索引、行
 
$ imp wangxiaoqi/wangxiaoqi file=/u01/oracle/devMISowb/exp/rman_db.dmp tables=bp,bs constraints=N indexs=N rows=N
 
注意:导入的用户必须要有create相应的导入objects的权限
 
 
三、总结
 
    上面举的例子基本上够用了,需要注意的是导入、导出时的字符集的关系,这在我之前的文章里已经记载过详细的说明,就不再记了。对于其他比较少用的子句,就等以后用到了再研究一下吧,应该不太难理解。
 
    OK,到此结束。
 
 
 
 
 
posted on 2009-06-22 20:32 decode360 阅读(2105) 评论(0)  编辑  收藏 所属分类: 10.DB_Tools

只有注册用户登录后才能发表评论。


网站导航: