Oracle 导出、导入详解(expdp、impdp、exp、imp)

1 概述

1.1 expdp、impdp 和 exp、imp 区别

expdp impdp exp imp
导出 × ×
导入 × ×
使用场景 仅服务端使用 服务端 和 客户端 均能使用
适用版本 Oralce 11g 之后 Oracle 10g 机之前
效率 较高 略低
其它 expdp 只能和 impdp 搭配使用 exp 只能和 imp 搭配使用

expdp(export dump):导出。 读取数据库并将结果集写入到 “导出转储文件(Export Dump File)” 的二进制文件中
impdp(import dump):导入。读取上述二进制文件并写入到数据库

1.2 使用建议

  • 建议将命令写在一行。避免因换行导致部分命令未执行
  • 建议在 Windows 运行窗口执行

2 expdp、impdp

-- 通过以下命令,可查询 expdp 支持的参数,impdp 同理
expdp help=y

在这里插入图片描述

2.1 前提:创建 directory 对象

-- 1.先看 directory 对象是否存在
select * from dba_directories t where t.directory_name = 'MYDIR';

-- 2.若不存在,则创建
create directory mydir as 'D:\mydir';

授权(若非 system 用户,则需要,如 scott):

-- 授予 create 权限
grant create any directory to scott;
-- 授予 read,write 权限
grant read, write on directory mydir to scott;

-- 查看拥有 directory 相关权限的用户
select * from dba_sys_privs t where t.privilege like '%DIRECTORY%';
-- 查询拥有 read,write 相关权限的用户
select * from dba_tab_privs t where t.privilege in ('READ', 'WRITE');

示例:

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 
Connected as system@orcl

SQL> create directory mydir as 'D:\mydir';
Directory created

SQL> grant read, write on directory mydir to scott;
Grant succeeded

SQL> select * from dba_directories t where t.directory_name = 'MYDIR';
OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH
------------------------------ ------------------------------------------- 
SYS                            MYDIR                          D:\mydir

SQL> 

2.2 导出 expdp

模式(dumpfile)描述
full全库
schema模式
table
tablespace表对象
-- 实际开发中,只保留一个空格、不换行,此处仅为了 "显示差异"
expdp 用户名/密码@数据库    目录            转储文件(二进制)         导出模式           是否开启日志
expdp scott/scott@orcl   directory=mydir dumpfile=tables.dmp      tables=emp,dept   logfile=tables.log
expdp scott/scott@orcl   directory=mydir dumpfile=schemas.dmp     schemas=scott     logfile=schemas.log
expdp scott/scott@orcl   directory=mydir dumpfile=tablespaces.dmp tablespaces=users logfile=tablespaces.log
expdp system/system@orcl directory=mydir dumpfile=full.dmp        full=Y            nologfile=Y

例1:导出 scott 下的表 emp

-- 其中 mydir、emp.dmp、emp.log 为自定义名称
expdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp logfile=emp.log

-- 导出多张表
expdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp,dept logfile=emp.log

例2:导出 scott 下的表 emp 中符合条件的记录

expdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp query='emp:"WHERE deptno=10 AND sal>2000"' logfile=emp.log

命令行截图:
在这里插入图片描述

物理文件截图:
在这里插入图片描述

2.3 导入 impdp

例1:导入 scott 下的表 emp

-- 一般格式
impdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp logfile=emp.log

-- 导入,追加
impdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp table_exists_action=APPEND

3 exp、imp

-- 通过以下命令,查询看 exp 支持的参数,imp 同理
exp help=y

在这里插入图片描述

3.1 导出 exp

模式描述
full全库
owner用户
tables
-- 1.全库导出
exp 用户名/密码 file=文件路径 full=Y

-- 2.按用户导出
exp 用户名/密码 file=文件路径 owner=用户

-- 3.按表导出
exp 用户名/密码 file=文件路径 tables=(1,2, 表n)

示例1:导出 scott 下的表 emp 和 dept

set name=demo_%date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%%time:~3,2%%time:~6,2%
exp scott/tiger@orcl  tables=(emp, dept)  file=D:\Demo备份\%name%.dmp statistics=none

注:以上可保存为 .bat 文件,双击运行

在这里插入图片描述

3.2 导入 imp

  • 导入 imp 与 导出 emp 命令基本一致
-- 导入表 emp 和 dept
imp soctt/tiger@orcl tables=(emp, dept) file=D:\Demo备份\备份.dmp

4 扩展

4.1 Windows 运行窗口

  • 快捷键 Win + r 打开运行窗口,并输入 cmd

在这里插入图片描述

cls:清屏命令

4.2 expdp 不是内部或外部命令

  • 报错截图:
    在这里插入图片描述
  • 解决办法:配置环境变量
    • ① 找到 Oracle 客户端 bin 目录 的路径,如:C:\orac1e\product\12.1.0\dbhome_1\bin
    • ② 在 “系统变量” 中选中 “Path” 变量,添加

4.3 将视图中的数据导出

  • 无法直接从 视图(view) 中导出数据,需先将数据插入 临时表 中,再进行导入操作!
### Oracle 数据库 expdp 导入导出使用方法 #### 准备工作 为了成功进行数据的导入导出操作,需确保目标环境已准备好。这包括但不限于确认源端与目的端之间的网络连通性正常,以及验证用于执行这些任务的服务账户具有足够的权限[^1]。 #### 使用 `expdp` 进行数据导出 `expdp` 是一种基于 Data Pump 技术实现的数据迁移工具,它提供了多种特性来支持复杂的企业级需求。以下是具体的操作指南: - **基本语法** ```bash expdp username/password@database_service_name parameters=value ... ``` - **实例展示** 要导出特定用户的全部数据到名为 `example.dmp` 的转储文件中,并将其保存至由目录对象 `DUMP_DIR` 所指向的位置,同时记录日志信息于 `export.log` 文件内,则可以按照如下命令格式输入: ```bash expdp system/manager@orcl schemas=SCOTT dumpfile=example.dmp directory=DUMP_DIR logfile=export.log ``` 此过程会启动一个新的Data Pump作业并依据给定参数集开始导出流程[^4]。 #### 使用 `impdp` 实现数据重载 当需要将之前通过 `expdp` 创建好的 `.dmp` 文件中的内容重新加载回另一个Oracle实例时,应当采用与其配套设计的 `impdp` 工具来进行这项工作。下面列举了一些常用的选项及其含义说明: - **基础调用结构** ```bash impdp username/password@target_database_service_name parameters=value ... ``` - **实际案例分析** 如果希望从位于 `/u01/app/oracle/dpdump/example.dmp` 处获取先前备份下来的资料,并且映射到当前环境中叫做 `NEW_SCOTT` 的新架构之下,那么可参照下列指令模板执行相应动作: ```bash impdp system/manager@new_orcl remap_schema=SCOTT:NEW_SCOTT dumpfile=example.dmp directory=DUMP_DIR table_exists_action=replace ``` 这里特别指定了 `table_exists_action=replace` 参数用来指示如果遇到同名表格存在的情况就直接覆盖掉旧版本[^2]。 #### 特殊注意事项 在整个过程中有几个要点值得注意: - 确认所涉及的所有路径都已经被正确配置成有效的操作系统级别访问地址; - 对于大型项目来说,建议提前规划好临时存储空间大小以免中途因为空间不足而失败; - 尽量保持源端跟目的地之间软件版本一致以减少潜在兼容性风险; - 利用官方文档进一步探索更多高级设置项以便更好地满足个性化业务场景下的特殊要求[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼丸丶粗面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值