Cleaning Up Oracle Sequences
I've worked extensively with SQL Server but have very little experience with Oracle. I've been given the task of "cleaning up" the Sequences in an Oracle database and am not sure how to go about this safely.
I need to determine the maximum value actually in the table (say the ID = 105). Then look and see what the next sequence for that ID is. If it is 106, then all would be good. If it were 110, then I need to reset it back to 106.
Can I safely drop a sequence then recreate it or will t开发者_开发百科hat muck up the already existing Primary Key? I'm guessing this wouldn't be a problem but before I jacked up someone else's system, I wanted to ask.
This is the command I am going to use
Drop Sequence blah.foo_seq
Create Sequence blah.foo_seq Start WIth 106 Min 1 Max 2147483647 yada yada
I'd generally be wary about the need to "clean up" an Oracle sequence. Since Oracle sequences cannot be used to generate gap-free values, if 110 would cause a problem for the application, there are bigger problems that need to be addressed.
Dropping a sequence has no impact on a primary key. But it does invalidate any objects that reference the sequence and remove any privilege grants. You can recompile the code after you've re-created the sequence, it's the dropping of the privileges that would be potentially problematic.
An alternative approach that avoids the need to deal with lost privileges would be to change the INCREMENT BY parameter to decrease the sequence value, i.e.
ALTER SEQUENCE foo_seq
INCREMENT BY -4;
SELECT foo_seq.nextval
FROM dual;
ALTER SEQUENCE foo_seq
INCREMENT BY 1;
精彩评论