Oracle 9 - 重置序列以匹配表的状态
我有一个序列用于在 oracle 表中播种我的(基于整数的)主键。
看来此序列并不总是用于将新值插入表中。如何使序列与表中的实际值保持一致?
I have a sequence used to seed my (Integer based) primary keys in an oracle table.
It appears this sequence has not always been used to insert new values into the table. How do I get the sequence back in step with the actual values in the table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果 ID 是 PK 列的名称,PK_SEQ 是序列的名称:
通过以下方式查找最高 PK 的值
SELECT MAX(ID) FROM tableName
通过以下方式查找下一个 PK_SEQ 的值
SELECT PK_SEQ.NEXTVAL FROM DUAL
完成,假设你对待这些
值作为真正的代理键
通过 ALTER SEQUENCE 跳转到最大 ID
PK_SEQ 增量 [#1 值 - #2
value]
通过 SELECT 改变序列
PK_SEQ.NEXTVAL FROM DUAL
重置序列增量值
通过 ALTER SEQUENCE PK_SEQ 变为 1
INCRMENT BY 1
这一切都假设您在执行此操作时没有向表中插入新内容...
If ID is the name of your PK column and PK_SEQ is the name of your sequence:
Find the value of the highest PK by
SELECT MAX(ID) FROM tableName
Find the value of the next PK_SEQ by
SELECT PK_SEQ.NEXTVAL FROM DUAL
done, assuming you treat these
values as true surrogate keys
jump to the max ID by ALTER SEQUENCE
PK_SEQ INCREMENT BY [#1 value - #2
value]
Bump the sequence by SELECT
PK_SEQ.NEXTVAL FROM DUAL
Reset the sequence increment value
to 1 by ALTER SEQUENCE PK_SEQ
INCREMENT BY 1
This all assumes that you don't have new inserts into the table while you're doing this...
简而言之,进行游戏:
您可以获得表中使用的最大序列值,进行数学计算,并相应地更新序列。
In short, game it:
You can get the max sequence value used within your table, do the math, and update the sequence accordingly.
我编写了这个脚本,因为我没有在网上找到可以将我的所有序列动态设置为当前最高 ID 的脚本。在 Oracle 11.2.0.4 上测试。
I made this script as I did not find a script online that dynamically sets all my sequences to the current highest ID. Tested on Oracle 11.2.0.4.
在某些情况下,您可能会发现简单地获取当前最大值更容易,然后
应用程序将在您执行删除操作后损坏。但这将阻止任何人在此期间插入行,并且创建序列很快。确保在序列上重新创建任何授权,因为序列创建时这些授权将被删除。并且您可能需要手动重新编译任何依赖于序列的 plsql。
In some cases, you may find it easier to simply get the current max value and then
The app will be broken after you do the drop. But that will keep anybody from inserting rows during that period, and creating a sequence is quick. Make sure you recreate any grants on the sequence since those will be dropped when the sequence is. And you may want to manually recompile any plsql that depends on the sequence.
添加到 https://stackoverflow.com/a/15929548/1737973,但不求助于
SEQUENCENAME。 NEXTVAL
因此不会导致一个位置超过它应该是:免责声明:如果序列使用缓存(
all_sequences.cache_size
设置为大于 0),您可能想要采取在计算差异步骤中考虑它。所有序列
的 Oracle 文档...。Adding up to https://stackoverflow.com/a/15929548/1737973, but without resorting to
SEQUENCENAME.NEXTVAL
hence not resulting in one position over it should be:Disclaimer: if the sequence is using caching (
all_sequences.cache_size
set to bigger than 0) you are probably wanting to take it into consideration in the Compute the difference step.Oracle documentation for
all sequences
....SELECT setval( 'table_id_seq_name', (SELECT MAX(id) FROM table_name ) );
SELECT setval( 'table_id_seq_name', (SELECT MAX(id) FROM table_name ) );