ORACLE数据泵导入导出案例(expdp & impdp)

概要:

因项目需要,通常需要将生产库下的部分数据抽取并恢复到测试库上

本文主要介绍数据泵导入导出的几种情况以及错误处理

案例环境:

rhel-server-6.5-x86_64

oracle 11.2.0.4.0

一、数据泵导出

a、按用户导出

b、按表导出

b1、全表导出

b2、按查询条件导出表

c、按表空间导出

二、数据泵导入

a、导入到具体用户

a1、按用户导入,不改变schema

a2、按用户导入,改变schema

b、导入表

b1、导入表,不改变schema

b2、导入表,改变schema

c、导入表空间

源数据库:

1、首先查看数据泵目录

[sql]  view plain  copy
  1. SQL> set lines 200  
  2. SQL> col DIRECTORY_PATH for a80  
  3. SQL> select * from dba_directories;  
  4.   
  5. OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH  
  6. ------------------------------ ------------------------------ --------------------------------------------------------------------------------  
  7. SYS                            SUBDIR                         /u01/app/oracle/product/11204/db/demo/schema/order_entry//2002/Sep  
  8. SYS                            SS_OE_XMLDIR                   /u01/app/oracle/product/11204/db/demo/schema/order_entry/  
  9. SYS                            LOG_FILE_DIR                   /u01/app/oracle/product/11204/db/demo/schema/log/  
  10. SYS                            MEDIA_DIR                      /u01/app/oracle/product/11204/db/demo/schema/product_media/  
  11. SYS                            DATA_FILE_DIR                  /u01/app/oracle/product/11204/db/demo/schema/sales_history/  
  12. SYS                            XMLDIR                         /u01/app/oracle/product/11204/db/rdbms/xml  
  13. SYS                            ORACLE_OCM_CONFIG_DIR          /u01/app/oracle/product/11204/db/ccr/hosts/localhost.localdomain/state  
  14. SYS                            DATA_PUMP_DIR                  /u01/app/oracle/admin/orcl/dpdump/  
  15. SYS                            ORACLE_OCM_CONFIG_DIR2         /u01/app/oracle/product/11204/db/ccr/state  
  16. rows selected.  
  17. SQL>  
 
 
 
 
 

此处,我们数据泵目录选用DATA_PUMP_DIR ,其对应的目录路径为  /u01/app/oracle/admin/orcl/dpdump/  当然此处也可以新建数据泵的directory,命令如下:

[sql]  view plain  copy
  1. SQL> create directory dump_test as'/dump/test';  
  2.   
  3. Directory created.  
 

2、赋权限(数据泵导入导出时的执行用户 对上面选用的数据泵目录要有读写权限)

[sql]  view plain  copy
  1. SQL> grant read,write on directory DATA_PUMP_DIR to hr;  
  2.   
  3. Grant succeeded.  
 

3、数据泵导出

      3.1>按用户导出

a>首先查看需要导出的用户所在的表空间,以及该用户下的表在初始化时占用表空间的大小(换算为MB)此处以HR用户为例:

[sql]  view plain  copy
  1. SQL> SELECT ds.tablespace_name,  
  2.   2         SUM(ds.initial_extent) / 1024 / 1024 as initial_extent  
  3.   3    FROM DBA_SEGMENTS ds  
  4.   4   WHERE ds.owner IN ( --查看账户状态为open的用户HR  
  5.   5                      SELECT du.username  
  6.   6                        FROM DBA_USERS du  
  7.   7                       WHERE du.account_status = 'OPEN'  
  8.   8                         and du.username = 'HR')  
  9.   9   GROUP BY ds.tablespace_name  
  10.  10   ORDER BY initial_extent desc;  
  11.   
  12. TABLESPACE_NAME                INITIAL_EXTENT  
  13. ------------------------------ --------------  
  14. EXAMPLE                                1.5625  
  15. USERS                                   .0625  
  16.   
  17. SQL>   

b>在表空间数量较多的情况下,可以用以下语句查看表在初始化时占用表空间的总大小:

[sql]  view plain  copy
  1. SQL> SELECT SUM(ds.initial_extent) / 1024 / 1024 as initial_extent  
  2.   2    FROM DBA_SEGMENTS ds  
  3.   3   WHERE ds.owner IN ( --查看账户状态为open的用户HR  
  4.   4                      SELECT du.username  
  5.   5                        FROM DBA_USERS du  
  6.   6                       WHERE du.account_status = 'OPEN'  
  7.   7                         and du.username = 'HR');  
  8.   
  9.   
  10. INITIAL_EXTENT  
  11. --------------  
  12.          1.625  
  13.   
  14. SQL>  

c>查看表空间对应的数据文件大小

