今天同事提出了一个问题:

使用数据泵expdp导出1个schema,有个表主键是触发器自增的id,导入测试库测试时,发现表里的数据比自增序列的值要大。导致插入数据报错。

最终结论是:

由于数据库先进行序列导出,然后再进行表数据导出。然后在导出的过程中,该表一直有插入操作,最终导致了这种差异。

解决方法:

重建触发器中的序列,让序列的开始值为表主键最大值+1。

下面我构造实验完整演示下这种场景。

1.准备测试环境

需要建立测试表,序列,触发器和模拟业务插入数据的存储过程。
以下是实际的创建语句:

--在测试用户jingyu下创建测试表book2
drop table book2 purge;
create table book2(
bookId number(10) primary key,
name varchar2(20)
); --创建序列
drop sequence book2_seq;
create sequence book2_seq start with 1 increment by 1; --创建触发器
create or replace trigger book2_trigger
before insert on book2
for each row
begin
select book2_seq.nextval into :new.bookId from dual;
end ;
/ --创建实现循环添加数据的存储过程 /*
--存储过程中使用需要显示赋权
grant execute on dbms_lock to jingyu;
*/ create or replace procedure proc_insert_book2 is
begin
loop
insert into book2(name) values ('xx');
commit;
dbms_lock.sleep(1);
end loop;
end;
/

2.开始模拟该表不断插入

由于我这里实际使用的是死循环,所以只要开始执行存储过程,每秒都会向测试表插入1条测试数据,直到手工停止。

--执行该存储过程
exec proc_insert_book2; --查询表的数量,确认是每秒多一条数据
select count(*) from book2;

3.进行数据泵导出操作

确认导出目录,编写expdp导出语句,最终将jingyu这个schema导出。实际命令如下:

--expdp 导出
create or replace directory jy as '/opt/app/orabak/';
expdp jingyu/jingyu directory=jy dumpfile=jingyu.dmp schemas=jingyu

实际执行导出的输出如下:

[oracle@jyrac1 orabak]$ expdp jingyu/jingyu directory=jy dumpfile=jingyu.dmp schemas=jingyu

