
1导出工具exp
$ORACLE_HOME/bin 操作系统下可执行文件
导出工具将数据库中的数据压缩在一个二进制文件 (在oracle之间使用)
三种模式
a.表模式:导出用户所有的表或者指定的表
b.用户模式:导出用户下的所有内容(表、索引、过程…)
c.整个数据库:导出数据库中的所有对象
查看帮助命令
exp help =y
例:
1.导出前先查询数据库的字符集(方法很多)
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
----------------------------------
AMERICAN_AMERICA.ZHS16GBK
2.设置Linux操作系统的NLS_LANG环境变量
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
3.导出
使用exp把scott.emp表导出来,导出文件名scott.emp
$ exp scott/oracle tables=emp file=scott.emp
假设有人误删除了scott.emp表
SQL> drop table scott.emp purge ;
现在后悔 选择用逻辑导入恢复数据
使用导出的文件恢复原来的emp表
$ imp scott/oracle tables=emp file=scott.emp
2导出模式
表模式
a. 单表(全表)导出
导出 scott.emp表
exp scott/oracle tables=emp file=emp.dmp
或
exp system/oracle tables=scott.emp file=emp.dmp
b. 单表的部分数据导出
QUERY一个表的一部分分数据导出
只导出工资高于1500
exp scott/oracle TABLES=emp QUERY=\"WHERE sal \>1500\" file=e1500.dmp
测试:
delete from scott.emp where sal >1500;
commit;
把数据还原
把e1500.dmp 导入 (只导入数据)
imp scott/oracle tables=emp file=e1500.dmp DATA_ONLY=y
只导出10号部门员工
exp scott/oracle TABLES=emp QUERY=\"WHERE deptno \=10 \" file=e10.dmp
只导出销售员的信息
exp scott/oracle TABLES=emp QUERY=\"WHERE job \=\'SALESMAN\' \" file=e10.dmp
c. 多张表导出
要导出emp、dept
exp scott/oracle tables=emp,dept file=ed.dmp
用户模式
注意:对象前面加拥有者的名字 比如:scott.emp
exp tables=scott.emp file=emp1.dmp (不指定登录用户,交互界面)
username:sys as sysdba
password: oracle
exp \'sys/oracle as sysdba\' tables=scott.emp file=emp2.dmp
建议不用sys,一般用管理员导出的,使用 system ,不用带as sysdba
exp system/oracle tables=scott.emp file=emp2.dmp
案例:还原删除的用户
先备份用户scott
exp scott/oracle owner=scott file=soctt.dmp
删除用户
drop user scott cascade;
管理员恢复用户
create user scott identified by oracle default tablespace users temporary tablespace temp;
grant connect ,resource to scott;
grant unlimited tablespace to scott;
imp system/oracle file=soctt.dmp FROMUSER=scott TOUSER=scott
案例:把用户对象从一个用户迁移到另外一个对象
exp scott/oracle owner=scott file=soctt.dmp
把scott的对象全部倒入hr用户
imp system/oracle file=soctt.dmp FROMUSER=scott TOUSER=hr;
把scott.dept表导入到hr用户下
exp scott/oracle tables=dept file=dept.dmp
imp system/oracle tables=dept file=dept.dmp fromuser=scott touser=hr;
3. 全库模式
exp system/oracle full=y file=full.dmp
指定导出片的大小,限制为1g
exp scott/oracle tables=emp file=emp2.dmp filesize=1g
参数filesize 指定了二进制备份文件的最大字节数,目的是解决某些操作系统下2g物理文件限制以及可以加快压缩和方便刻录光盘等。
远程导出到本地
exp system/oracle@orcl tables=scott.emp file=emp168.dmp
3导入工具imp
$ORACLE_HOME/bin下一个可执行文件
imp将exp生成的二进制文件导入数据库中
只有exp_full_database 和dba权限的用户才能做整个数据库的导入
grant exp_full_database to scott;
例:
先删除 scott.emp 表的记录,使用备份文件恢复记录
SQL> select count(*) from emp;
COUNT(*)
----------
14
exp scott/oracle file=emp.dmp tables=emp
sqlplus scott/oracle
truncate table scott.emp;
SQL> select count(*) from scott.emp;
COUNT(*)
----------
0
还原数据
imp scott/oracle tables=emp file=emp.dmp
IMP-00015: following statement failed because the object already exists:
imp scott/oracle tables=emp file=emp.dmp ignore=y
参数ignore=y 表示直接导入数据 忽略表的创建
检查结果select cont(*) from emp;
或只导入数据
imp scott/oracle tables=emp file=emp.dmp DATA_ONLY=Y
将某用户的表导入到另一个用户下
把scott.emp导入到hr模式下
先把scott.emp备份
exp scott/oracle file=emp.dmp tables=emp
再导入hr
imp system/oracle tables= emp file=emp.dmp fromuser=scott touser=hr
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
. importing SCOTT's objects into HR
. . importing table "EMP" 14 rows imported
IMP-00017: following statement failed with ORACLE error 942:
"ALTER TABLE "EMP" ADD CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO") REFEREN"
"CES "DEPT" ("DEPTNO") ENABLE NOVALIDATE"
IMP-00003: ORACLE error 942 encountered
ORA-00942: table or view does not exist
About to enable constraints...
IMP-00017: following statement failed with ORACLE error 2430:
"ALTER TABLE "EMP" ENABLE CONSTRAINT "FK_DEPTNO""
Import terminated successfully with warnings.
原因:建立外键的时候去依赖dept表的字段,但是这里没有
解决办法:先导入主表dept 再导入从表emp
或者可以不需要创建约束,加入参数CONSTRAINTS=N
imp system/oracle tables= emp file=emp.dmp fromuser=scott touser=hr CONSTRAINTS=N
导入可能出现的问题
a. 数据对象已经存在
解决方法:使用参数ignore=y 会把exp文件的数据内容直接导入
如果表有唯一关键字约束,不符合条件的不导入
如果表没有唯一关键字约束,将引起重复记录
b. 数据库对象有主外键约束
不符合主外键约束,数据导入失败
解决方法:先导入主表,在导入从表
或者先禁用主外键,导入完成后在启用
c. 权限不够
给予imp_full_database
exp_full_database
d. 字符集问题
如果字符集不同,导入失败
查看oracle server端的字符集
select userenv('language') from dual;
查看oracle client端字符集
echo $NLS_LANG
如果检查结果发现server端和client字符集不一样,修改为同server端相同的字符集
导出的过程转换
在export过程中,源数据库字符集与export用户会话字符集不一致,会发生字符集转换,并在导出文件头部几个字节中存储export用户会话字符集ID号,在这个过程中可能发生数据丢失。
要正确导出源数据库数据,要求export过程的用户会话字符集应等于源数据库字符集或是源数据库字符集的超集。
如:
把NLS_LANG等于数据库的字符集
导出前查看数据库的字符集
SQL> select userenv('language') from dual;
USERENV('LANGUAGE')
------------------------------
AMERICAN_AMERICA.ZHS16GBK
设置Linux操作系统的NLS_LANG环境变量
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
要求export过程的用户会话字符集(NLS_LANG)=源数据库字符集或是源数据库字符集的超集
避免转换数据丢失
源库: exp
目标库:imp
1. 源和目标数据库字符集都相同
2. 执行exp的会话设置NLS_LANG=源库字符集
3. 执行imp的会话设置NLS_LANG=目标库的字符集
e. imp,exp版本不能向上兼容,向下兼容
imp可以导入低版本exp生成的文件,不能导入高版本exp生成的文件
f. 导入大表(大于80M),存储失败
默认的exp时,compress=y,也就是数据将压缩在一个数据块上,导入时,如果不存在连续一个大数据块,导入失败;建议导出80m以上大表时,将compress=n
4exp、imp参数
buffer=4096000 (数据缓冲区,以字节为单位,自行调整)
对性能有很大影响
compress=y(将在导出时候合并碎片)
direct=y(以direct 方式导出,告诉exp直接读取数据,速度快)
feedback(每x行显示进度)
例:
将用户的所有内容删除后再恢复
1. 导出用户所有的对象
exp scott/oracle owner=scott file=soctt.dmp
2. 删除用户
drop user scott cascade ;
导入用户(必须先存在)
create user scott identified by oracle default tablespace users temporary tablespace temp quota unlimited on users;
grant connect,resource to scott;
imp system/oracle file=scott.dmp fromuser=scott touser=scott ignore=y buffer=200000
使用脚本完成导出某个用户的所有表

vi home/oracle/a.sql
set feedback off heading off verify off trimspool off
set pagesize 0 linesize 200
spool u01/table_scott.sh
select 'exp scott/oracle tables=' || table_name || ' file=/u01/' || table_name ||'.dmp' from user_tables;
spool off
sqlplus scott/oracle
@/home/oracle/a.sql
ll u01
table_scott.sh
给予执行权限
chmod u+x /u01/table_scott.sh
./u01/table_scott.sh