[sql]  view plain  copy
  1. SQL> set lines 200  
  2. SQL> col FILE_NAME for a50  
  3. SQL> col TOTAL_SPACE for a20  
  4. SQL> select tablespace_name, file_id, file_name,  
  5.   2  round(bytes/(1024*1024),0)||' MB' total_space,AUTOEXTENSIBLE  
  6.   3  from dba_data_files  
  7.   4  order by tablespace_name;  
  8.   
  9. TABLESPACE_NAME                   FILE_ID FILE_NAME                                          TOTAL_SPACE          AUT  
  10. ------------------------------ ---------- -------------------------------------------------- -------------------- ---  
  11. EXAMPLE                                 5 /oradata/orcl/example01.dbf                        313 MB               YES  
  12. SYSAUX                                  2 /oradata/orcl/sysaux01.dbf                         520 MB               YES  
  13. SYSTEM                                  1 /oradata/orcl/system01.dbf                         750 MB               YES  
  14. TEST_TBS                                6 /oradata/orcl/test_tbs.dbf                         30 MB                YES  
  15. UNDOTBS1                                3 /oradata/orcl/undotbs01.dbf                        90 MB                YES  
  16. USERS                                   4 /oradata/orcl/users01.dbf                          5 MB                 YES  
  17.   
  18. rows selected.  
  19.   
  20. SQL>  
上面a、b、c三个步骤查到的结果,在目标库规划存储空间大小和建表空间初始大小时作为参考条件。