Export: Release 11.2.0.4.0 - Production on Thu Jun 8 17:08:29 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
Starting "JINGYU"."SYS_EXPORT_SCHEMA_05": jingyu/******** directory=jy dumpfile=jingyu.dmp schemas=jingyu
Estimate in progress using BLOCKS method...
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 10.12 MB
Processing object type SCHEMA_EXPORT/USER
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
. . exported "JINGYU"."T2" 6.649 MB 100000 rows
. . exported "JINGYU"."SYS_EXPORT_SCHEMA_01" 142.0 KB 1195 rows
. . exported "JINGYU"."SYS_EXPORT_SCHEMA_02" 142.2 KB 1196 rows
. . exported "JINGYU"."SYS_EXPORT_SCHEMA_03" 142.6 KB 1198 rows
. . exported "JINGYU"."SYS_EXPORT_SCHEMA_04" 149.7 KB 1201 rows
. . exported "JINGYU"."T_OLD" 160.8 KB 20000 rows
. . exported "JINGYU"."T" 82.94 KB 10000 rows
. . exported "JINGYU"."T_NOLOG" 51.53 KB 5998 rows
. . exported "JINGYU"."BOOK" 5.421 KB 2 rows
. . exported "JINGYU"."BOOK2" 6.734 KB 123 rows
. . exported "JINGYU"."EMP" 8.562 KB 14 rows
. . exported "JINGYU"."T1" 11.75 KB 100 rows
Master table "JINGYU"."SYS_EXPORT_SCHEMA_05" successfully loaded/unloaded
******************************************************************************
Dump file set for JINGYU.SYS_EXPORT_SCHEMA_05 is:
/opt/app/orabak/jingyu.dmp
Job "JINGYU"."SYS_EXPORT_SCHEMA_05" successfully completed at Thu Jun 8 17:10:26 2017 elapsed 0 00:01:36

4.进行数据泵导入操作

将上一步的导出文件,导入到另一个新建的测试用户jingyu2下。实际命令如下:

--创建测试用户并赋予一定的权限
create user jingyu2 identified by jingyu2 default tablespace dbs_d_jingyu;
grant connect, resource to jingyu2; --impdp 导入到用户jingyu2
impdp jingyu/jingyu directory=jy dumpfile=jingyu.dmp REMAP_SCHEMA=jingyu:jingyu2

实际执行导入的输出如下:

[oracle@jyrac1 orabak]$ impdp jingyu/jingyu directory=jy dumpfile=jingyu.dmp REMAP_SCHEMA=jingyu:jingyu2

Import: Release 11.2.0.4.0 - Production on Thu Jun 8 17:11:21 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
Master table "JINGYU"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
Starting "JINGYU"."SYS_IMPORT_FULL_01": jingyu/******** directory=jy dumpfile=jingyu.dmp REMAP_SCHEMA=jingyu:jingyu2
Processing object type SCHEMA_EXPORT/USER
ORA-31684: Object type USER:"JINGYU2" already exists
Processing object type SCHEMA_EXPORT/SYSTEM_GRANT
Processing object type SCHEMA_EXPORT/ROLE_GRANT
Processing object type SCHEMA_EXPORT/DEFAULT_ROLE
Processing object type SCHEMA_EXPORT/TABLESPACE_QUOTA
Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA
Processing object type SCHEMA_EXPORT/SEQUENCE/SEQUENCE
Processing object type SCHEMA_EXPORT/TABLE/TABLE
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "JINGYU2"."T2" 6.649 MB 100000 rows
. . imported "JINGYU2"."SYS_EXPORT_SCHEMA_01" 142.0 KB 1195 rows
. . imported "JINGYU2"."SYS_EXPORT_SCHEMA_02" 142.2 KB 1196 rows
. . imported "JINGYU2"."SYS_EXPORT_SCHEMA_03" 142.6 KB 1198 rows
. . imported "JINGYU2"."SYS_EXPORT_SCHEMA_04" 149.7 KB 1201 rows
. . imported "JINGYU2"."T_OLD" 160.8 KB 20000 rows
. . imported "JINGYU2"."T" 82.94 KB 10000 rows
. . imported "JINGYU2"."T_NOLOG" 51.53 KB 5998 rows
. . imported "JINGYU2"."BOOK" 5.421 KB 2 rows
. . imported "JINGYU2"."BOOK2" 6.734 KB 123 rows
. . imported "JINGYU2"."EMP" 8.562 KB 14 rows
. . imported "JINGYU2"."T1" 11.75 KB 100 rows
Processing object type SCHEMA_EXPORT/TABLE/COMMENT
Processing object type SCHEMA_EXPORT/PROCEDURE/PROCEDURE
Processing object type SCHEMA_EXPORT/PROCEDURE/ALTER_PROCEDURE
ORA-39082: Object type ALTER_PROCEDURE:"JINGYU2"."PRO_SELECT" created with compilation warnings
ORA-39082: Object type ALTER_PROCEDURE:"JINGYU2"."PROC_INSERT_BOOK2" created with compilation warnings
Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
Processing object type SCHEMA_EXPORT/TABLE/TRIGGER
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "JINGYU"."SYS_IMPORT_FULL_01" completed with 3 error(s) at Thu Jun 8 17:11:52 2017 elapsed 0 00:00:26

导入完成,但存在一些警告,与本实验有关的只有"JINGYU2"."PROC_INSERT_BOOK2" 编辑警告需要处理,在下面的步骤中详细说明。

5.问题现象重现并解决

**问题现象重现:**
查询到表最大的BOOKID大于序列的当前值,具体情况如下:

SQL> select max(BOOKID) from book2;

MAX(BOOKID)
-----------
505 SQL> select book2_seq.currval from dual;
select book2_seq.currval from dual
*
ERROR at line 1:
ORA-08002: sequence BOOK2_SEQ.CURRVAL is not yet defined in this session SQL> select book2_seq.nextval from dual; NEXTVAL
----------
341

导入的存储过程存在编译警告的问题,排查原因是权限问题,需要先处理下:

--执行存储过程报错对象无效
SQL> exec proc_insert_book2
BEGIN proc_insert_book2; END; *
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object JINGYU2.PROC_INSERT_BOOK2 is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored --重新编译存储过程依然有错误
SQL> alter procedure proc_insert_book2 compile; Warning: Procedure altered with compilation errors. --显示具体的错误
SQL> show errors
Errors for PROCEDURE PROC_INSERT_BOOK2: LINE/COL ERROR
-------- -----------------------------------------------------------------
6/5 PL/SQL: Statement ignored
6/5 PLS-00201: identifier 'DBMS_LOCK' must be declared --根据错误提示,赋权解决
SQL> show user
USER is "SYS"
SQL> grant execute on dbms_lock to jingyu2; Grant succeeded. --再次编译成功
SQL> alter procedure proc_insert_book2 compile; Procedure altered.

编译存储过程成功后,执行它模拟插入数据,意料之中的会报错:

SQL> exec proc_insert_book2
BEGIN proc_insert_book2; END; *
ERROR at line 1:
ORA-00001: unique constraint (JINGYU2.SYS_C0011351) violated
ORA-06512: at "JINGYU2.PROC_INSERT_BOOK2", line 4
ORA-06512: at line 1 --查询测试表主键bookid的最大值
SQL> select max(bookid) from book2; MAX(BOOKID)
-----------
505

重新创建序列,序列开始值设置为MAX(BOOKID)+1,再次执行就可以正常插入了。

重新创建序列的语句如下:

--重新创建序列
drop sequence book2_seq;
create sequence book2_seq start with 506 increment by 1;

至此,整个实验完成。

实验:Oracle数据泵导出导入之序列问题的更多相关文章

  1. oracle数据泵导出导入

    先创建一个目录:比如 Create  or Replace directory  DATA_PUMP_DIR as 'D:\DataPipe';   然后给导入导出的用户赋权限: Grant read ...

  2. Oracle数据泵导出导入(expdp/impdp)

    一.创建表空间 create tablespace atp logging datafile 'D:\oracle\oradata\orcl\atp.dbf' size 50m autoextend ...

  3. 原创Oracle数据泵导出/导入(expdp/impdp)

    //创建目录 create Or Replace directory dpdata1 as 'd:\test\dump'; //赋予读写权限 grant read,write on directory ...

  4. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)

    <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)> <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)> 目的:指导项 ...

  5. Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)

    <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(上)> <Oracle简单常用的数据泵导出导入(expdp/impdp)命令举例(下)> 目的:指导项 ...

  6. Oracle基础 数据泵导出/导入Expdp/impdp(转)

    一.EXPDP和IMPDP使用说明 Oracle Database 10g引入了最新的数据泵(Data Dump)技术,数据泵导出导入(EXPDP和IMPDP)的作用 1)实现逻辑备份和逻辑恢复. 2 ...

  7. Oracle数据泵的导入和导出

    前言 今天王子要分享的内容是关于Oracle的一个实战内容,Oracle的数据泵. 网上有很多关于此的内容,但很多都是复制粘贴别人的,导致很多小伙伴想要使用的时候不能直接上手,所以这篇文章一定能让你更 ...

  8. 数据泵导出/导入Expdp/impdp

    一下转自 http://blog.csdn.net/jionjionyoushen/article/details/6789686 数据泵导出/导入Expdp/impdp Oracle 10g引入了D ...

  9. Oracle数据泵导出数据库

    Oracle数据泵导出数据库 特别注意:如果后续要导入的数据库版本低,所有导出命令就需要在后面加一个version=指定版本. 例如从11g导出数据导入到10g,假设10g具体版本为10.2.0.1, ...

随机推荐

  1. Java集合之Map和Set

    以前就知道Set和Map是java中的两种集合,Set代表集合元素无序.不可重复的集合:Map是代表一种由多个key-value对组成的集合.然后两个集合分别有增删改查的方法.然后就迷迷糊糊地用着.突 ...

  2. 在国内使用maven下载jar包非常慢的解决方法

    在国内使用maven下载jar包非常慢的解决方法 1.原因: 很多jar包在国外环境,所以会很慢. 2.解决方法 maven支持镜像环境下载,所以首先找到maven的conf目录中的settings. ...

  3. eNSP自学入门(基础)

    写了上篇博客之后,就立即投入到了eNSP的怀抱之中了,自己从零基础,入门到现在.也学了不少东西,在这里和大家分享一下. 说一下学习的过程吧,老师说做网络工程的课程设计用eNSP,关于这个软件什么都没有 ...

  4. 【山东省选2008】郁闷的小J 平衡树Treap

    小J是国家图书馆的一位图书管理员,他的工作是管理一个巨大的书架.虽然他很能吃苦耐劳,但是由于这个书架十分巨大,所以他的工作效率总是很低,以致他面临着被解雇的危险,这也正是他所郁闷的.具体说来,书架由N ...

  5. laytpl--前端数据绑定

    发现一枚前端数据绑定导弹:laytpl,官网:http://www.layui.com/laytpl/ 为了不用angularJS等较为重量级的,和繁琐的配置,所以就用了laytpl,可以配合JQ使用 ...

  6. EasyUI开发的驾校管理系统

    开源SmartLife驾校管理系统,地址:https://github.com/SmartOfLife/DriveMgr 1.界面布局是用的ymnets大神的界面,具体参考:http://www.cn ...

  7. sscanf( )函数初体验

    解析字符串,将%格式的内容,存储到后面的参数中 %% - 返回一个百分号 % %c - ASCII 值对应的字符 %d - 包含正负号的十进制数(负数.0.正数) %e - 使用小写的科学计数法(例如 ...

  8. Java之路 ——初识Eclipse

    零.大纲 一.前言 二.获取Eclipse 三.运行Eclipse 四.创建及运行第一个Java Project 五.界面介绍 六.如何调试 七.获取插件 八.Eclipse 快捷键 九.总结 一.前 ...

  9. SSH小结

    工作有一段时间了,经常用SSH登录远程机器,但对原理一直不是很了解,所以查阅了一些资料,写个小结. 一. SSH是什么? SSH的全称是Secure Shell, 是一种"用来在不安全的网络 ...

  10. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...