文章目录
1 概述
1. 导出:expdp(export dump):读取数据库并将结果集写入到 '导出转储文件'(Export Dump File)的二进制文件中
导入:impdp(import dump):读取上述二进制文件并写入到数据库
2. 注意项
(1) 导入、导出命令几乎无差异 -- 若未特别说明,则可相互通用
(2) 建议命令 '写在一行'(切勿换行,避免有些命令未执行)
(3) 在 Windows '运行窗口' 执行导入、导出命令
2 示例
2.1 前提:创建 directory 对象
1. 定义 'directory 对象'
(1) 查询目录对象是否已定义
select * from dba_directories t where t.directory_name = 'MYDIR';
-- drop directory [MYDIR]; -- 删除
(2) 若不存在,则创建,若提示权限不足,请看 '2. 授权,若需要'
create directory mydir as 'D:\mydir';
同时鼠标右键 -> 新建文件夹(与上述路径一致)
2. 授权,若需要 -- system 用户授权即可
权限授予:
(1) grant create any directory to scott;
(2) grant read, write on directory mydir to scott;
权限查询:
(1) select * from dba_sys_privs t where t.privilege like '%DIRECTORY%';
(2) 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
1. 导出模式,有四中,按需选择即可
(1) full (全库)
(2) schema(模式)
(3) table (表)
(4) tablespace(表空间)
2. 打开运行 windows 的运行窗口:
(1) 快捷键 win + r,并输入 cmd
(2) cls:清屏命令
例1:导出 scott 下的表 emp
-- 其中 mydir、emp.dmp、emp.log 为自定义名称
C:\> expdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp logfile=emp.log
-- 当然也可以导出多张表,用 逗号 "," 隔开即可
-- 实际开发中,只保留一个空格、不换行(同上),此处仅为了 "显示差异"
expdp 用户名/密码@数据库 目录 转储文件(二进制) 导出模式 是否开启日志
C:\> expdp scott/scott@orcl directory=mydir dumpfile=tables.dmp tables=emp,dept logfile=tables.log
C:\> expdp scott/scott@orcl directory=mydir dumpfile=schemas.dmp schemas=scott logfile=schemas.log
C:\> expdp scott/scott@orcl directory=mydir dumpfile=tablespaces.dmp tablespaces=users logfile=tablespaces.log
C:\> expdp system/system@orcl directory=mydir dumpfile=full.dmp full=Y nologfile=Y
例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
C:\> impdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp logfile=emp.log
特别的:追加导入
C:\> impdp scott/scott@orcl directory=mydir dumpfile=emp.dmp tables=emp table_exists_action=APPEND
3 扩展
3.1 expdp / exp 和 impdp / imp 的区别
1. 相同点
(1) expdp 与 exp 都是导出
(2) impdp 与 imp 都是导入
2. 不同点
(1) '执行效率' 不同:expdp / impdp 明显高于 exp / imp
-- exp / imp:Oracle 10g 前使用
-- expdp / impdp 是 Oracle 11g 及以后推荐使用的
(2) '使用场景' 不同
exp 和 imp 是 '客户端' 程序,它们既可以在客户端使用,又可以在服务端使用
expdp 和 impdp 是 '服务端' 程序,他们 '只能在服务端' 使用,无法在客户端使用
(3) '导入格式' 不同
imp 只适用于 exp 导出的文件
impdp 只适用于 expdp 导出的文件
(4) 对于 10g 以上,0 行数据的 '空表' 导出不同:exp 报错,只能用 expdp
3.2 ORA-39070: 无法打开日志文件
解决办法:
(1) 创建 directory 中定义的文件夹(DIRECTORY_PATH 中定义的路径)
(2) 授权,如: grant read, write on directory mydir to scott;