0.目标
通过Oracle数据泵工具导入导出数据
测试版本 Oracle Release 11.2.0.4.0 - 64bit Production
1 从原始库通过expdp 导出数据库指定对象或一个用户schema 到文件dmp中
- 2 通过impdp将数据文件导入到目标库中
1. 创建导出文件夹目录
1 | SQL>create directory expdir as 'D:\myjava\olm\20170913\approbe0904'; |
2. 查询已经有的directory
查看当前数据库导入导出路径
1 | select * from dba_directories; |
3. 导出dmp文件
注意
users是本地的表空间,请用实际的表空间替换users
操作系统命令:1
expdp "'/ as sysdba'" directory=expdir dumpfile=si0915.dmp logfile=si0915_exp.log schemas=appuser
4.目标数据库导入全量表方法如下:
(1) 删除approbe用户
1 | drop user approbe cascade; |
(2)导入新的全量数据
注意—users是本地的表空间,请用实际的表空间替换users
将appuser用户的数据导入到approbe用户下
操作系统命令:1
impdp "'/ as sysdba'" directory=expdir dumpfile=si0915.dmp logfile=si0915_imp.log remap_schema=appuser:approbe
5.可能遇到的问题
5.1 问题1 :Object type TYPE failed to create with error
5.1.1 错误信息
1 | TYPE: |
5.1.2 错误原因
这个错误是因为同一个OID的对象已经在数据库中存在了,所以导致冲突无法创建,通过impdp help=y命令查看帮助,发现有参数如下:1
123 TRANSFORM Metadata transform to apply to applicable objects. Valid transform keywords: SEGMENT_ATTRIBUTES, STORAGE OID, and PCTSPACE.
查看联机文档发现关于OID这个参数的解释如下:1
1 OID - If the value is specified as n, the assignment of the exported OID during the creation of object tables and types is inhibited. Instead, a new OID is assigned. This can be useful for cloning schemas, but does not affect referenced objects. The default value is y.
意思就是说如果这个OID参数设置为“N”,那么导出的对象的OID在object table和type被创建时会被抑制,它会被分配一个新的OID,这个参数可以被用来clone schema,但是对被引用的对象是无效的。这个参数默认值为“Y”
于是只要将参数transform的OID显示的设置为N即可解决,如下所示:
http://www.ask600.com/ora-39083-object-type-type-failed-to-create-with-error.html
5.1.3 解决办法
1 | impdp "'/ as sysdba'" directory=approbebak dumpfile=APPROBE_V2.4.DMP logfile=APPROBE_V2.4_imp.log remap_schema=approbe:approbebak transform=oid:n |
5.2 问题2:违反唯一性约束 JOB_ID重复
5.2.1 错误日志
1 | ORA-39083: Object type JOB failed to create with error: |
5.2.2 解决办法
(1)导出dmp文件中的ddl sql语句
1
impdp "'/ as sysdba'" directory=approbebak dumpfile=APPROBE_V2.4.DMP logfile=APPROBE_V2.4_imp.log remap_schema=approbe:approbebak transform=oid:n SQLFILE=impdp.sql
(2)在SQLFILE=impdp.sql找到相关JOB id的创建语句1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28-- new object type path: SCHEMA_EXPORT/JOB
BEGIN SYS.DBMS_IJOB.SUBMIT(
JOB=> 401,
LUSER=> 'APPROBEBAK',
PUSER=> 'APPROBEBAK',
CUSER=> 'APPROBEBAK',
NEXT_DATE=> TO_DATE('2017-12-15 09:08:27', 'YYYY-MM-DD:HH24:MI:SS'),
INTERVAL=> 'sysdate+1',
BROKEN=> FALSE,
WHAT=> 'pkg_XXXXX;',
NLSENV=> 'NLS_LANGUAGE=''AMERICAN'' NLS_TERRITORY=''AMERICA'' NLS_CURRENCY=''$'' NLS_ISO_CURRENCY=''AMERICA'' NLS_NUMERIC_CHARACTERS=''.,'' NLS_DATE_FORMAT=''DD-MON-RR'' NLS_DATE_LANGUAGE=''AMERICAN'' NLS_SORT=''BINARY''',
ENV=> '0102000200000000');
END;
/
BEGIN SYS.DBMS_IJOB.SUBMIT(
JOB=> 402,
LUSER=> 'APPROBEBAK',
PUSER=> 'APPROBEBAK',
CUSER=> 'APPROBEBAK',
NEXT_DATE=> TO_DATE('2017-12-15 09:08:27', 'YYYY-MM-DD:HH24:MI:SS'),
INTERVAL=> 'sysdate+1',
BROKEN=> FALSE,
WHAT=> 'pkg_XXXXX.222_XXXXX;',
NLSENV=> 'NLS_LANGUAGE=''AMERICAN'' NLS_TERRITORY=''AMERICA'' NLS_CURRENCY=''$'' NLS_ISO_CURRENCY=''AMERICA'' NLS_NUMERIC_CHARACTERS=''.,'' NLS_DATE_FORMAT=''DD-MON-RR'' NLS_DATE_LANGUAGE=''AMERICAN'' NLS_SORT=''BINARY''',
ENV=> '0102000200000000');
END;
/
(3) 查到最大JOB_ID修改语句
1 | select max (job) from dba_jobs; |
(4) 调整报错SQL语句为最大JOB_ID ,本例为403 404,手工创建如下语句 SYStem用户执行
1 | -- new object type path: SCHEMA_EXPORT/JOB |
(5)查看最后结果是否插入成功
1 | SQL> select job,SCHEMA_USER,what from dba_jobs; |