在另一篇博客有说到利用exp和imp导出和导入一个普通用户(不包含分区表的用户),那现在这篇就来说下导出和导入一个用户的某些表。
实验环境
1、Oracle11g企业版
2、登录scott用户
实验目的
将scott用户下的某个表导出,然后导入到hr用户下
实验过程
exp导出数据表
为了不影响scott自带的表,我们使用以下语句创建两张测试表。
create table ex1 as select * from emp;
create table ex2 as select * from dept;
可以用以下语句验证ex1和ex2的存在
select * from ex1;
select * from ex2;
在导出scott用户的表ex1和表ex2之前,我们来捋一下下面的几个问题:
- 要导出的表在哪个用户下?【scott用户】
- 导出的dmp文件放在哪里?【file=C:\Users\Administrator\Desktop\scott.dmp】
- 日志文件存放在哪里?【log=C:\Users\Administrator\Desktop\scott.log】
- 要导出哪些表?【tables=(ex1,ex2)】
现在我们来整理下上述问题:
从scott用户导出表ex1和ex2,数据存放在scott.dmp文件中,日志放在scott.log中。
下面我们来导出数据:
1、打开cmd
2、运行以下语句:
exp scott/tiger@Q file=C:\Users\Administrator\Desktop\scott.dmp log=C:\Users\Administrator\Desktop\scott.log tables=(ex1,ex2)
exp username/password@db_name file=存放路径 log=日志存放路径 tables=(table1,table2)
注意细节:该语句并没有分号结尾,若有分号,会报错。
可以看到,在指定路径已经生成了一个dmp文件。这也就是所谓的逻辑备份。到目前为止数据表的导出操作已经完成。
imp导入数据表
同样的,在导入数据表之前,来回答几个问题:
- 导入到哪个用户【hr用户】
- dmp文件存放在哪个地方【C:\Users\Administrator\Desktop\scott.dmp】
即将dmp文件的数据导入到hr用户
下面我们来导入数据:
1、打开cmd
2、运行以下语句:
imp hr/hr@Q file=C:\Users\Administrator\Desktop\scott.dmp full=y
imp username/password@db_name file=存放路径 full=y
同样的,该语句也是没有分号结尾的,有的话会报错。
通过查询这两个表,可以看到这两张表在hr用户下了。