Oracle数据迁移简述

exp导出

命令行

1.多表导出

mkdir /u02
[oracle@OCPLHR u02]$  exp scott/oracle file=/u02/scott1.dmp tables=emp,dept;

在这里插入图片描述

2.按用户导出

[oracle@OCPLHR ~]$ exp scott/oracle file=/u02/scottu.dmp owner=scott

在这里插入图片描述
在这里插入图片描述

3.按多用户导出

[oracle@OCPLHR u02]$ exp system/oracle file=/u02/scottU.dmp owner=scott,lyh log=/tmp/expscott.log

在这里插入图片描述

在这里插入图片描述
其中,log=/tmp/expscott.log为导出过程的记录日志
在这里插入图片描述
4.sysdba

[oracle@OCPLHR u02]$ exp \'sys/oracle as sysdba\' file=/u02/uscott.dmp owner=scott

在这里插入图片描述
在这里插入图片描述
5.文件大,导出数据文件多个

[oracle@OCPLHR u02]$ exp scott/oracle file=/u02/s01.dmp,/u02/s02.dmp filesize=1g tables=emp,dept log=/tmp/emp01.log

在这里插入图片描述

6.全库

[oracle@OCPLHR u02]$ exp \'sys/oracle as sysdba\' file=/u02/full1.dmp,/u02/full2.dmp filesize=100m full=y

全库导出,必须用sysdba等dba权限的用户
在这里插入图片描述

参数文件

按表导出
在这里插入图片描述

脚本:
userid=scott/oracle
file=/u02/ss01.tmp
tables=emp,dept

[oracle@OCPLHR u02]$ exp parfile=p1.txt

在这里插入图片描述
按表导出,导出其他用户的表
在这里插入图片描述

userid=system/oracle
file=/u02/ss03.dmp
tables=scott.emp,scott.dept

[oracle@OCPLHR u02]$ exp parfile=p2.txt

在这里插入图片描述
按用户导出其他的表
在这里插入图片描述

userid=system/oracle
file=/u02/scottexp.dmp
owner=scott

[oracle@OCPLHR u02]$ exp parfile=p3.txt

在这里插入图片描述

在这里插入图片描述

按条件导出(比如时间)

在这里插入图片描述

userid=scott/oracle
file=emp10-20.dmp
tables=emp
query='where deptno in (10,20)'


[oracle@OCPLHR u02]$ exp parfile=p4.txt

在这里插入图片描述

Imp导入

在这里插入图片描述

命令行

[oracle@OCPLHR u02]$ imp scott/oracle file=/u02/scott.dmp tables=emp,dept

在这里插入图片描述
在这里插入图片描述

参数文件

在这里插入图片描述

userid=scott/oracle
file=/u02/scott.dmp
tables=emp,dept

imp parfile=im.txt

在这里插入图片描述
在这里插入图片描述
表存在,但部分内容清除
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[oracle@OCPLHR u02]$ imp scott/oracle file=/u02/scott.dmp tables=emp ignore=y

在这里插入图片描述

导入表时更换表的用户

[oracle@OCPLHR u02]$ imp system/oracle fromuser=scott touser=lyh file=/u02/scott.dmp tables=emp,dept

在这里插入图片描述

在这里插入图片描述

Expdp/impdp

Expdp

新建逻辑目录
最好以system等管理员创建逻辑目录,Oracle不会自动创建实际的物理目录“/u02”(务必手动创建此目录),仅仅是进行定义逻辑路径exp;

[oracle@OCPLHR u02]$ sqlplus / as sysdba

SYS@OCPLHR1> create directory exp as '/u02/';

Directory created.

SYS@OCPLHR1> grant read,write on directory exp to scott;

Grant succeeded.

查看管理员目录
同时查看操作系统是否存在该目录,因为oracle并不关心该目录是否存在,假如不存在,则出错

sql>select * from dba_directories;

导出制定表

[oracle@OCPLHR u02]$ expdp scott/oracle@OCPLHR1 tables=emp,dept dumpfile=expdp.dmp directory=exp;

在这里插入图片描述

导出用户及其对象

[oracle@OCPLHR u02]$ expdp scott/oracle@OCPLHR1 schemas=scott dumpfile=expdp2.dmp directory=exp;

在这里插入图片描述

按查询条件导出

