数据泵对已经存在的表加载索引 imp 和impdp

简介: 这里探讨了使用imp 和impdp对含有索引的表的导入的一点差别。 使用imp可以在表存在的情况下,不删除表,且导入表的数据和索引。 1)创建实验表cust(已存在) SQL> conn scott/yang已连接。

这里探讨了使用imp 和impdp对含有索引的表的导入的一点差别。

使用imp可以在表存在的情况下,不删除表,且导入表的数据和索引。

1)创建实验表cust(已存在)

SQL> conn scott/yang
已连接。
SQL> select * from cust;

        ID CUTNAME                                                             
---------- ----------                                                          
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5 yangyi                                                              
         6 xiaonan                                                             
已选择6行。
SQL> create index indcust_id on cust(id);

索引已创建。
2)导出表

HOST exp scott/yang file=cust.dmp   tables=cust

连接到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已导出 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集

即将导出指定的表通过常规路径...
. . 正在导出表                            CUST导出了           6 行
成功终止导出, 没有出现警告。

SQL> drop index indcust_id;
索引已删除

3)使用imp导入表

SQL> HOST imp scott/yang file=cust.dmp    tables=cust  ignore=y

连接到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

经由常规路径由 EXPORT:V11.01.00 创建的导出文件
已经完成 ZHS16GBK 字符集和 AL16UTF16 NCHAR 字符集中的导入
. 正在将 SCOTT 的对象导入到 SCOTT
. 正在将 SCOTT 的对象导入到 SCOTT
. . 正在导入表                          "CUST"导入了           6 行
成功终止导入, 没有出现警告。
SQL> select * from cust;

        ID CUTNAME                                                             
---------- ----------                                                          
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5  yangyi                                                              
         6 xiaonan                                                             
         1 JANE                                                                
         2 Jone                                                                
         3 TOM                                                                 
         4 yang                                                                 
         5 yangyi                                                                                                                      
         6 xiaonan                                                             
已选择12行。
SQL> select index_name from user_indexes
  2  where table_name='CUST';

INDEX_NAME                                                                     
------------------------------
                                                 
INDCUST_ID          

4)impdp的默认工作并非如此,监测到表存在时,impdp会跳过索引的创建                                                        

SQL> create table texp(id number,name varchar2(30));

表已创建。

SQL> insert into texp
  2  select rownum,tname
  3  from tab;
已创建9行。
SQL> commit;
提交完成。
SQL> create index indtexp_id on texp (id);
索引已创建。

5)导出表

expdp scott/yang  directory=dump dumpfile=scottexp.dp tables=texp

连接到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
启动 "SCOTT"."SYS_EXPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp
正在使用 BLOCKS 方法进行估计...
处理对象类型 TABLE_EXPORT/TABLE/TABLE_DATA
使用 BLOCKS 方法的总估计: 64 KB
处理对象类型 TABLE_EXPORT/TABLE/TABLE
处理对象类型 TABLE_EXPORT/TABLE/INDEX/INDEX
处理对象类型 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
. . 导出了 "SCOTT"."TEXP"                              5.507 KB       9 行
已成功加载/卸载了主表 "SCOTT"."SYS_EXPORT_TABLE_01"
******************************************************************************
SCOTT.SYS_EXPORT_TABLE_01 的转储文件集为:
  F:\DUMP\SCOTTTEXP.DMP
作业 "SCOTT"."SYS_EXPORT_TABLE_01" 已于 23:29:38 成功完成


清除数据,并删除索引:
SQL> drop index indtexp_id;
索引已删除。
SQL> truncate table texp;
表被截断。

6)然后再导入表

impdp scott/yang  directory=dump dumpfile=scottexp.dp tables=texp
SQL> select count(*) from texp;

  COUNT(*)                                                                     
----------                                                                     
         9                                                                     
SQL> select index_name from user_indexes
  2  where table_name='TEXP';

未选定行

