Cleaning Up Oracle Sequences

你。 提交于 2019-12-30 11:09:45

问题


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 that 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

回答1:


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;


来源:https://stackoverflow.com/questions/4948325/cleaning-up-oracle-sequences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!