oracle怎么重置序列,如何把SEQUENCE重置?

本文介绍了如何在Oracle数据库中通过调整序列增量来重置序列值,避免使用drop和recreate导致依赖对象失效的问题。操作包括确定当前序列值,设置负增量并选择序列,然后恢复正增量,从而实现序列值的重置。需要注意的是,在此过程中如果其他用户在使用序列,可能会遇到错误,应当谨慎操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There is no "alter sequence reset" but you can, by playing with the increment

by, reset it.  Consider:

ops$tkyte@DEV816> create sequence seq;

Sequence created.

ops$tkyte@DEV816> select seq.nextval from emp;

NEXTVAL

----------

1

2

3

4

5

6

7

8

9

10

11

12

13

14

14 rows selected.

ops$tkyte@DEV816> column S new_val inc;

ops$tkyte@DEV816> select seq.nextval S from dual;

S

----------

15

ops$tkyte@DEV816> alter sequence seq increment by -&inc minvalue 0;

old   1: alter sequence seq increment by -&inc minvalue 0

new   1: alter sequence seq increment by -        15 minvalue 0

Sequence altered.

ops$tkyte@DEV816> select seq.nextval S from dual;

S

----------

0

ops$tkyte@DEV816> alter sequence seq increment by 1;

Sequence altered.

ops$tkyte@DEV816>

ops$tkyte@DEV816> select seq.nextval from dual;

NEXTVAL

----------

1

So, by finding out the current value of the sequence and altering the

increment by to be negative that number and selecting the sequence once -- we

can reset it.  Just beware that if others are using the sequence during this

time - they (or you) may get

ORA-08004: sequence SEQ.NEXTVAL goes below MINVALUE and cannot be instantiated

until you set the sequence increment back to +1.

This would be preferred to dropping and recreating the sequence which would

invalidate any dependent objects (such as triggers/stored procedures and so on)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值