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

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

大家好,欢迎来到IT知识分享网。

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” 变量,添加

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/114784.html

(0)
上一篇 2025-12-06 18:20
下一篇 2025-12-06 18:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信