oracle数据库的导入导出

常用命令示例:

导出: 
 

       expdp acl/kingdom898@kingt1 directory=dir schemas=acl  dumpfile=ACL.DMP logfile=acl.log

导入:
       impdp acl/kingdom898@kingt1 directory=dir schemas=acl  dumpfile=ACL.DMP logfile=acl.log 

注意:导出语句后面不要有分号


Oracle数据导入导出之imp/exp与Expdp/impdp

         需要进行oracle数据库数据导入导出的时候,我们只需要将数据泵的命令写一个批处理脚本就可以方便的进行数据的操作。比如在windows下写一个.bat文件,或在linux下写一个.sh文件,在其中添加以下想要的命令执行即可。但expdp/impdp只能在数据库所在的服务器上执行导入导出命令。

        exp/imp已经很好用了,但是唯一的缺点是速度太慢,从Oracle 10g开始提供了称为数据泵新的工具expdp/impdp,它为Oracle数据提供高速并行及大数据的迁移。

        imp/exp可以在客户端调用,但是expdp/impdp只能在服务端,因为在使用expdp/impdp以前需要在数据库中创建一个Directory。 

 

Expdp/impdp

一、directory目录:

1、查看管理员目录(同时查看操作系统中是否存在,因为Oracle并不关心该目录是否存在,如果不存在,则出错)
select * from dba_directories;

2、建立数据库指定目录:(如果不想使用旧的目录或不存在指定目录)      注:一般数据库目录只允许sys创

create directory dir as 'd:dump';        //dir名称可以随便命名   需要手工创建,即:create directory 别名 as 'd:服务器目录名',将导入或导出的文件放在服务器目录下 。

注意:导入时一样要建立目录,并将要导入的文件放置在建立的目录下,而且要将导出时生成log也放置在下面否则会报错。

3、给用户赋予在指定目录的操作权限,最好以system等管理员赋予。

select privilege,table_name from dba_tab_privs where grantee='scott';  --查询权限

grant connect,resource to scott;

grant read,write on directory dir to scott;   ----最少要赋予这个权限

grant exp_full_database,imp_full_database to scott;

4、删除自定目录

delete directory dir;

二、导出:
1、导出内容相关的参数:
expdp
        导出命令
ktrade/ktrade@orcl
        用户及数据库实例验证 
directory=dump
        指定转储文件和日志文件所在的目录,目录对象是使用CREATE DIRECTORY语句建立的对象,而不是OS目录  ?os目录          注意:此参数需要放在靠前的位置,在后面有可能会报错ORA-39002: 操作无效
schemas:
        schemas=ktrade
        注:导出实例下的所有对象
dumpfile:
       用于指定转储文件的名称,默认名称为expdat.dmp
        DUMPFILE=[directory_object:]file_name [,….]
        Directory_object用于指定目录对象名,file_name用于指定转储文件名,如果不指定directory_object,导出会自动使用DIRECTORY选项指定的目录对象

logfile:   
        指定导出日志文件文件的名称,默认名称为export.log
        LOGFILE=[directory_object:]file_name
        Directory_object用于指定目录对象名称,file_name用于指定导出日志文件名.如果不指定directory_object.导出会自动使用DIRECTORY的相应选项值.
content:     
        content=metadata_only:导出表结构
        content=data_only:导出表数据
        content=all:导出表结构+表数据
        注:如果不指定content,默认情况下是all
tables:
        tables=tablename1,tablename2
        注:如果不写,默认情况下导出模式下所有
full:
        full=y
        注:导出全库,要求使用较高权限的sys或system用户
parallel:
        parallel=:并行度设置,根据cpu的数量设置
tablespace:
        tablespace=:导出表空间内的所有数据
transport_tablespaces:
        transport_tablespaces=:导出表空间的定义,用于表空间传输
parfile:
        parfile=:可以将参数文件在文件中加载
query:
        query="where id=20":用于按条件查询,在按表导出时用到
 

filesize:

 

        指定导出文件的最大尺寸,默认为0,(表示文件尺寸没有限制)

job_name:
        指定要导出作用的名称,默认为SYS_EXPORT_SCHEMA_01
        JOB_NAME=jobname_string
        SELECT * FROM DBA_DATAPUMP_JOBS;--查看存在的job
2、示例:

SID=orcl,导出dmp的账号为test,导入dmp的账号为test为例。 
若将数据从sfz中导出: 
expdp test/test@orcl directory=别名 dumpfile=导出文件名 
1)按用户导

