How do I reset a sequence in Oracle?
Resetting a sequence in Oracle can be achieved using ALTER SEQUENCE
and a negative increment trick:
Replace currval
with the current maximum value of the sequence to be subtracted. This sets the next my_seq.NEXTVAL
to 100.
Handy alternatives
The standard approach is perfect for one-off resets, but what about when we need an automated, dynamic solution? We resort to a fabulous trick by the remarkable Tom Kyte:
And for the lucky ones running Oracle 18c and above, your life just got simpler:
Data integrity? Anyone?
Resetting sequences might seem fun, till it ruins your data. Pay heed to the repercussions of dropping/creating sequences on existing data.
Masters guide to sequence reset
Bulk reset
If all your sequences flew out of sync, here's how to get them back in line:
Tricky problems
Quick reminder, resetting sequences can stir pandemonium in your applications. Be wary of sequence caches and race conditions when executing concurrent operations.
Chasing unicorns
Refrain from the allure of undocumented features, akin to chasing unicorns. They're not guaranteed and can cause stability issues.
Was this article helpful?