Oracle 的「导入」和「导出」

Oracle 的「导入」和「导出」是为数据库的备份和恢复而生的。备份分为「逻辑备份」和「物理备份」。

  • 逻辑备份:通过 exp 方式导出,只能在数据库 open 的模式下导入。
  • 物理备份:在数据库 open 或 close 的情况下都能做。

导出

具体讲,「导出」分为:导出表,导出方案,导出数据库。

导出示例

1. 导出某几张表的数据

C:\»exp userid=scott/hello@iridium tables=(emp, emp4, dept) file=D:\exp1.dmp

需要留意,exp 是外部程序,可能存在于如下目录中:

  • Oracle 9i: C:\oracle\ora92\bin\EXP.EXE
  • Oracle 11g: D:\programs\oracle11g\product\11.2.0\dbhome_1\BIN

由于 exp 是外部程序,所以上面提到的 exp 命令无法在 sqlplusw 的窗口中运行。

2. 导出其他用户方案的表

需要 dba 或者 exp_full_database 权限,语句和上面类似,但表名前应该添加方案名

3. 导出表的结构

C:\»exp userid=scott/hello@iridium tables=(emp, emp4, dept) file=D:\exp1.dmp rows=n

直接导出。在上面的语句中添加参数:direct=y 比较适合大表,数据量非常多的情况。

4. 导出方案

导出自己的方案:

C:\»exp userid=scott/hello@iridium owner=scott file=D:\exp6.dmp direct=y

另一个示例:exp userid=wificity_test/wificity_test@ORCL1111219 owner=wificity_test file=D:\wificity_test.dmp direct=y statistics=none

导出别人的方案,类似上面的导出表,需要的权限一样。

C:\»exp userid=system/hello1234@iridium owner=(system, scott) file=D:\exp8.dmp direct=y

可能的问题

导出操作并非每次都能执行成功。如果客户端的版本和服务端的版本不一致,如服务端是64位的,本地的是32位的,等等,有可能导致操作失败(ora-00904)。可以考虑安装多个虚拟机,在各虚拟机中安装不同版本的客户端备用。

导入

IMP 是导入的命令,也是外部程序。能够通过 IMP 导入的文件必须是前面通过 EXP 命令导出的文件。

导入其他用户的表需要权限:imp_full_database.

导入命令和上面的导出很相似,不同的是把 EXP 换成 IMP 而已

一个完整的导出并导入示例

导出:C:\»exp userid=scott/hello@iridium tables=(emp, emp4, dept) file=D:\exp3tables.dmp direct=y

删除:drop table emp4;

导入:imp userid=scott/hello@iridium tables=(emp4) file=D:\exp3tables.dmp

导入其他用户的表,如果要用 system 导入 scott 导出的表,那么这表的约束关系也得满足才行。

导入方案示例

导入自身的方案

imp userid=scott/hello@iridium file=D:\da.dmp

另一个示例:imp userid=wificity_test/wificity_test@orcl file=D:\others\wificity_test.dmp full=y

导入其他方案要求用户具有 dba 权限

imp userid=system/hello1234 file=XXX fromuser=XXX touser=XXX(未做实验)

导入数据库

imp userid=system/hello1234 full=y file=XXX