sequence有关问题
sequence问题
比如主键是 1,3,5,7,9,11 中间跳号了。。。
用什么方法可以把主键重新排列为 1,2,3,4,5
------解决方案--------------------
update tablename set id=rownum
通过下面的方法重置你得序列修改那个start with值
SQL> create sequence seq_1 increment by 1 start with 1 maxvalue 999999999;
序列已创建。
SQL> create or replace procedure seq_reset(v_seqname varchar2) as
2 n number(10);
3 tsql varchar2(100);
4 begin
5 execute immediate 'select '||v_seqname||'.nextval from dual' into n;
6 n:=-(n-1);
7 tsql:='alter sequence '||v_seqname||' increment by '|| n;--让序列一次递增-N,实现归0
8 execute immediate tsql;
9 execute immediate 'select '||v_seqname||'.nextval from dual' into n;
10 tsql:='alter sequence '||v_seqname||' increment by 1';
11 execute immediate tsql;
12 end seq_reset;
13 /
过程已创建。
SQL> select seq_1.nextval from dual;
NEXTVAL
---------
2
SQL> /
NEXTVAL
---------
3
SQL> /
NEXTVAL
---------
4
SQL> /
NEXTVAL
---------
5
SQL> exec seq_reset('seq_1');
PL/SQL 过程已成功完成。
SQL> select seq_1.currval from dual;
CURRVAL
---------
1
SQL>
------解决方案--------------------
第一种方法:
这种方法可以实现你的要求,但要分两步做:
(1)创建一个序列:
create sequence minus_value
start with 1
minvalue 1
increment by 1;
(2)更新数据库列
update yourtable set colnum=minus_value.nextval;
如果是一个刚刚新创建的序列,则执行上面的更新语句后,可以保证主键顺序是从1开始的.
如果你所用的序列是一个已经被用过的,则还要执行一个更新语句,
update yourtable set colnum=colnum - <value>;
这个value就是序列的起始值与1的差值.
第二种方法:
可以用ROWNUM与主键值之间的差值实现
sequence有关问题的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- (AOP)理解
AOP的全称: Aspact Oriented Programming AOP的目标(作用):让我们可以“专心做事” 日志记录,事务处理,异常捕获,缓存操作. AOP原理 将复杂的需求分解出不同 ...
- 对于HDMI设备连接状态的监听
对与最近主要做的是电视机盒子端的开发,其中涉及到设备的状态监听比较繁琐,所以对HDMI的连接状态的监听方法做个记录,方便后续查看. 主要通过两种方式: (1)比较常用的广播监听 注册一个动态广播来获取 ...
- xp密钥
Windows XP 专业版 : CCC64-69Q48-Y3KWW-8V9GV-TVKRM
- apache安装报错
libtool: install: error: cannot install `libaprutil-1.la' to a directory not ending /some_directory ...
- c#将本地文件上传至服务器(内网)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Python 字符编码问题的处理
python中的字符编码问题往往是初学者容易弄不明白的问题, 要想将这个问题搞清楚,需要先弄明白以下的概念 decode 和 encode 函数的作用 字符串字面量的编码格式 decode(str) ...
- ubuntu 14.04安装 nginx直播服务平台
在官网上下载nginx,可以选中直接从ubuntu的源红直接安装:sudo apt-get install nginx.还有就是源码编译安装,我选择的是源码编译安装.具体的步骤如下: ll /usr/ ...
- 数组初始化 和 vector初始化
] = {}; 整个数组都初始化为0 vector<); 整个vector初始化为1 如果你定义的vector是这样定义的: vector<int> B; 去初始化,千万不要用: ; ...
- 判断一个链表是否为回文结构 【题目】 给定一个链表的头节点head,请判断该链表是否为回 文结构。 例如: 1->2->1,返回true。 1->2->2->1,返回true。 15->6->15,返回true。 1->2->3,返回false。 进阶: 如果链表长度为N,时间复杂度达到O(N),额外空间复杂 度达到O(1)。
方式1:借助栈 空间辅助度是O(N) 方式2: 借助栈 空间复杂度是 O(n/2).只存后半个链表 方式3: 反转后半个链表 最后再反转回来 package my_basic.class_3; im ...
- Django-C002-深入模型,到底有多深
此文章完成度[100%]留着以后忘记的回顾.多写多练多思考,我会努力写出有意思的demo,如果知识点有错误.误导,欢迎大家在评论处写下你的感想或者纠错. ORM介绍:对象关系映射(英语:(Object ...