关于数据泵impdp导入的部分参数的使用验证,本章验证的参数包括remap_data、remap_datafile、remap_schema、remap_table、remap_tablespace、reuse_datafile。
1. remap_data
1.创建源表并增加数据
SQL> create table t_source (id number,name varchar2(20));
Table created.
SQL> insert into t_source values (1,'aaa');
1 row created.
SQL> insert into t_source values (2,'bbb');
1 row created.
2.导出源表
[oracle@ENMOEDU ~]$ expdp \'/ as sysdba\' directory=MY_DIR tables=test.t_source
3.创建包和包体
SQL> create or replace package p_stu
2 as
3 function my_upper(text varchar2) return varchar2;
4 end;
5 /
Package created.
SQL> create or replace package body p_stu as
2 function my_upper(text varchar2) return varchar2 as
3 begin
4 return upper(text);
5 end;
6 end;
7 /
4.验证remap_data
[oracle@ENMOEDU ~]$ impdp \'/ as sysdba\' directory=MY_DIR remap_data=test.t_source.name:test.p_stu.my_upper table_exists_action=replace
SQL> select * from t_source;
ID NAME
---------- --------------------
1 AAA
2 BBB
说明:数据结果从小写字母变成大写字母。remap_data参数的意思是导入指定表时对字段内容作修改。格式为:remap_data=用户名.表名.字段:用户名.包名.方法名
2.remap_datafile
注:修改源数据文件名到目录数据文件名,并参考源数据文件中创建表空间,创建数据字典,创建目录。一般会用在全库导出后跨平台导入。
1. 全库导出:
[oracle@ENMOEDU dmp]$ expdp \'/ as sysdba\' directory=my_dir full=y dumpfile=all.dmp
2. 删除test用户和所属test_space表空间
SQL> drop user test cascade;
User dropped.
SQL> drop tablespace test_space;
Tablespace dropped.
3.手动删除表空间物理文件/u01/app/oracle/oradata/enmo/test01.dbf
FILE_ID FILE_NAME
---------- ------------------------------------------------------------
4 /u01/app/oracle/oradata/enmo/users01.dbf
3 /u01/app/oracle/oradata/enmo/undotbs01.dbf
2 /u01/app/oracle/oradata/enmo/sysaux01.dbf
1 /u01/app/oracle/oradata/enmo/system01.dbf
5 /u01/app/oracle/oradata/enmo/test01.dbf
6 /u01/app/oracle/oradata/enmo/bidpro01.dbf
1. 行执全库导入,验证没有创建test用户和test_space表空间是否会报错
1.全库导入
impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp full=y
2.导入完成后用户和表空间已经自动创建
SQL> select username,default_tablespace from dba_users;
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SYS SYSTEM
SYSTEM SYSTEM
TEST TEST_SPACE
BIDPRO BIDPRO_SPACE
OUTLN SYSTEM
3.表空间数据文件已经自动创建,数据文件路径与文件名与源库一至。
FILE_ID FILE_NAME
---------- ------------------------------------------------------------
4 /u01/app/oracle/oradata/enmo/users01.dbf
3 /u01/app/oracle/oradata/enmo/undotbs01.dbf
2 /u01/app/oracle/oradata/enmo/sysaux01.dbf
1 /u01/app/oracle/oradata/enmo/system01.dbf
5 /u01/app/oracle/oradata/enmo/test01.dbf
6 /u01/app/oracle/oradata/enmo/bidpro01.dbf
说明:非跨平台时不用指定此remap_datafile参数。全库导入会自动创建用户和表空间,并使用源数据库数据文件路径和数据文件名称创建表空间。
2. 导入时使用remap_datafile指定数据文件路径和名字
1.删除test用户和所属test_space表空间
SQL> drop user test cascade;
User dropped.
SQL> drop tablespace test_space;
Tablespace dropped.
2.全库导入时使用remap_datafile指定数据文件
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp full=y remap_datafile=\"\'/u01/app/oracle/oradata/enmo/test01.dbf\':\'/u01/app/oracle/oradata/enmo /test_space.dbf\'\"
3.导入完成后查看数据文件状态
FILE_ID FILE_NAME
---------- ------------------------------------------------------------
4 /u01/app/oracle/oradata/enmo/users01.dbf
3 /u01/app/oracle/oradata/enmo/undotbs01.dbf
2 /u01/app/oracle/oradata/enmo/sysaux01.dbf
1 /u01/app/oracle/oradata/enmo/system01.dbf
5 /u01/app/oracle/oradata/enmo/test_space.dbf
6 /u01/app/oracle/oradata/enmo/bidpro01.dbf
说明:全库导入时用户、表空间、数据文件都可以自动创建,并可以使用remap_datafile更改数据文件路径和数据文件名称。
3.全库导入ASM磁盘管理数据库(源数据库为本地文件管理)
1.如果全库导入ASM磁盘管理的数据库时会提示无法创建表空间,因为导入表空间的创建语句中还使用原来的数据文件路径和名称。测试结果如下:
[oracle@rac2 dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp logfile=all.log full=y
Import: Release 11.2.0.3.0 - Production on Wed Jul 2 22:20:16 2014
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "SYS"."SYS_IMPORT_FULL_01": "/******** AS SYSDBA" directory=my_dir dumpfile=all.dmp logfile=all.log full=y
Processing object type DATABASE_EXPORT/TABLESPACE
ORA-31684: Object type TABLESPACE:"UNDOTBS1" already exists
ORA-31684: Object type TABLESPACE:"TEMP" already exists
ORA-31684: Object type TABLESPACE:"USERS" already exists
ORA-39083: Object type TABLESPACE failed to create with error:
ORA-01119: error in creating database file '/u01/app/oracle/oradata/enmo/test01.dbf'
说明:全库导入不同环境的数据库时需要使用remap_datafile参数重新指定数据文件。
2.全库导入ASM磁盘管理数据库时,使用remap_datafile参数
[oracle@rac2 dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp logfile=test.log remap_datafile=\"\'/u01/app/oracle/oradata/enmo/test01.dbf\':\'+DATA/rac/datafile/test01\'\" full=y
3.用户和表空间已经创建
SQL> select username,default_tablespace from dba_users;
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
MGMT_VIEW SYSTEM
SYS SYSTEM
SYSTEM SYSTEM
DBSNMP SYSAUX
SYSMAN SYSAUX
TEST TEST_SPACE
4.数据文件已经按指定路径和文件名创建
SQL> select tablespace_name,file_name from dba_data_files;
TABLESPACE_NAME FILE_NAME
------------------------------ ------------------------------------------------------------
SYSTEM +DATA/rac/datafile/system.261.850108223
SYSAUX +DATA/rac/datafile/sysaux.260.850108227
UNDOTBS1 +DATA/rac/datafile/undotbs1.259.850108229
UNDOTBS2 +DATA/rac/datafile/undotbs2.257.850108237
USERS +DATA/rac/datafile/users.256.850108239
TEST_SPACE +DATA/rac/datafile/test01
3.remap_schema
1.用全库导出的dmp文件导入单个用户
[oracle@ENMOEDU ~]$ impdp \'/ as sysdba\' directory=my_dir schemas=test remap_schema=test:test1 dumpfile=all.dmp logfile=test1.log
2.导入完成后用户被自动创建
SQL> select username,default_tablespace from dba_users;
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SYS SYSTEM
SYSTEM SYSTEM
TEST1 TEST_SPACE
TEST TEST_SPACE
BIDPRO BIDPRO_SPACE
说明:remap_schema参数指定的目标用户Test1会被自动创建了。test1所属表空间默认使用源表表空间。
3.按用户导出一个dmp文件
[oracle@ENMOEDU ~]$ expdp \'/ as sysdba\' directory=my_dir schemas=test dumpfile=test.dmp
4.导入
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp remap_schema=test:test2 logfile=re_sch.log
5.用户已经创建
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SYS SYSTEM
SYSTEM SYSTEM
TEST2 TEST_SPACE
TEST1 TEST_SPACE
TEST TEST_SPACE
说明:与是否全库导出的dmp文件无关,remap_schema指定的目标用户都会自动被创建。
4. remap_table
注:格式:Remap_table=旧表名:新表名
1.使用remap_table参数执行导入
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp tables=t_source remap_table=t_source:t_source3
Import: Release 11.2.0.3.0 - Production on Thu Jul 10 14:52:51 2014
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39002: invalid operation
ORA-39166: Object SYS.T_SOURCE was not found.
说明:因为是用sysdba用户来操作的,所以未找到t_source表。
2.在参数tables中指定表的所属用户,重新导入
impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp tables=test.t_source remap_table=t_source:t_source3
3.导入完成
SQL> select count(*) from test.t_source3;
COUNT(*)
----------
2
4.删除用户(验证一下按用户导入时,使用schemas参数指定用户,并指定remap_table重命名表名,源表是否同样被导入。)
SQL> drop user test cascade;
User dropped.
5.导入时同时使用schemas和remap_table参数
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp schemas=test remap_table=t_source:t_source5
6.导入完成后只有t_source5表,没有源表t_source。说明remap_table只是将源表重命名后导入目标库。
SQL> select table_name from dba_tables where owner='TEST';
TABLE_NAME
------------------------------
PRO_BDG_ALLOCATION_TBL
T_SOURCE2
T_TASKDONE
T_BASEITEM
T_TARGET
T_SOURCE5
TEST.T_TARGET
PRO_APP_PROJECT_BASEINFO_TBL
PRO_APP_INVEST_BUDGET_TBL
PRO_BDG_APPBDG_CTRL_TBL
PRO_ATH_PROORBDG_CTRL_TBL
5. remap_tablespace
注:如果参数中使用table_exists_action,并且值为,skip,truncate,append,那么目标库中的数据将不会受到任务影响。
1. 验证是否会自动创建新的表空间
1.如果目标数据库中未创建remap_tablespace参数指定表空间,执行导入
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp remap_schema=test:test1 remap_tablespace=test_space:test_space2 table_exists_action=replace
执行后出现错误信息:ORA-00959: tablespace 'TEST_SPACE2' does not exist说明使用remap_tablespace时表空间不会自动创建,必须手动创建。(可能是因为创建表空间时要先创建数据文件。)
2.验证remap_tablespace
1.创建一个表空间
SQL> create tablespace test_space2 datafile '/u01/app/oracle/oradata/enmo/test_space2.dbf' size 20M autoextend on next 5M;
Tablespace created.
2.导入时不指定table_exists_action,默认skip。
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp remap_schema=test:test1 remap_tablespace=test_space:test_space2
3.导入完成,数据已经导入到指定表空间
SQL> select OWNER,TABLE_NAME,TABLESPACE_NAME from dba_tables where table_name =upper('t_source');
OWNER TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
TEST1 T_SOURCE TEST_SPACE2
TEST2 T_SOURCE TEST_SPACE
3.验证remap_tablespace已经存在的数据是否会移到指定表空间
1.新创建一个无数据的表空间
SQL> create tablespace test_space3 datafile '/u01/app/oracle/oradata/enmo/test_space3.dbf' size 20M autoextend on next 5M;
Tablespace created.
2.执行导入(在目标数据库中,要导入的数据已经存在于test_space表空间中)
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp remap_tablespace=test_space:test_space3
3.因为table_exists_action参数默认是skip所以test_space3表空间中没有数据
SQL> select count(*) from dba_tables where tablespace_name=upper('test_space3');
COUNT(*)
----------
0
说明:因为这些表在目录库的表空间中都已经存在了。它们并不会移到remap_tablespace 新指定的表空间中,所以表空间test_space3无数据。
4. 使用remap_tablespace并设置table_exists_action=replace验证已经存在的数据是否会移到新表空间
1.创建新表空间
SQL> create tablespace space01 datafile '/u01/app/oracle/oradata/enmo/space01.dbf' size 10M autoextend on;
Tablespace created.
2.执行导入
[oracle@ENMOEDU ~]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp schemas=test remap_tablespace=TEST_SPACE:space01 table_exi
sts_action=replace
3.查询数据是否移到新表空间
SQL> select owner,table_name ,tablespace_name from dba_tables where tablespace_name in ('SPACE01','TEST_SPACE');
OWNER TABLE_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
TEST T_BASEITEM SPACE01
TEST T_TASKDONE SPACE01
TEST PRO_BDG_ALLOCATION_TBL SPACE01
TEST PRO_APP_INVEST_BUDGET_TBL SPACE01
TEST PRO_APP_PROJECT_BASEINFO_TBL SPACE01
TEST PRO_ATH_PROORBDG_CTRL_TBL SPACE01
TEST PRO_BDG_APPBDG_CTRL_TBL SPACE01
说明在设置table_exists_action=replace时会将源表空间中已经存在的表删除,重新导入到新表空间中。
6.reuse_datafile
使用已经存在的数据文件来创建表空间。只有两个参数值yes,no。指定参数yes,将重新初始化数据文件,可能造成数据丢失。
1.删除表空间和用户,但未删除数据文件
SQL> drop tablespace TEST_SPACE;
Tablespace dropped.
SQL> drop user test cascade;
User dropped.
SQL> drop tablespace SPACE01;
Tablespace dropped.
SQL> select tablespace_name ,file_name from dba_data_files;
TABLESPACE_NAME FILE_NAME
-------------------- ------------------------------------------------------------
USERS /u01/app/oracle/oradata/enmo/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/enmo/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/enmo/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/enmo/system01.dbf
BIDPRO_SPACE /u01/app/oracle/oradata/enmo/bidpro01.dbf
2查看数据文件(未删除)
[oracle@ENMOEDU enmo]$ ls -trl
total 6325148
-rw-r----- 1 oracle oinstall 52429312 Jul 11 00:13 redo02.log
-rw-r----- 1 oracle oinstall 30416896 Jul 11 00:13 temp01.dbf
-rw-r----- 1 oracle oinstall 52429312 Jul 11 00:30 redo03.log
-rw-r----- 1 oracle oinstall 828579840 Jul 11 00:31 test01.dbf
-rw-r----- 1 oracle oinstall 5251072 Jul 11 00:35 users01.dbf
-rw-r----- 1 oracle oinstall 828579840 Jul 11 00:35 space01.dbf
-rw-r----- 1 oracle oinstall 3221233664 Jul 11 00:35 bidpro01.dbf
-rw-r----- 1 oracle oinstall 78651392 Jul 11 00:35 undotbs01.dbf
-rw-r----- 1 oracle oinstall 754982912 Jul 11 00:35 system01.dbf
-rw-r----- 1 oracle oinstall 566239232 Jul 11 00:35 sysaux01.dbf
-rw-r----- 1 oracle oinstall 52429312 Jul 11 00:35 redo01.log
-rw-r----- 1 oracle oinstall 10043392 Jul 11 00:35 control01.ctl
3.执行按用户导入
[oracle@ENMOEDU enmo]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=test.dmp schemas=test reuse_datafiles=y
Import: Release 11.2.0.3.0 - Production on Fri Jul 11 00:42:26 2014
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-39005: inconsistent arguments
ORA-39032: function REUSE_DATAFILES is not supported in SCHEMA jobs
Reuse_datafiles不支持指定schema操作,可能只适用全库导出的文件(全库导出的dmp文件中记录数据文件路径和名称)。
4.使用全库导出的dmp文件执行导入
[oracle@ENMOEDU dmp]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp schemas=test reuse_datafiles=y
SQL> select tablespace_name ,file_name from dba_data_files;
TABLESPACE_NAME FILE_NAME
-------------------- ------------------------------------------------------------
USERS /u01/app/oracle/oradata/enmo/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/enmo/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/enmo/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/enmo/system01.dbf
TEST_SPACE /u01/app/oracle/oradata/enmo/test01.dbf
BIDPRO_SPACE /u01/app/oracle/oradata/enmo/bidpro01.dbf
删除数据文件,验证如果没有dmp文件中记录的数据文件。是重新生成数据文件还是重用其它无用的数据文件
SQL> drop user test cascade;
User dropped.
SQL> drop tablespace TEST_SPACE;
Tablespace dropped.
删除test_space表空间所属数据文件
[oracle@ENMOEDU enmo]$ rm test01.dbf
执行全库导入
[oracle@ENMOEDU enmo]$ impdp \'/ as sysdba\' directory=my_dir dumpfile=all.dmp reuse_datafiles=y
只会重用空间指定文件名的数据文件,如果找不到还是会重新创建数据文件。
TABLESPACE_NAME FILE_NAME
-------------------- ------------------------------------------------------------
USERS /u01/app/oracle/oradata/enmo/users01.dbf
UNDOTBS1 /u01/app/oracle/oradata/enmo/undotbs01.dbf
SYSAUX /u01/app/oracle/oradata/enmo/sysaux01.dbf
SYSTEM /u01/app/oracle/oradata/enmo/system01.dbf
TEST_SPACE /u01/app/oracle/oradata/enmo/test01.dbf
BIDPRO_SPACE /u01/app/oracle/oradata/enmo/bidpro01.dbf
无用的数据文件space01.dbf并没有重用,还是会按dmp文件中记录的文件路径和名称生成新的数据文件。
[oracle@ENMOEDU enmo]$ ls -trl
total 6381720
-rw-r----- 1 oracle oinstall 828579840 Jul 11 00:38 space01.dbf
-rw-r----- 1 oracle oinstall 52429312 Jul 11 01:16 redo02.log
-rw-r----- 1 oracle oinstall 30416896 Jul 11 01:16 temp01.dbf
-rw-r----- 1 oracle oinstall 5251072 Jul 11 01:16 users01.dbf
-rw-r----- 1 oracle oinstall 78651392 Jul 11 01:16 undotbs01.dbf
-rw-r----- 1 oracle oinstall 807411712 Jul 11 01:16 system01.dbf
-rw-r----- 1 oracle oinstall 566239232 Jul 11 01:16 sysaux01.dbf
-rw-r----- 1 oracle oinstall 3221233664 Jul 11 01:16 bidpro01.dbf
-rw-r----- 1 oracle oinstall 52429312 Jul 11 01:16 redo03.log
-rw-r----- 1 oracle oinstall 10043392 Jul 11 01:16 control01.ctl
-rw-r----- 1 oracle oinstall 52429312 Jul 11 01:16 redo01.log
-rw-r----- 1 oracle oinstall 829628416 Jul 11 01:16 test01.dbf
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28793776/viewspace-1591913/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28793776/viewspace-1591913/