Informix:当您删除具有serial/serial8 主键的行时会发生什么?
我有一个关于 informix 数据库主键上使用的串行数据类型的快速问题。
如果我删除一行,序列号会继续计数还是会针对已删除的任何行重新调整?
因此,如果当前行是序列号 5,我删除序列号为 3 的数字行,下一个值为 6 并继续进行吗?现在被删除的序列号3是不是就永远丢失了,不能再用了?
I had a quick question on serial data types used on primary key on informix db's.
If I delete a row, will the serial key carry on counting or will it re-adjust for any rows that were deleted?
So if current row is serial no 5, I delete number row withs serial no 3, will the next value be 6 and keep carrying on? Is serial no 3 that is now deleted forever lost not to be used again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SERIAL、SERIAL8 或 BIGSERIAL 使用的计数器单调递增,直至回绕。删除的值只是被删除。如果插入的文字值大于计数器,则计数器会进行调整,以便下一个插入的值大一:
这将生成结果:
所有类型的行为都类似。如果达到最大值(SERIAL 为 2^32-1,SERIAL8 和 BIGSERIAL 为 2^63-1),则计数器将回滚到零,但您可能会遇到未空出的空间被重用且主键拒绝的问题重复的行。一般来说,避免包裹它们。 (使它们换行需要相当长的时间,尤其是 64 位计数器。)
请注意,您可以手动插入“缺失”值 - 例如 3 或 7。但是,IDS 不会为您执行此操作。
@iQ 问道:
并不真地。该值回绕到 1;如果值为1的行存在,则插入失败;如果没有,则成功;无论哪种方式,下一次尝试都会尝试 2。为了说明这一点,继续上一个示例停止的地方:
最终结果是:
显然,接下来的三个插入将失败,一个会成功,另外两个会失败,然后它们会成功接下来的几十亿个插入。
The counter used by SERIAL, SERIAL8 or BIGSERIAL is monotonically increasing until it wraps around. Deleted values are simply deleted. If you insert a literal value that is larger than the counter, the counter is adjusted so that the next inserted value is one bigger:
This generates the results:
All the types behave similarly. If you reach the maximum (2^32-1 for SERIAL, 2^63-1 for SERIAL8 and BIGSERIAL), then the counter wraps back to zero, but you may run into problems with unvacated spaces being reused and the primary key rejected the duplicate rows. Generally, avoid wrapping them. (It takes quite a while to make them wrap, especially the 64-bit counters.)
Note that you can manually insert a 'missing' value - such as 3 or 7. However, IDS will not do that for you.
@iQ asked:
Not really. The value wraps back to 1; if the row with value 1 exists, the insert fails; if it doesn't, it succeeds; either way, the next attempt will try 2. To illustrate, continuing where the last example left off:
The end result is:
Clearly, the next three inserts would fail, one would succeed, two more would fail, and then they would succeed for the next couple of billion inserts.