[oracle@OCPLHR u02]$  expdp scott/oracle@OCPLHR1 directory=exp dumpfile=expdp3.dmp tables=emp query=\'where DEPTNO=20\';

在这里插入图片描述

按表空间导出

[oracle@OCPLHR u02]$ expdp system/oracle@OCPLHR1 directory=exp dumpfile=tablespace.dmp tablespaces=temp,example;

在这里插入图片描述
在这里插入图片描述

全库导出

[oracle@OCPLHR u02]$ expdp system/oracle@OCPLHR1 directory=exp dumpfile=full.dmp full=y;

在这里插入图片描述

Impdp

在正式导入数据前,要先确保要导入的用户已存在,如果没有存在,请先用下述命令进行新建用户

--创建表空间
SYS@OCPLHR1> create tablespace tb_name datafile '/u02/tb_name.dbf' size 1024m AUTOEXTEND ON;

--创建用户
      create user user_name identified by A123456a default tablespace tb_name temporary tablespace TEMP;
--给用户授权
sql>grant read,write on directory dump_dir to user_name;
sql>grant dba,resource,unlimited tablespace to user_name;

导入用户

(从用户scott导入到用户scott)
[oracle@OCPLHR u02]$ impdp scott/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp tables=emp,dept;

在这里插入图片描述
在这里插入图片描述

导入表

(从scott用户中把表dept和emp导入到system用户中)
[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp tables=scott.dept,scott.emp remap_schema=scott:lyh;

在这里插入图片描述
在这里插入图片描述

导入表空间

impdp system/oracle@OCPLHR1 directory=exp dumpfile=tablespace.dmp tablespaces=example;

导入数据库

补充知识点:删除数据库
在这里插入图片描述
以上与本节内容无关

[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=full.dmp full=y;

追加数据
在这里插入图片描述
在这里插入图片描述

[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp schemas=emp table_exists_action=append
[oracle@OCPLHR ~]$ impdp scott/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp tables=emp table_exists_action=append

在这里插入图片描述
在这里插入图片描述
表存在,替换

在这里插入图片描述
在这里插入图片描述

[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp schemas=scott table_exists_action=replace
[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp table_exists_action=replace

在这里插入图片描述
在这里插入图片描述
表存在,删除旧表
在这里插入图片描述
在这里插入图片描述

impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp schemas=system table_exists_action=truncate
[oracle@OCPLHR u02]$ impdp system/oracle@OCPLHR1 directory=exp dumpfile=expdp.dmp table_exists_action=truncate

在这里插入图片描述
在这里插入图片描述

参数文件

[oracle@OCPLHR ~]$ vim /u02/dp.txt

userid=scott/oracle
dumpfile=expdp.dmp
directory=exp
table_exists_action=truncate

在这里插入图片描述

[oracle@OCPLHR u02]$ impdp parfile=/u02/dp.txt

在这里插入图片描述

表空间传输(transport tablespace)

举例:将tt从OCPLHR1传输到OCPLHR2
创建一个表空间tt
SYS@OCPLHR1> create tablespace tt datafile '/u01/app/oracle/oradata/OCPLHR1/tt.dbf' size 10m;

Tablespace created.

SYS@OCPLHR1> create user tt identified by tt default tablespace tt;

User created.

SYS@OCPLHR1> grant connect,resource to tt;

Grant succeeded.

SYS@OCPLHR1> create table tt.e as select * from scott.emp;

Table created. 

在这里插入图片描述

在OCPLHR1库上将tt导出并备份数据文件

SYS@OCPLHR1> alter tablespace tt read only;

Tablespace altered.

在这里插入图片描述

[oracle@OCPLHR u02]$  exp \'sys/oracle as sysdba\' file=/u02/tt.dmp transport_tablespace=y tablespaces=tt

在这里插入图片描述

[oracle@OCPLHR OCPLHR2]$ cp /u01/app/oracle/oradata/OCPLHR1/tt.dbf /u01/app/oracle/OCPLHR2

在这里插入图片描述

SYS@OCPLHR1> alter tablespace tt read write;

在这里插入图片描述

[oracle@OCPLHR u02]$ imp \'sys/oracle as sysdba\' file=/u02/tt.dmp transport_tablespace=y tablespaces=tt datafiles=/u01/app/oracle/OCPLHR2/tt.dbf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值