一、导入导出数据库对象的四种模式:
- 数据库模式:导出和导入整个数据库中的所有对象
- 表空间模式:导出和导入一个或多个指定的表空间中的所有对象
- 用户模式:导出和导入一个用户模式的所有对象
- 表模式:导出和导入一个或多个指定的表或表分区
注意:这四种模式是互斥的。
二、命令(exp--导出,imp--导入)
- 表模式导出:导出student表和arddress表到C盘下,名称是scott1.dmp 日志是C盘下scott1.log
--表示自己登陆的用户名和密码
exp scott/scott@orcl tables=(student,address) file=c:\scott1.dmp log=c:\scott1.log
- 用户模式:导出scott(用户)的所有对
exp scott/scott@orcl owner=scott file=c:\scott2.dmp log=c:\scott2.log
- 表空间模式:导出users表空间
exp system/system@orcl tablespaces=(users) file=c:\users.dmp log=c:\users.log
- 数据库模式
--将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
exp system/manager@TEST file=d:\daochu.dmp full=y
注意:导出的时候,可能会出现权限问题。比如普通用户A只能导出自己的表,用户等模式,不能去操作用户B的表。
--scott导出user1的t表
exp scott/scott@orcl tables=(user1.t) file =c:\scott5.dmp log=c:\scott5.log --表示没有导出t表权限
--管理员可以导出,有权限
exp system/system@orcl tables=(user1.t) file =c:\scott5.dmp log=c:\scott5.log
- 使用参数文件导出数据
--可以用文件
/*
比如:在c盘下建立一个par.txt文件
userid=scott/scott@orcl
tables=student
file=c:\scott2.dmp
cd\ --进入C盘
exp parfile=par.txt
*/
exp system/system@orcl parfile=c:par.txt
三、导入
--导入自己的表
--先导出来:
exp scott/scott@orcl tables=(student,address) file=c:\scott1.dmp log=c:\scott1.log
--如果要验证,可以把student,address表删除 drop,然后执行导入
imp scott/scott@orcl file=c:\scott.dmp
imp scott/scott@orcl file=c:\scott.dmp tables=student;--指定导入某张表
--导入别人的表(必须是管理员才能操作): fromuser 从(某)用户 touser 到(某)用户
exp scott/scott@orcl tables=(student,address) file=c:\scott1.dmp log=c:\scott1.log
--用scott用户,给user1用户导入之前的表备份
imp scott/scott@orcl file=c:\scott1.dmp fromuser=scott touser=user1;--这个不行的
--用user1用户,给user1用户导入之前的表备份
imp user1/user@orcl file=c:\scott1.dmp fromuser=scott touser=user1;--这个可以
--用system用户,给user1用户导入之前的表备份
imp system/system@orcl file=c:\scott1.dmp fromuser=scott touser=user1;--这个可以
四、实战:将数据库很大的表导出到另外一个数据库上。使用可传输表空间,orcl数据库的tb1传输到wang数据库去。(可以先建设wang数据库)
1.创建表空间 system管理员创建:
create tablespace tb1 datafile 'D:\app\administrator\oradata\orcl\tb101.dbf'
size 10m;
scott:
create table t tablespace tb1 as select * from all_tables;
--意思是orcl数据库有tb1这个表空间,这个表空间里面有t这张表
检查要传输的表空间是否是自包含的。
假如t表里面的索引在tb2的表空间里面,那么你传输tb1这个表空间,你表的索引肯定是传输不过去的。
所以要检查自包含。
dbms_tts. 检查表空间是否自包含
2.将表空间设置只读
alter tablespace tb1 read only;
3.exp进行可传输表空间模式的导出
exp 'system/system@orcl as sysdba' tablespaces=tb1 transport_tablespace=y file=c:\exp_tb1.dmp
4.将导出文件和数据文件复制到目标数据库上
--假如数据库在不同计算机上
exp_tb1.dmp(刚刚导出的文件C盘)文件复制在d盘下
tb101.dbf复制到 d:\app\administrator\oradata\wang文件夹下
5.目标数据库上,imp进行可传输表空间模式的导入
imp 'system/system@wang as sysdba' tablespaces=tb1 transport_tablespace=y file=d:\exp_tb1.dmp
datafiles='d:\app\administrator\oradata\wang\tb101.dbf'
6.目标数据库上,把表空间设置成读写状态。连接到wang数据库
alter tablespace tb1 read write;
select * from v$tablespaces;--查看表空间