English 中文(简体)
Informix: What happens when you delete a row with serial/serial8 primary key?
原标题:
  • 时间:2009-11-30 10:37:03
  •  标签:
  • sql
  • informix

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?

最佳回答

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.

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签