Informix:当您删除具有serial/serial8 主键的行时会发生什么?

发布于 2024-08-13 03:34:18 字数 166 浏览 7 评论 0原文

我有一个关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为一人 2024-08-20 03:34:18

SERIAL、SERIAL8 或 BIGSERIAL 使用的计数器单调递增,直至回绕。删除的值只是被删除。如果插入的文字值大于计数器,则计数器会进行调整,以便下一个插入的值大一:

CREATE TABLE s (s SERIAL(2) NOT NULL PRIMARY KEY, v VARCHAR(20) NOT NULL);

INSERT INTO s(s,v) VALUES(0, "Row 2");
INSERT INTO s(s,v) VALUES(0, "Row 3");
INSERT INTO s(s,v) VALUES(0, "Row 4");
INSERT INTO s(s,v) VALUES(0, "Row 5");
DELETE FROM s WHERE s = 3;
INSERT INTO s(s,v) VALUES(0, "Row 6");
INSERT INTO s(s,v) VALUES(8, "Row 8"); -- Skip 7
INSERT INTO s(s,v) VALUES(0, "Row 9");

SELECT * FROM s ORDER BY s;

这将生成结果:

          2     Row 2
          4     Row 4
          5     Row 5
          6     Row 6
          8     Row 8
          9     Row 9

所有类型的行为都类似。如果达到最大值(SERIAL 为 2^32-1,SERIAL8 和 BIGSERIAL 为 2^63-1),则计数器将回滚到零,但您可能会遇到未空出的空间被重用且主键拒绝的问题重复的行。一般来说,避免包裹它们。 (使它们换行需要相当长的时间,尤其是 64 位计数器。)

请注意,您可以手动插入“缺失”值 - 例如 3 或 7。但是,IDS 不会为您执行此操作。

@iQ 问道:

那么 Informix 在回绕时会自动重新使用未使用或删除的序列值吗?

并不真地。该值回绕到 1;如果值为1的行存在,则插入失败;如果没有,则成功;无论哪种方式,下一次尝试都会尝试 2。为了说明这一点,继续上一个示例停止的地方:

INSERT INTO s(s,v) VALUES(2147483647, "Row 2,147,483,647");
INSERT INTO s(s,v) VALUES(0, "Row next")     { 1 - Pass };
INSERT INTO s(s,v) VALUES(0, "Row next + 1") { 2 - Fail };
INSERT INTO s(s,v) VALUES(0, "Row next + 2") { 3 - Pass };
INSERT INTO s(s,v) VALUES(0, "Row next + 3") { 4 - Fail };
SELECT * FROM s ORDER BY s;

最终结果是:

          1     Row next            
          2     Row 2               
          3     Row next + 2        
          4     Row 4               
          5     Row 5               
          6     Row 6               
          8     Row 8               
          9     Row 9               
 2147483647     Row 2,147,483,647   

显然,接下来的三个插入将失败,一个会成功,另外两个会失败,然后它们会成功接下来的几十亿个插入。

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:

CREATE TABLE s (s SERIAL(2) NOT NULL PRIMARY KEY, v VARCHAR(20) NOT NULL);

INSERT INTO s(s,v) VALUES(0, "Row 2");
INSERT INTO s(s,v) VALUES(0, "Row 3");
INSERT INTO s(s,v) VALUES(0, "Row 4");
INSERT INTO s(s,v) VALUES(0, "Row 5");
DELETE FROM s WHERE s = 3;
INSERT INTO s(s,v) VALUES(0, "Row 6");
INSERT INTO s(s,v) VALUES(8, "Row 8"); -- Skip 7
INSERT INTO s(s,v) VALUES(0, "Row 9");

SELECT * FROM s ORDER BY s;

This generates the results:

          2     Row 2
          4     Row 4
          5     Row 5
          6     Row 6
          8     Row 8
          9     Row 9

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:

So does Informix automatically re-use the unused or deleted serial values when it wraps around?

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:

INSERT INTO s(s,v) VALUES(2147483647, "Row 2,147,483,647");
INSERT INTO s(s,v) VALUES(0, "Row next")     { 1 - Pass };
INSERT INTO s(s,v) VALUES(0, "Row next + 1") { 2 - Fail };
INSERT INTO s(s,v) VALUES(0, "Row next + 2") { 3 - Pass };
INSERT INTO s(s,v) VALUES(0, "Row next + 3") { 4 - Fail };
SELECT * FROM s ORDER BY s;

The end result is:

          1     Row next            
          2     Row 2               
          3     Row next + 2        
          4     Row 4               
          5     Row 5               
          6     Row 6               
          8     Row 8               
          9     Row 9               
 2147483647     Row 2,147,483,647   

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文