If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to constraint sequence number in generated identity columns without missing value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-11, 08:40
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to constraint sequence number in generated identity columns without missing value

Hi,
using DB2 v9.5 fixpack 2a on Linux I have created a table with "generated always as identity" column.

Code:
CREATE TABLE ADMIN.TAB
(
ID INT NOT NULL GENERATED ALWAYS AS IDENTITY
   (START WITH 1,
   INCREMENT BY 1,
   NO CACHE),
COL2 INT NOT NULL UNIQUE
);
Above table also has a unique value in col2, to easily demonstrate the problem.
Code:
INSERT INTO ADMIN.TAB (COL2) VALUES (1);
INSERT INTO ADMIN.TAB (COL2) VALUES (1);
INSERT INTO ADMIN.TAB (COL2) VALUES (2);
Second insert fails because of unique constraint.

Looking at the table with select:
Code:
SELECT * FROM ADMIN.TAB;
and I see in first ID column the following numbers: 1 and 3.
Code:
ID          COL2
----------- -----------
          1           1
          3           2
I have expected to see 1 and 2 value, but 2 is missing and 3 is inserted. How to prevent missing values (like 2) in ID column?
Why does DB2 increases sequence number if SQL fails?

Thanks

Last edited by grofaty; 11-09-11 at 08:51.
Reply With Quote
  #2 (permalink)  
Old 11-09-11, 09:08
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
DB2 does not rollback increments to sequences or identity columns (which are implemented with sequences behind the scenes) when an associated SQL fails. This is for performance reasons, since DB2 does not want to hold a lock on that sequence for the duration of your transaction. You would have to implement your own "next_available_number" table to have it fall under transaction control.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 11-09-11, 12:34
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
As Marcus mentioned, rolled back transactions may leave gaps. It can also occur that identity values are cached in bigger chunks and that not all numbers in a chunk are used. That could also cause gaps.

What's the problem with the gap? If you can leave with it, it would be the best
approach. If you have a hard requirement (i.e. legal requirements) that no gaps must exist, you can't use sequences and identity columns.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On