000-0很久很久以前,Oracle就开始提供用来摄取表、模式或整个数据库的定义,

然后导入到其他模式或数据的小工具:那就是exp/imp

那个时候数据库规模都很小(几百M就算超大数据库了),而且对于数据库的要求也没有那么高,不像现如今,

动不动就是7*24小时高并发、高可用,以至在某些领域,exp/imp也被视作备份恢复的工具使用并延续至今。

如果你使用exp备份几十G、数百G甚至更大规模数据库,并且将这种方式作为生产数据库的备份策略,这就太不合理了。






1、导出某用户下所有表

exp scott/lipengfei file=scott_all_tables.dmp log=scott_all_tables.log



2、导出scott用户下的部分表

  (1)exp scott/lipengfei tables=\(emp,salgrade\) file=scott_emp_salgrade.dmp log=scott_emp_salgrade.log 

(2)exp scott/lipengfei tables=emp file=scott_emp.dmp log=scott_emp.log 


3、参数文件

vi /home/oracle/dept.txt 文件内容如下:

userid=scott/lipengfei

log=/home/oracle/scott_dept.log

file=/home/oracle/scott_dept.dmp

tables=dept


exp parfile=/home/oracle/dept.txt


4、按条件导出

(1)

exp parfile=/home/oracle/emp.txt


vi /home/oracle/emp.txt  内容如下:

userid=scott/lipengfei

log=/home/oracle/emp.log

file=/home/oracle/emp.dmp

tables=emp

query='where sal>1000'


(2)条件中是数字

exp scott/lipengfei tables=emp query="'where sal >1000'" file=/home/oracle/emp.dmp log=/home/oracle/emp.log


(3)条件中带有字符串

exp scott/lipengfei tables=emp query="'where sal >1000 and job=''CLERK'''" file=/home/oracle/emp.dmp log=/home/oracle/emp.log


(4)参数文件,处理条件中带有字符串

exp parfile=/home/oracle/emp.txt


vi /home/oracle/emp.txt  内容如下:

userid=scott/lipengfei

log=/home/oracle/emp.log

file=/home/oracle/emp.dmp

tables=emp

query='where sal>1000 and job=''CLERK'''



5、导出某几个用户的所有表


create tablespace li datafile '/oracle/app/oradata/ecom/li.dbf' size 30M AUTOEXTEND OFF;

create user li identified by li default tablespace li;

alter user li account unlock;

grant connect,resource to li;


create tablespace peng datafile '/oracle/app/oradata/ecom/peng.dbf' size 30M AUTOEXTEND OFF;

create user peng identified by peng default tablespace peng;

alter user peng account unlock;

grant connect,resource to peng;


create tablespace fei datafile '/oracle/app/oradata/ecom/fei.dbf' size 30M AUTOEXTEND OFF;

create user fei identified by fei default tablespace fei;

alter user fei account unlock;

grant connect,resource to fei;



sqlplus li/li

create table haha(id int);

insert into haha values(1);

commit;


sqlplus peng/peng

create table hehe(id int);

insert into hehe values(1);

commit;


sqlplus fei/fei

create table hihi(id int);

insert into hihi values(1);

commit;


exp \'sys/lipengfei as sysdba\' file=/home/oracle/li_peng_fei.dmp log=/home/oracle/li_peng_fei.log owner=\(li,peng,fei\)



6、不想导出索引、不想导出约束、不想导出授权、不想导出与表相关的触发器等


exp scott/lipengfei file=scott_all_tables.dmp log=scott_all_tables.log indexes=N constraints=N grants=N triggers=N



7、导出的文件太大了,超出文件系统限制【fat32单个文件不能超过4G,ntfs单个文件不能超过2T,ext3理想情况下单个文件不能超过2T】


exp scott/lipengfei filesize=500M  file=scott_all_tables1.dmp scott_all_tables2.dmp log=scott_all_tables.log


如果指定filesize参数,那么file参数也要跟着修改。exp在导出的时候有可能会生成多个dmp文件,

因此必须在file参数中为每一个文件分别命名(多个名称间以逗号分隔)

如果file参数指定的文件名多于实际生成的文件,多出指定的文件不会被生成。

如果file参数指定的文件名少于实际生成的文件,exp执行过程中,

在用完用户所指定的文件后,就会提示输入新的文件名。

如果没有人在旁边操作,那么整个导出任务就会停在这里了。



接着你可能就要问,怎么知道要导出的数据一共占用多大空间?

select sum(bytes)/1024/1024 "total(M)" from user_segments;



SQL> create table nimei as select * from all_objects;  

SQL> create index i_nimei on nimei(object_id);

select SEGMENT_NAME,SEGMENT_TYPE,TABLESPACE_NAME from user_segments;  【验证索引默认和表在同一表空间中】


--------------------------------------------------------------

1、导入数据

exp li/li file=li_all_tables.dmp log=li_all_tables.log


sqlplus li/li

SQL> drop table haha;


imp li/li file=li_all_tables.dmp log=li_all_tables.log


2、导入指定用户下所有对象到其他用户


imp peng/peng fromuser=li touser=peng file=li_all_tables.dmp log=li_to_peng_all_tables.log


sqlplus peng/peng


SQL> select  tname from tab;


上面的操作看起来成功?其它并没有,数据虽然成功导入了,但不是严谨的方式,可能无意中给数据库埋了一颗雷。

SQL> show user

SQL> select username,default_tablespace from user_users;

SQL> select table_name,tablespace_name from user_tables;


奇怪吗?虽然peng用户默认的表空间是peng,但是新导入的haha表被存储到li表空间中。如下方式解决


sqlplus / as sysdba


alter user peng quota unlimited on peng; 【让peng用户可以无限使用 peng表空间】

revoke unlimited tablespace from peng; 【回收peng用户可以无限使用其它所有表空间的权限】


sqlplus peng/peng

drop table haha;


imp peng/peng fromuser=li touser=peng file=li_all_tables.dmp log=li_to_peng_all_tables.log 【报同名对象已经存在】

imp peng/peng fromuser=li touser=peng file=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y


sqlplus peng/peng


SQL> select table_name,tablespace_name from user_tables;



如果要导入的对象已经存在,默认情况导入就会报错。

ignore=N 【默认】,出错对象会被跳过,imp继续后续操作。

ignore=Y,自动忽略对象已存在的事实,继续导入数据,也就会出现重复数据,可能通过手工去重。


3、导入表结构到指定用户


sqlplus peng/peng

SQL> drop table haha;


imp peng/peng fromuser=li touser=peng file=li_all_tables.dmp log=li_to_peng_all_tables.log ignore=y rows=N


sqlplus peng/peng

SQL> select tname from tab;

SQL> select * from haha;