d> expdp按用户导出(schemas=hr

[sql]  view plain  copy
  1. $ expdp hr/hr directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=hr.log schemas=hr  
结果如下:

[sql]  view plain  copy
  1. Export: Release 11.2.0.4.0 - Production on Sun Jul 3 21:38:06 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Starting "HR"."SYS_EXPORT_SCHEMA_01":  hr/******** directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=hr.log schemas=hr   
  8. Estimate in progress using BLOCKS method...  
  9. Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA  
  10. Total estimation using BLOCKS method: 448 KB  
  11. Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA  
  12. Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE  
  13. Processing object type SCHEMA_EXPORT/TABLE/TABLE  
  14. Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  15. Processing object type SCHEMA_EXPORT/TABLE/COMMENT  
  16. Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE  
  17. Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE  
  18. Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX  
  19. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  20. Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  21. Processing object type SCHEMA_EXPORT/VIEW/VIEW  
  22. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  23. Processing object type SCHEMA_EXPORT/TABLE/TRIGGER  
  24. Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  25. . . exported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  26. . . exported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  27. . . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  28. . . exported "HR"."JOBS"                                 6.992 KB      19 rows  
  29. . . exported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  30. . . exported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  31. . . exported "HR"."REGIONS"                              5.476 KB       4 rows  
  32. Master table "HR"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded  
  33. ******************************************************************************  
  34. Dump file set for HR.SYS_EXPORT_SCHEMA_01 is:  
  35.   /u01/app/oracle/admin/orcl/dpdump/hr.dmp  
  36. Job "HR"."SYS_EXPORT_SCHEMA_01" successfully completed at Sun Jul 3 21:39:19 2016 elapsed 0 00:01:09  

      3.2>按表导出

3.2.1>全表导出

[sql]  view plain  copy
  1. expdp hr/hr directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=tab.log tables=hr.employees,hr.departments  
结果如下:

[sql]  view plain  copy
  1. Export: Release 11.2.0.4.0 - Production on Sun Jul 3 21:51:45 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Starting "HR"."SYS_EXPORT_TABLE_01":  hr/******** directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=tab.log tables=hr.employees,hr.departments   
  8. Estimate in progress using BLOCKS method...  
  9. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  10. Total estimation using BLOCKS method: 128 KB  
  11. Processing object type TABLE_EXPORT/TABLE/TABLE  
  12. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  13. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  14. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  15. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  16. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  17. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  18. Processing object type TABLE_EXPORT/TABLE/TRIGGER  
  19. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  20. . . exported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  21. . . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  22. Master table "HR"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded  
  23. ******************************************************************************  
  24. Dump file set for HR.SYS_EXPORT_TABLE_01 is:  
  25.   /u01/app/oracle/admin/orcl/dpdump/tab.dmp  
  26. Job "HR"."SYS_EXPORT_TABLE_01" successfully completed at Sun Jul 3 21:52:05 2016 elapsed 0 00:00:17  

             3.2.2>按查询条件导出表

[sql]  view plain  copy
  1. $ expdp hr/hr directory=DATA_PUMP_DIR dumpfile=condition.dmp logfile=condition.log tables=hr.employees query=\' WHERE DEPARTMENT_ID\=\10\'  

结果如下: 
  

[sql]  view plain  copy
  1. Export: Release 11.2.0.4.0 - Production on Sun Jul 3 22:06:09 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Starting "HR"."SYS_EXPORT_TABLE_01":  hr/******** directory=DATA_PUMP_DIR dumpfile=condition.dmp logfile=condition.log tables=hr.employees query=' WHERE DEPARTMENT_ID=10'   
  8. Estimate in progress using BLOCKS method...  
  9. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  10. Total estimation using BLOCKS method: 64 KB  
  11. Processing object type TABLE_EXPORT/TABLE/TABLE  
  12. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  13. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  14. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  15. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  16. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  17. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  18. Processing object type TABLE_EXPORT/TABLE/TRIGGER  
  19. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  20. . . exported "HR"."EMPLOYEES"                            9.320 KB       1 rows  
  21. Master table "HR"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded  
  22. ******************************************************************************  
  23. Dump file set for HR.SYS_EXPORT_TABLE_01 is:  
  24.   /u01/app/oracle/admin/orcl/dpdump/condition.dmp  
  25. Job "HR"."SYS_EXPORT_TABLE_01" successfully completed at Sun Jul 3 22:06:25 2016 elapsed 0 00:00:14  

      3.4>按表空间导出

             以example表空间为例

[sql]  view plain  copy
  1. expdp hr/hr directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tablespace.log  TABLESPACES=example;  
结果如下:

[sql]  view plain  copy
  1. Export: Release 11.2.0.4.0 - Production on Sun Jul 3 22:14:26 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Starting "HR"."SYS_EXPORT_TABLESPACE_01":  hr/******** directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tablespace.log TABLESPACES=example   
  8. Estimate in progress using BLOCKS method...  
  9. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  10. Total estimation using BLOCKS method: 448 KB  
  11. Processing object type TABLE_EXPORT/TABLE/TABLE  
  12. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  13. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  14. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  15. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  16. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  17. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  18. Processing object type TABLE_EXPORT/TABLE/TRIGGER  
  19. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  20. . . exported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  21. . . exported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  22. . . exported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  23. . . exported "HR"."JOBS"                                 6.992 KB      19 rows  
  24. . . exported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  25. . . exported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  26. . . exported "HR"."REGIONS"                              5.476 KB       4 rows  
  27. Master table "HR"."SYS_EXPORT_TABLESPACE_01" successfully loaded/unloaded  
  28. ******************************************************************************  
  29. Dump file set for HR.SYS_EXPORT_TABLESPACE_01 is:  
  30.   /u01/app/oracle/admin/orcl/dpdump/tablespace.dmp  
  31. Job "HR"."SYS_EXPORT_TABLESPACE_01" successfully completed at Sun Jul 3 22:15:04 2016 elapsed 0 00:00:36  

4、将源数据库出的文件传到目标数据库

      4.1>首先查看目标端据库的数据泵目录

[sql]  view plain  copy
  1. SQL> set lines 200  
  2. SQL> col DIRECTORY_PATH for a80  
  3. SQL> select * from dba_directories;  
  4.   
  5. OWNER                          DIRECTORY_NAME                 DIRECTORY_PATH  
  6. ------------------------------ ------------------------------ --------------------------------------------------------------------------------  
  7. SYS                            XMLDIR                         /u01/app/oracle/product/11204/db/rdbms/xml  
  8. SYS                            ORACLE_OCM_CONFIG_DIR          /u01/app/oracle/product/11204/db/ccr/hosts/ggs2/state  
  9. SYS                            DATA_PUMP_DIR                  /u01/app/oracle/admin/hankey/dpdump/  
  10. SYS                            ORACLE_OCM_CONFIG_DIR2         /u01/app/oracle/product/11204/db/ccr/state  
  11.   
  12. SQL>   
 
 

       4.2>在源端,通过scp命令或者其他工具传送文件到目标端,此处使用scp命令

[sql]  view plain  copy
  1. $ pwd  
  2. /u01/app/oracle/admin/orcl/dpdump  
  3. $ ls  
  4. condition.dmp  condition.log  export.log  full.dmp  full.log  hr.dmp  hr.log  tab.dmp  tablespace.dmp  tablespace.log  tab.log  
  5. $ scp * 10.10.10.10:/u01/app/oracle/admin/hankey/dpdump/  
  6. The authenticity of host '10.10.10.10 (10.10.10.10)' can't be established.  
  7. RSA key fingerprint is 42:41:f4:75:62:6d:17:2c:e8:79:c1:05:97:82:41:7b.  
  8. Are you sure you want to continue connecting (yes/no)? yes  
  9. Warning: Permanently added '10.10.10.10' (RSA) to the list of known hosts.  
  10. oracle@10.10.10.10's password:   
  11. condition.dmp                                                                                                      100%  176KB 176.0KB/s   00:00      
  12. condition.log                                                                                                      100% 1613     1.6KB/s   00:00      
  13. export.log                                                                                                         100% 2056     2.0KB/s   00:00      
  14. full.dmp                                                                                                           100%  151MB  21.6MB/s   00:07      
  15. full.log                                                                                                           100%  105KB 104.5KB/s   00:00      
  16. hr.dmp                                                                                                             100%  488KB 488.0KB/s   00:00      
  17. hr.log                                                                                                             100% 2325     2.3KB/s   00:00      
  18. tab.dmp                                                                                                            100%  224KB 224.0KB/s   00:00      
  19. tablespace.dmp                                                                                                     100%  404KB 404.0KB/s   00:00      
  20. tablespace.log                                                                                                     100% 2079     2.0KB/s   00:00      
  21. tab.log                                                                                                            100% 1658     1.6KB/s   00:00      
  22. $   

目标端(实例名为hankey)

5、进入目标端的数据泵目录,切换用户到root,查看文件并修改权限和目标端数据库一致

[sql]  view plain  copy
  1. $ su - root  
  2. Password:   
  3. # cd /u01/app/oracle/admin/hankey/dpdump  
  4. # chown oracle:oinstall *  
  5. # ls -la  
  6. total 156580  
  7. drwxr-x---. 2 oracle oinstall      4096 Jul  4 01:00 .  
  8. drwxr-x---. 5 oracle oinstall      4096 Jun 19 20:26 ..  
  9. -rw-r-----. 1 oracle oinstall    180224 Jul  4 01:00 condition.dmp  
  10. -rw-r--r--. 1 oracle oinstall      1613 Jul  4 01:00 condition.log  
  11. -rw-r-----. 1 oracle oinstall       116 Jun 19 20:39 dp.log  
  12. -rw-r--r--. 1 oracle oinstall      2056 Jul  4 01:00 export.log  
  13. -rw-r-----. 1 oracle oinstall 158691328 Jul  4 01:00 full.dmp  
  14. -rw-r--r--. 1 oracle oinstall    107013 Jul  4 01:00 full.log  
  15. -rw-r-----. 1 oracle oinstall    499712 Jul  4 01:00 hr.dmp  
  16. -rw-r--r--. 1 oracle oinstall      2325 Jul  4 01:00 hr.log  
  17. -rw-r-----. 1 oracle oinstall    229376 Jul  4 01:00 tab.dmp  
  18. -rw-r-----. 1 oracle oinstall    413696 Jul  4 01:00 tablespace.dmp  
  19. -rw-r--r--. 1 oracle oinstall      2079 Jul  4 01:00 tablespace.log  
  20. -rw-r--r--. 1 oracle oinstall      1658 Jul  4 01:00 tab.log  
  21. #   
 
  

6、查看需要目标数据库存在的表空间

[sql]  view plain  copy
  1. SQL> select name from v$tablespace;  
  2.   
  3. NAME  
  4. ------------------------------  
  5. SYSTEM  
  6. SYSAUX  
  7. UNDOTBS1  
  8. TEMP  
  9. USERS  
 
  

参考步骤3.1对比源数据库和目标数据库表空间差异,发现,目标端无EXAMPLE,所以在目标端创建EXAMPL 表空间 ,

参考源数据库估算,这里初始大小设置为300M,允许扩展数据文件,在需要扩展时,则分配给数据文件的下一个磁盘空间量的大小为50M

[sql]  view plain  copy
  1. SQL> create tablespace EXAMPLE   
  2.   2  datafile '/oradata/hankey/example01.dbf' size 300M  
  3.   3  autoextend on next 50M;  
  4.   
  5. Tablespace created.  
  6.   
  7. SQL>  
 
 

附:若创建不合适,需要删除表空间的话,执行以下语句,然后重新创建即可。

[sql]  view plain  copy
  1. SQL> drop tablespace example including contents and datafiles cascade constraints;   

7、数据泵导入

       7.1>导入到具体用户

7.1.1>按用户导入,不改变schema

             对应步骤3.1按用户导出的文件 

首先创建需要导入的用户hr

[sql]  view plain  copy
  1. SQL> create user hr identified by hr;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to hr;  
  6.   
  7. Grant succeeded.  
执行导入命令 
  

[sql]  view plain  copy
  1. $ impdp  \'/ as sysdba\'  directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imhr.log SCHEMAS=hr;  

结果如下:

[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Mon Jul 4 21:00:46 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_SCHEMA_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imhr.log SCHEMAS=hr   
  9. Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA  
  10. Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE  
  11. Processing object type SCHEMA_EXPORT/TABLE/TABLE  
  12. Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA  
  13. . . imported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  14. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  15. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  16. . . imported "HR"."JOBS"                                 6.992 KB      19 rows  
  17. . . imported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  18. . . imported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  19. . . imported "HR"."REGIONS"                              5.476 KB       4 rows  
  20. Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  21. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  22. ORA-01917: user or role 'OE' does not exist  
  23. Failing sql is:  
  24. GRANT REFERENCES ON "HR"."COUNTRIES" TO "OE"  
  25. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  26. ORA-01917: user or role 'OE' does not exist  
  27. Failing sql is:  
  28. GRANT SELECT ON "HR"."COUNTRIES" TO "OE"  
  29. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  30. ORA-01917: user or role 'OE' does not exist  
  31. Failing sql is:  
  32. GRANT SELECT ON "HR"."JOB_HISTORY" TO "OE"  
  33. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  34. ORA-01917: user or role 'OE' does not exist  
  35. Failing sql is:  
  36. GRANT REFERENCES ON "HR"."EMPLOYEES" TO "OE"  
  37. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  38. ORA-01917: user or role 'OE' does not exist  
  39. Failing sql is:  
  40. GRANT SELECT ON "HR"."EMPLOYEES" TO "OE"  
  41. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  42. ORA-01917: user or role 'OE' does not exist  
  43. Failing sql is:  
  44. GRANT SELECT ON "HR"."JOBS" TO "OE"  
  45. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  46. ORA-01917: user or role 'OE' does not exist  
  47. Failing sql is:  
  48. GRANT SELECT ON "HR"."LOCATIONS" TO "OE"  
  49. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  50. ORA-01917: user or role 'OE' does not exist  
  51. Failing sql is:  
  52. GRANT REFERENCES ON "HR"."LOCATIONS" TO "OE"  
  53. ORA-39083: Object type OBJECT_GRANT failed to create with error:  
  54. ORA-01917: user or role 'OE' does not exist  
  55. Failing sql is:  
  56. GRANT SELECT ON "HR"."DEPARTMENTS" TO "OE"  
  57. Processing object type SCHEMA_EXPORT/TABLE/COMMENT  
  58. Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE  
  59. ORA-31684: Object type PROCEDURE:"HR"."ADD_JOB_HISTORY" already exists  
  60. ORA-31684: Object type PROCEDURE:"HR"."SECURE_DML" already exists  
  61. Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE  
  62. Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX  
  63. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  64. Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  65. Processing object type SCHEMA_EXPORT/VIEW/VIEW  
  66. ORA-31684: Object type VIEW:"HR"."EMP_DETAILS_VIEW" already exists  
  67. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  68. Processing object type SCHEMA_EXPORT/TABLE/TRIGGER  
  69. Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  70. Job "SYS"."SYS_IMPORT_SCHEMA_01" completed with 15 error(s) at Mon Jul 4 21:00:55 2016 elapsed 0 00:00:07  

发现有报错,从报错信息来看,是因为赋权限时找不到名为OE的用户,实际此时hr用户的数据已经完全导入,若就希望在执行时候不报错误,那么继续往下走

通过语句查看目标库确实无OE用户

[sql]  view plain  copy
  1. select username from dba_users;  

所以,创建oe用户:

[sql]  view plain  copy
  1. SQL> create user oe identified by oe;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to oe;  
  6.   
  7.   
  8. Grant succeeded.  
  9.   
  10. SQL>  
因上述导入报错,但实际hr用户数据已经导入,若要重新完全导入,首先删除hr用户。

[sql]  view plain  copy
  1. SQL> drop user hr cascade;  
重新创建用户

[sql]  view plain  copy
  1. SQL> create user hr identified by hr;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to hr;  
  6.   
  7. Grant succeeded.  
  8.   
  9. SQL>   
重新执行导入语句:

[sql]  view plain  copy
  1. $ impdp  \'/ as sysdba\'  directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imhr.log SCHEMAS=hr;  
结果如下:

[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Mon Jul 4 21:43:25 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_SCHEMA_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imhr.log SCHEMAS=hr   
  9. Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA  
  10. Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE  
  11. Processing object type SCHEMA_EXPORT/TABLE/TABLE  
  12. Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA  
  13. . . imported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  14. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  15. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  16. . . imported "HR"."JOBS"                                 6.992 KB      19 rows  
  17. . . imported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  18. . . imported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  19. . . imported "HR"."REGIONS"                              5.476 KB       4 rows  
  20. Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  21. Processing object type SCHEMA_EXPORT/TABLE/COMMENT  
  22. Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE  
  23. Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE  
  24. Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX  
  25. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  26. Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  27. Processing object type SCHEMA_EXPORT/VIEW/VIEW  
  28. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  29. Processing object type SCHEMA_EXPORT/TABLE/TRIGGER  
  30. Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  31. Job "SYS"."SYS_IMPORT_SCHEMA_01" successfully completed at Mon Jul 4 21:43:33 2016 elapsed 0 00:00:06  
导入成功后,可自行连接hr用户验证核对源库和目标库的数据。

7.1.2>按用户导入,改变schema

首先,目标端新建用户,如下,创建名为buzhengjing的用户

[sql]  view plain  copy
  1. SQL> create user buzhengjing identified by buzhengjing;  
  2.   
  3. User created.  
  4. SQL> grant connect,resource to buzhengjing;  
  5.   
  6. Grant succeeded.  
  7.   
  8. SQL>   
执行导入语句,注意REMAP_SCHEMA=hr:buzhengjing,参数中冒号的前半部分为源数据库的SCHEMA,后半部分为目标数据库要导入的SCHEMA,即新建的用户

[sql]  view plain  copy
  1. $ impdp  \'/ as sysdba\'  directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imbzj.log REMAP_SCHEMA=hr:buzhengjing  
执行结果如下

[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Tue Jul 5 22:00:02 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_FULL_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=hr.dmp logfile=imbzj.log REMAP_SCHEMA=hr:buzhengjing   
  9. Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA  
  10. Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE  
  11. Processing object type SCHEMA_EXPORT/TABLE/TABLE  
  12. Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA  
  13. . . imported "BUZHENGJING"."COUNTRIES"                   6.367 KB      25 rows  
  14. . . imported "BUZHENGJING"."DEPARTMENTS"                 7.007 KB      27 rows  
  15. . . imported "BUZHENGJING"."EMPLOYEES"                   16.80 KB     107 rows  
  16. . . imported "BUZHENGJING"."JOBS"                        6.992 KB      19 rows  
  17. . . imported "BUZHENGJING"."JOB_HISTORY"                 7.054 KB      10 rows  
  18. . . imported "BUZHENGJING"."LOCATIONS"                   8.273 KB      23 rows  
  19. . . imported "BUZHENGJING"."REGIONS"                     5.476 KB       4 rows  
  20. Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  21. Processing object type SCHEMA_EXPORT/TABLE/COMMENT  
  22. Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE  
  23. Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE  
  24. Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX  
  25. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  26. Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  27. Processing object type SCHEMA_EXPORT/VIEW/VIEW  
  28. Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  29. Processing object type SCHEMA_EXPORT/TABLE/TRIGGER  
  30. Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  31. Job "SYS"."SYS_IMPORT_FULL_01" successfully completed at Tue Jul 5 22:02:08 2016 elapsed 0 00:00:06  

        7.2>导入表

              用到步骤3.2按表导出的数据文件

              7.2.1>导入表,不改变schema

导入表到同名用户下,由于上一步骤已导入数据,所以此处删除hr用户并重新创建。

[sql]  view plain  copy
  1. SQL> drop user hr cascade;  
重新创建用户

[sql]  view plain  copy
  1. SQL> create user hr identified by hr;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to hr;  
  6.   
  7. Grant succeeded.  
  8.   
  9. SQL>   

执行导入命令

[sql]  view plain  copy
  1. $ impdp  \'/ as sysdba\'  directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=imtab.log  
执行结果:
[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Mon Jul 4 22:16:03 2016  
  2.   
  3.   
  4. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  5.   
  6.   
  7. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  8. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  9. Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded  
  10. Starting "SYS"."SYS_IMPORT_FULL_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=imtab.log   
  11. Processing object type TABLE_EXPORT/TABLE/TABLE  
  12. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  13. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  14. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  15. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  16. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  17. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  18. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  19. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  20. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  21. ORA-39083: Object type REF_CONSTRAINT failed to create with error:  
  22. ORA-00942: table or view does not exist  
  23. Failing sql is:  
  24. ALTER TABLE "HR"."EMPLOYEES" ADD CONSTRAINT "EMP_JOB_FK" FOREIGN KEY ("JOB_ID"REFERENCES "HR"."JOBS" ("JOB_ID") ENABLE  
  25. ORA-39083: Object type REF_CONSTRAINT failed to create with error:  
  26. ORA-00942: table or view does not exist  
  27. Failing sql is:  
  28. ALTER TABLE "HR"."DEPARTMENTS" ADD CONSTRAINT "DEPT_LOC_FK" FOREIGN KEY ("LOCATION_ID"REFERENCES "HR"."LOCATIONS" ("LOCATION_ID") ENABLE  
  29. Processing object type TABLE_EXPORT/TABLE/TRIGGER  
  30. ORA-39082: Object type TRIGGER:"HR"."SECURE_EMPLOYEES" created with compilation warnings  
  31. ORA-39082: Object type TRIGGER:"HR"."SECURE_EMPLOYEES" created with compilation warnings  
  32. ORA-39082: Object type TRIGGER:"HR"."UPDATE_JOB_HISTORY" created with compilation warnings  
  33. ORA-39082: Object type TRIGGER:"HR"."UPDATE_JOB_HISTORY" created with compilation warnings  
  34. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  35. Job "SYS"."SYS_IMPORT_FULL_01" completed with 6 error(s) at Mon Jul 4 22:16:07 2016 elapsed 0 00:00:03  
从报错信息看,是导入完数据后,创建外键约束和触发器失败。当然,数据已经完全导入。

[sql]  view plain  copy
  1. SQL> select table_name from user_tables;  
  2.   
  3. TABLE_NAME  
  4. ------------------------------  
  5. DEPARTMENTS  
  6. EMPLOYEES  
  7.   
  8. SQL> select count(*) from DEPARTMENTS;  
  9.   
  10.   COUNT(*)  
  11. ----------  
  12.         27  
  13.   
  14. SQL> select count(*) from EMPLOYEES;  
  15.   
  16.   COUNT(*)  
  17. ----------  
  18.        107  
  19.   
  20. SQL>   

此错误可以忽略,但对于强迫症来说,哦,不,对于一个处事风格严谨的人来说,不容许有一个错误出现,才叫完美,那么,接着走。

毫无疑问,上一步骤已导入数据,所以继续删除hr用户并重新创建。

[sql]  view plain  copy
  1. SQL> drop user hr cascade;  

重新创建用户

[sql]  view plain  copy
  1. SQL> create user hr identified by hr;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to hr;  
  6.   
  7. Grant succeeded.  
  8.   
  9. SQL>   

若要不报错,可以在导出命令中加一个参数exclude,过滤掉constraint(约束)、TRIGGER(触发器)、grant(赋权限)

注意,因为导出的单个表,难免会存在和其他未导出的表存在关联的情况,而这种做法并不是将错误无视并跳过,因为这些对数据导入完整性没有影响,都是在导入完成后进行的。我们可以避免在导出过程中报错,然后根据需要手动建立constraint(约束)、TRIGGER(触发器)和grant(赋权限)等操作,而多数情况下,对于单个表,后续并不需要操作这些的。

执行导入命令

[sql]  view plain  copy
  1. $ impdp \'/ as sysdba\' directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=imtab.log tables=hr.employees,hr.departments exclude=constraint,trigger,grant  
[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Tue Jul 5 00:36:29 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_TABLE_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=tab.dmp tables=hr.employees,hr.departments exclude=constraint,trigger,grant   
  9. Processing object type TABLE_EXPORT/TABLE/TABLE  
  10. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  11. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  12. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  13. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  14. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  15. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  16. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  17. Job "SYS"."SYS_IMPORT_TABLE_01" successfully completed at Tue Jul 5 00:36:33 2016 elapsed 0 00:00:03  
7.2.2>导入表,改变schema

创建一个新的正经的用户zhengjing

[sql]  view plain  copy
  1. SQL> create user zhengjing identified by zhengjing;  
  2.   
  3.   
  4. User created.  
  5.   
  6. SQL> grant connect,resource to zhengjing;  
  7.   
  8.   
  9. Grant succeeded.  
  10.   
  11. SQL>   
执行导入命令,注意用参数REMAP_SCHEMA改变schema,参数exclude的使用同上一步骤

[sql]  view plain  copy
  1. $ impdp \'/ as sysdba\' directory=DATA_PUMP_DIR dumpfile=tab.dmp logfile=imtab.log REMAP_SCHEMA=hr:zhengjing exclude=constraint,trigger,grant  
[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Tue Jul 5 01:00:13 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_FULL_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=tab.dmp REMAP_SCHEMA=hr:zhengjing exclude=constraint,trigger,grant   
  9. Processing object type TABLE_EXPORT/TABLE/TABLE  
  10. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  11. . . imported "ZHENGJING"."DEPARTMENTS"                   7.007 KB      27 rows  
  12. . . imported "ZHENGJING"."EMPLOYEES"                     16.80 KB     107 rows  
  13. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  14. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  15. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  16. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  17. Job "SYS"."SYS_IMPORT_FULL_01" successfully completed at Tue Jul 5 01:00:17 2016 elapsed 0 00:00:03  

7.3>导入表空间

对应步骤3.4的按表空间导出文件

首先在目标端查看有哪些表在使用example表空间以及其所属用户

[sql]  view plain  copy
  1. SQL> set lines 200  
  2. SQL> select dt.owner, dt.table_name, dt.tablespace_name, dt.status  
  3.   2    from dba_tables dt  
  4.   3   where lower(dt.tablespace_name) = 'example';  
  5.   
  6. OWNER                          TABLE_NAME                     TABLESPACE_NAME                STATUS  
  7. ------------------------------ ------------------------------ ------------------------------ --------  
  8. BUZHENGJING                    DEPARTMENTS                    EXAMPLE                        VALID  
  9. BUZHENGJING                    REGIONS                        EXAMPLE                        VALID  
  10. BUZHENGJING                    LOCATIONS                      EXAMPLE                        VALID  
  11. BUZHENGJING                    JOBS                           EXAMPLE                        VALID  
  12. BUZHENGJING                    EMPLOYEES                      EXAMPLE                        VALID  
  13. BUZHENGJING                    JOB_HISTORY                    EXAMPLE                        VALID  
  14. HR                             EMPLOYEES                      EXAMPLE                        VALID  
  15. HR                             DEPARTMENTS                    EXAMPLE                        VALID  
  16. HR                             JOB_HISTORY                    EXAMPLE                        VALID  
  17. HR                             JOBS                           EXAMPLE                        VALID  
  18. HR                             LOCATIONS                      EXAMPLE                        VALID  
  19.   
  20. OWNER                          TABLE_NAME                     TABLESPACE_NAME                STATUS  
  21. ------------------------------ ------------------------------ ------------------------------ --------  
  22. HR                             REGIONS                        EXAMPLE                        VALID  
  23. ZHENGJING                      EMPLOYEES                      EXAMPLE                        VALID  
  24. ZHENGJING                      DEPARTMENTS                    EXAMPLE                        VALID  
  25.   
  26. 14 rows selected.  
  27.   
  28. SQL>   
因之前演示中创建了许多用户,为避免上述导入的数据干扰产生嫌疑,此处删除这几个用户,当然用户下对应的表也将跟着一起被干掉。

[sql]  view plain  copy
  1. SQL> drop user BUZHENGJING cascade;  
  2. drop user HR cascade;  
  3. drop user ZHENGJING cascade;  
  4.   
  5. User dropped.  
  6.   
  7. SQL>   
  8. User dropped.  
  9.   
  10. SQL>   
  11. User dropped.  
源库的example下存在hr用户,因而,目标库对应的hr用户也要存在。

[sql]  view plain  copy
  1. SQL> create user hr identified by hr;  
  2.   
  3. User created.  
  4.   
  5. SQL> grant connect,resource to hr;  
  6.   
  7. Grant succeeded.  
执行导入命令

[sql]  view plain  copy
  1. $ impdp \'/ as sysdba\' directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tsb.log TABLESPACES=example  
执行结果如下

[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Tue Jul 5 01:36:23 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_TABLESPACE_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_TABLESPACE_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tsb.log TABLESPACES=example   
  9. Processing object type TABLE_EXPORT/TABLE/TABLE  
  10. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  11. . . imported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  12. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  13. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  14. . . imported "HR"."JOBS"                                 6.992 KB      19 rows  
  15. . . imported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  16. . . imported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  17. . . imported "HR"."REGIONS"                              5.476 KB       4 rows  
  18. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  19. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  20. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  21. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  22. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  23. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  24. Processing object type TABLE_EXPORT/TABLE/TRIGGER  
  25. ORA-39082: Object type TRIGGER:"HR"."SECURE_EMPLOYEES" created with compilation warnings  
  26. ORA-39082: Object type TRIGGER:"HR"."SECURE_EMPLOYEES" created with compilation warnings  
  27. ORA-39082: Object type TRIGGER:"HR"."UPDATE_JOB_HISTORY" created with compilation warnings  
  28. ORA-39082: Object type TRIGGER:"HR"."UPDATE_JOB_HISTORY" created with compilation warnings  
  29. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  30. Job "SYS"."SYS_IMPORT_TABLESPACE_01" completed with 4 error(s) at Tue Jul 5 01:36:28 2016 elapsed 0 00:00:04  
查看报错信息,得知是由触发器的引起的,需要加上参数exclude=TRIGGER

在执行导入语句之前,清理并重建用户

[sql]  view plain  copy
  1. SQL> drop user hr cascade;  
  2.   
  3. User dropped.  
  4.   
  5. SQL> create user hr identified by hr;  
  6.   
  7. User created.  
  8.   
  9. SQL> grant connect,resource to hr;  
  10. Grant succeeded.  
  11. SQL>  

执行导入语句 
  
[sql]  view plain  copy
  1. $ impdp \'/ as sysdba\' directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tsb.log TABLESPACES=example exclude=TRIGGER  
执行结果如下

[sql]  view plain  copy
  1. Import: Release 11.2.0.4.0 - Production on Tue Jul 5 01:44:30 2016  
  2.   
  3. Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.  
  4.   
  5. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production  
  6. With the Partitioning, OLAP, Data Mining and Real Application Testing options  
  7. Master table "SYS"."SYS_IMPORT_TABLESPACE_01" successfully loaded/unloaded  
  8. Starting "SYS"."SYS_IMPORT_TABLESPACE_01":  "/******** AS SYSDBA" directory=DATA_PUMP_DIR dumpfile=tablespace.dmp logfile=tsb.log TABLESPACES=example exclude=TRIGGER   
  9. Processing object type TABLE_EXPORT/TABLE/TABLE  
  10. Processing object type TABLE_EXPORT/TABLE/TABLE_DATA  
  11. . . imported "HR"."COUNTRIES"                            6.367 KB      25 rows  
  12. . . imported "HR"."DEPARTMENTS"                          7.007 KB      27 rows  
  13. . . imported "HR"."EMPLOYEES"                            16.80 KB     107 rows  
  14. . . imported "HR"."JOBS"                                 6.992 KB      19 rows  
  15. . . imported "HR"."JOB_HISTORY"                          7.054 KB      10 rows  
  16. . . imported "HR"."LOCATIONS"                            8.273 KB      23 rows  
  17. . . imported "HR"."REGIONS"                              5.476 KB       4 rows  
  18. Processing object type TABLE_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANT  
  19. Processing object type TABLE_EXPORT/TABLE/COMMENT  
  20. Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX  
  21. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT  
  22. Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS  
  23. Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT  
  24. Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS  
  25. Job "SYS"."SYS_IMPORT_TABLESPACE_01" successfully completed at Tue Jul 5 01:44:34 2016 elapsed 0 00:00:04  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值