expdp scott/tiger@orcl directory=dir dumpfile=expdp.dmp schemas=scott   logfile=expdp.log

2)并行进程parallel

expdp scott/tiger@orcl directory=dir dumpfile=scott3.dmp parallel=40 job_name=scott3  logfile=expdp.log

3)按表名导

expdp scott/tiger@orcl directory=dir dumpfile=expdp.dmp tables=emp,dept   logfile=expdp.log

4)按查询条件导

expdp scott/tiger@orcl directory=dir dumpfile=expdp.dmp tables=emp query='WHERE deptno=20'  

5)按表空间导

expdp system/manager directory=dir dumpfile=expdp.dmp tablespaces=temp,example

6)导整个数据库

expdp system/manager directory=dir dumpfile=full.dmp full=y

 
三、导入:
1、导入内容相关的参数:
impdp
        导入命令

remap_schema:

        该选项用于将源方案的所有对象装载到目标方案中(当两个方案名称不同时).
        remap_schema=source_schema:target_schema

remap_tablespace:

 

        将源表空间的所有对象导入到目标表空间中(当两个表空间名称不同时)
        remap_tablespace=source_tablespace:target:tablespace

remap_datafile:
        该选项用于将源数据文件名转变为目标数据文件名,在不同平台之间搬移表空间时可能需要该选项.
        remap_datafile=source_datafie:target_datafile
2、示例:

注意:如果导入的数据库是新建数据库那么需要先创建表空间以及用户,如下面这样:

drop tablespace koauth24 including contents and datafiles;
create tablespace koauth24 datafile 'F:appAdministratorproduct4.0datakoauth24' size 100M autoextend on next 100M;
drop user koauth24 cascade;
create user koauth24 identified by kingddom88 default tablespace koauth24;
grant dba,resource,connect to koauth24;
grant select any table to koauth24;
grant update any table to koauth24;
grant insert any table to koauth24;

1)导到指定用户下

impdp scott/tiger@orcl directory=dir dumpfile=expdp.dmp SCHEMAS=scott logfile=impdp.log

2)改变表的owner

impdp system/manager directory=dir dumpfile=expdp.dmp TABLES=scott.dept REMAP_SCHEMA=scott:system;

3)导入表空间

impdp system/manager directory=dir dumpfile=tablespace.dmp TABLESPACES=example;

4)导入数据库

impdb system/manager directory=dump_dir dumpfile=full.dmp FULL=y;

5)追加数据

impdp system/manager directory=dir DUMPFILE=expdp.dmp SCHEMAS=system TABLE_EXISTS_ACTION

6、导入时用户名、表空间名称等不一样时,可做个映射
remap_schema=test(导出的名称):test1(将要导入的名称)

remap_tablespace=oauth2:koauth2

 

最后可以检查一下数据库中是不是所有表都有不足的可以再补齐

imp/exp

一、数据导出:

 1 将数据库TEST完全导出,用户名system 密码manager 导出到D:daochu.dmp中

   exp system/manager@TEST file=d:daochu.dmp full=y

 2 将数据库中system用户与sys用户的表导出

   exp system/manager@TEST file=d:daochu.dmp owner=(system,sys)

 3 将数据库中的表table1 、table2导出

   exp system/manager@TEST file=d:daochu.dmp tables=(table1,table2)

 4 将数据库中的表table1中的字段filed1以"00"打头的数据导出

   exp system/manager@TEST file=d:daochu.dmp tables=(table1) query=" where filed1 like  '00%'"

 

上面是常用的导出,对于压缩我不太在意,用winzip把dmp文件可以很好的压缩。不过在上面命令后面 加上 compress=y  就可以了

二、数据的导入:

 1 将D:daochu.dmp 中的数据导入 TEST数据库中。

   imp system/manager@TEST  file=d:daochu.dmp

 上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。

 在后面加上 ignore=y 就可以了。

 

 2 将d:daochu.dmp中的表table1 导入

 imp system/manager@TEST  file=d:daochu.dmp  tables=(table1)

 注意:基本上上面的导入导出够用了。不少情况我是将表彻底删除,然后导入。

 

同名用户之间的数据导入:

imp hkb/hkb@xe file=c:orabackuphkbfull.dmp log=c:orabackuphkbimp.log full=y

 

不同名之间的数据导入:

imp system/test@xe fromuser=hkb touser=hkb_new file=c:orabackuphkbfull.dmp

log=c:orabackuphkbimp.log;

原文地址:https://www.cnblogs.com/quyong/p/6688155.html