---
title: "Oracle数据库备份与恢复之exp/imp"
author: "slbszq"
date: "2014-01-15T15:48:59Z"
tags: ["Oracle数据库", "exp", "imp", "备份", "恢复"]
categories: ["数据库"]
slug: "oracle-exp-imp"
draft: false
lastmod: "2014-01-15T15:48:59Z"
id: 2004375
url: "https://it4j.iteye.com/blog/2004375"
---

**Oracle数据库备份与恢复之exp/imp**

**1.1  基本命令**

    **1.**  **获取帮助**

exp help=y

导出使用exp命令来完成的，该命令常用的选项有：

**userid：** 用于指定执行导出操作的用户名，口令，连接字符串

**tables：** 用于指定执行导出操作的表

**owner：** 用于指定执行导出操作的方案

**full=y：** 用于指定执行导出操作的数据库

**rows：** 用于指定执行导出操作是否要导出表中的数据

**file：** 用于指定导出文件名

imp help=y

imp常用的选项有：

**userid：** 用于指定执行导入操作的用户名，口令，连接字符串

**tables：** 用于指定执行导入操作的表

**formuser：** 用于指定源用户

**touser：** 用于指定目标用户

**file：** 用于指定导入文件名

**full=y：** 用于指定执行导入整个文件

**inctype：** 用于指定执行导入操作的增量类型

**rows：** 指定是否要导入表行（数据）

**ignore：** 如果表存在，则只导入数据

    **2.  三种工作方式**

    （1）交互式方式

    exp        //  然后按提示输入所需要的参数

    **（2）命令行方式**

    exp [user/pwd@dbname](mailto:user/pwd@dbname) file=/oracle/test.dmp full=y  //命令行中输入所需的参数

   （3）参数文件方式

    exp parfile=username.par    //  在参数文件中输入所需的参数

参数文件 username.par 内容 userid=username/userpassword buffer=8192000 compress=n grants=y file=/oracle/test.dmp full=y

 

    **3.  三种模式**

**（1）表方式，将指定表的数据导出/导入。**

**导出：**导出一张或几张表：

**导出格式：exp用户名/密码@数据库实例file=保存路径 tables = ...**

a.**导出自己的表**

**expuserid**=scott/tiger@myoral **tables**=(emp,dept) **file**=d:\\e1.dmp

注：userid= 可以省略，但scott/tiger@myoral必须紧跟exp的后面.

b.**导出其它方案的表**

如果用户要导出其它方案的表，则需要dba的权限或是exp\_full\_database的权限，比如system就可以导出scott的表

**expuserid**=system/manager@myoral **tables**=(scott.emp) **file**=d:\\e2.emp

c.**导出表的结构**

**expuserid**=scott/tiger@accp **tables**=(emp) **file**=d:\\e3.dmp **rows**=n

d. 使用**直接导出方式**

**expuserid**=scott/tiger@accp **tables**=(emp) **file**=d:\\e4.dmp **direct**=y

这种方式比默认的常规方式**速度要快**，当数据量大时，可以考虑使用这样的方法。 这时需要数据库的**字符集**要与客户端字符集**完全一致**，**否则会报错**...   

e. 导出某张**表的部分数据**

经测试,在windows平台只能使用参数文件方式使用query能正常导出

在D:\\  创建a.txt 内容：file=d:\\someemp.dmp tables=emp query="where deptno=10"

exp user/pwd@orcl parfile=d:\\a.txt

**导入：**导入一张或几张表

**导入格式：imp用户名/密码@数据库实例file=保存路径tables = ...**

a. **导入自己的表**

imp userid=scott/tiger@myorcl tables=(emp) file=d:\\xx.dmp

b. 导入**表到其它用户** 要求该用户具有**dba的权限**，或是imp\_full\_database

imp userid=system/tiger@myorcl tables=(emp) file=d:\\xx.dmp **touser=scott**

c. 导入**表的结构** 只导入表的结构而不导入数据

imp userid=scott/tiger@myorcl tables=(emp) file=d:\\xx.dmp **rows=n**

d. 导入**数据** 如果对象（如比表）已经存在可以只导入表的数据

imp userid=scott/tiger@myorcl tables=(emp) file=d:\\xx.dmp **ignore=y**   

 

**（2）用户（方案）方式**，将指定用户的所有对象及数据导出/导入。

**导出格式：exp用户名/密码@数据库实例file=保存路径 owner = ...**

a. 导出**自己的方案**

exp userid=scott/tiger@myorcl **owner**=scott **file**=d:\\scott.dmp

b. 导出**其它方案**

如果用户要导出其它方案，则需要**dba的权限**或是exp\_full\_database的权限，比如system用户就可以导出任何方案

exp userid=system/manager@myorcl **owner**=(system,scott) **file**=d:\\system.dmp

**导入格式：imp用户名/密码@数据库实例file=保存路径 touser = ...**

1． 导入**自身的方案**

imp userid=scott/tiger file=d:\\xxx.dmp

2． 导入**其它方案** 要求该用户具有**dba的权限**

imp userid=system/manager file=d:\\xxx.dmp fromuser=system touser=scott

 

**（3）全库（数据库）方式**，将数据库中的所有对象导出/导入导出：

**导出数据库：expuserid**=**system/manager@orclfull=yfile**=d:\\all.dmp

**导入数据库：**

在默认情况下，当导入数据库时，会**导入所有对象结构和数据**，案例如下：

 

**imp userid**=system/manager **full=y file**=d:\\xxx.dmp