数据虽然导入了,但是索引没有创建。不过要解决这个问题也很简单,通过INCLUDE就可以解决这个问题:
SQL> truncate table texp;
表被截断。
impdp scott/yang directory=dump  dumpfile=scottexp.dp tables=texp table_exists_action=truncate include=index    

 连接到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已成功加载/卸载了主表 "SCOTT"."SYS_IMPORT_TABLE_01"
启动 "SCOTT"."SYS_IMPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index
处理对象类型 TABLE_EXPORT/TABLE/INDEX/INDEX
处理对象类型 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
作业 "SCOTT"."SYS_IMPORT_TABLE_01" 已于 23:35:39 成功完成

SQL> select count(*) from texp;
  COUNT(*)                                                                     
----------                                                                     
         0                                                                
SQL> select index_name from user_indexes
  2  where table_name='TEXP';
INDEX_NAME                                                                     
------------------------------
                                                 
INDTEXP_ID    

 SQL> drop index indtexp_id;
索引已删除。

impdp  scott/yangdirectory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index include=table_data

连接到: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
已成功加载/卸载了主表 "SCOTT"."SYS_IMPORT_TABLE_01"
启动 "SCOTT"."SYS_IMPORT_TABLE_01":  scott/******** directory=dump dumpfile=scotttexp.dmp tables=texp table_exists_action=truncate include=index include=table_data
处理对象类型 TABLE_EXPORT/TABLE/TABLE_DATA
. . 导入了 "SCOTT"."TEXP"                              5.507 KB       9 行
处理对象类型 TABLE_EXPORT/TABLE/INDEX/INDEX
处理对象类型 TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
作业 "SCOTT"."SYS_IMPORT_TABLE_01" 已于 23:37:57 成功完成

最后检查一下是否成功
SQL> select count(*) from texp;
  COUNT(*)                                                                     
----------                                                                     
         9                                                                     
SQL> select index_name from user_indexes
  2  where table_name='TEXP';
INDEX_NAME                                                                     
------------------------------                                                 
INDTEXP_ID
                                                                     

 

相关实践学习
数据库实验室挑战任务-初级任务
本场景介绍如何开通属于你的免费云数据库,在RDS-MySQL中完成对学生成绩的详情查询,执行指定类型SQL。
阿里云云原生数据仓库AnalyticDB MySQL版 使用教程
云原生数据仓库AnalyticDB MySQL版是一种支持高并发低延时查询的新一代云原生数据仓库,高度兼容MySQL协议以及SQL:92、SQL:99、SQL:2003标准,可以对海量数据进行即时的多维分析透视和业务探索,快速构建企业云上数据仓库。 了解产品 https://www.aliyun.com/product/ApsaraDB/ads
目录
相关文章
|
5月前
Oracle11G用EXP导出时,空表不能导出解决
Oracle11G用EXP导出时,空表不能导出解决
|
SQL Oracle 关系型数据库
数据泵避免个别表数据的导出
对于数据泵EXPDP/IMPDP而言,功能比普通EXP/IMP功能要强的多,因此也可以实现一些普通导出导入工具很难完成的工作。     比如今天碰到的这个问题,要导出一些表,但是其中个别表只导出结构而不导出数据。
911 0
|
机器学习/深度学习 Oracle 关系型数据库
【expdp】10g数据泵expdp工具选项详解及应用示例
理解expdp各个选项的含义最好的途径就是逐一的进行测试,这样可以在感性上有一个真实的体验。 1.数据泵expdp导出工具与传统的exp导出工具的区别 1)exp是客户端程序,既可以在客户端使用,也可以在服务器端使用; 2)expdp是服务器端工具,只能在ORACLE服务器端使用,不能在客户端使用; 3)这两个工具生成的备份文件不能被对方与之对应的导入工具使用; 4)expdp在灵活性和功能性上与exp相比,有质上的飞跃。 2.expdp命令行选项列表 使用“-help”选项获得expdp命令可用的选项列表和简单的注释信息。 ora10g@linux5 /expdp$ expdp help
275 0
|
网络协议 Oracle 关系型数据库
|
关系型数据库 Oracle