oracle导出序列的几种办法
oracle导出序列的几种办法
注:本文来源于《oracle导出序列的几种办法》
方法一:
-
select 'create sequence ' ||sequence_name||
-
' minvalue ' ||min_value||
-
' maxvalue ' ||max_value||
-
' start with ' ||last_number||
-
' increment by ' ||increment_by||
-
( case when cache_size= 0 then ' nocache' else ' cache ' ||cache_size end) || ';'
-
from dba_sequences where sequence_owner= 'JXDB' ;-- JXDB貌似要大写才行
执行此操作后会生成创建序列的语句
方法二:
select dbms_metadata.get_ddl('SEQUENCE',u.object_name) from user_objects u where object_type='SEQUENCE'
方法三:
用plsql,直接用鼠标操作,右边对象窗口,找到Sequences,在要导出的序列名称上右键--查看,然后点击右下角的查看sql,即可查看该序列的创建sql,然后复制即可
实验:Oracle数据泵导出导入之序列问题
注:本文来源于《实验:Oracle数据泵导出导入之序列问题》
使用数据泵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导出序列的几种办法的更多相关文章
- Oracle用户解锁的三种办法及默认的用户与密码
ORA-28000: the account is locked-的解决办法 2009-11-11 18:51 ORA-28000: the account is locked 第1步:使用PL/SQ ...
- Oracle导出/导入数据库的三种模式
导出 模式一:全量导出(慎用) exp 用户名/密码@数据库实例 owner=用户名 file=文件存储路径 log=日志存储路径 full=y 栗子:exp Mark/123456@151.2.*. ...
- “只有DBA才能导入由其他DBA导出的文件”各种解决办法
“只有DBA才能导入由其他DBA导出的文件”各种解决办法 当oracle导入的时候出现“只有 DBA 才能导入由其他 DBA 导出的文件”的时候通常有以下几种解决办法! 1:常见的是直接grant ...
- Oracle导出警告“EXP-00003: 未找到段 (0,0) 的存储定义”解决
环境:CentOS7.4 Oracle11.2.0.4(搭建rac集群) 问题描述:在使用exp命令执行导出的时候,部分表提示“EXP-00003: 未找到段 (0,0) 的存储定义”警告. 问题 ...
- Oracle导出excel
oracle导出excel(非csv)的方法有两种,1.使用sqlplus spool,2.使用包体 现将网上相关代码整理后贴出以备不时之需: 使用sqlplus: 使用sqlplus需要两个文件: ...
- oracle 导出导入常见问题
oracle 导入导出常见有两种方法 EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户 ...
- 查看Oracle执行计划的几种方法
查看Oracle执行计划的几种方法 一.通过PL/SQL Dev工具 1.直接File->New->Explain Plan Window,在窗口中执行sql可以查看计划结果.其中,Cos ...
- Oracle的常见错误及解决办法
ORA-12528: TNS:listener: all appropriate instances are blocking new connections ORA-12528问题是因为监听中的服务 ...
- oracle 导出导入数据
在window的运行中输出cmd,然后执行下面的一行代码, imp blmp/blmp@orcl full=y file=D:\blmp.dmp OK,问题解决.如果报找不到该blmp.dmp文件,就 ...
随机推荐
- 2017ICPC南宁 M题 The Maximum Unreachable Node Set【二分图】
题意: 找出不能相互访问的点集的集合的元素数量. 思路: 偏序集最长反链裸题. 代码: #include<iostream> #include<cstring> using n ...
- 第20月第4天 pycharm utf-8
1.运行python %run a.py 运行 https://blog.csdn.net/little_bobo/article/details/78982412 2.UnicodeDecodeEr ...
- Mysql-5.7.20-winx64绿色版安装步骤
Mysql-5.7.20-winx64绿色版安装步骤 1. 下载 mysql-5.7.20-winx64.zip 2.解压 解压到指定目录: C:\AppDate\mysql-5.7.20-winx6 ...
- java乱码解决方法
String name = request.getParameter("name"); 乱码解决:String name = new String(request.getParam ...
- excel数据有隐藏字符导致正则校验不通过
问题现象: 原因: 肉眼看不出任何问题,实际原因“有问题的”待校验字符串第一个单引号和第一个数字之间有个不可见字符 (注:Chrome控制台.常见编辑器定位光标 “Backspace退格删除”时,第一 ...
- window 安装gcc交叉编译器
参考网址: https://blog.csdn.net/zsy19881226/article/details/46952535
- mongodb 系列 ~ mongo的副本集(3)
一 简介:今天咱们来聊聊mongodb复制的具体一些案例 二 副本集 1 当mongodb采用全量复制时,如何观察全量复制的进度 对比文件本身和primary大小 2 mongodb全量复制的过程 旧 ...
- Tip: JSP开发模式
SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. JSP+JavaBean模式适合开发业务 ...
- 假设result 是一个float型变量,value是一个int型变量。执行以下赋值语句以后,变量value将是什么类型?为什么?
假设result 是一个float型变量,value是一个int型变量.执行以下赋值语句以后,变量value将是什么类型?为什么? 在执行这条语句的过程中,保存在vulue变量中的值被读取出来并转化为 ...
- awk基本用法
1 简介 awk实质是一种编程语言,基本作用在于查找和替换. 2 基本用法 有文本名称为:awk.txt 内容为: john.wang male 30 021-111111 lucy.yang f ...