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 > Duplicate error inserting default into identity column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-26-04, 06:53
martynhodgson martynhodgson is offline
Registered User
 
Join Date: Aug 2004
Posts: 4
Duplicate error inserting default into identity column

We have a table with a single unique index. The index is implicitly created by the primary key, which is on an integer identity column. Every so often an application gets the following error during an insert

ERROR [23505] [IBM][CLI Driver][DB2/LINUX] SQL0803N One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "1" constrains table "MYUSER.MYTABLE" from having duplicate rows for those columns. SQLSTATE=23505

The insert statement specifies DEFAULT for the identity column.

The application is not thrashing the system and the database server is very lightly loaded. On retrying the statement it generally works fine.
Reply With Quote
  #2 (permalink)  
Old 08-27-04, 06:35
vidyaraok vidyaraok is offline
Registered User
 
Join Date: Aug 2004
Posts: 15
if your Mytab table has structure:

(id -- identity column,
name )

use insert statement as

insert into mytab(name) values ('aaa')
Reply With Quote
  #3 (permalink)  
Old 08-27-04, 08:15
ApoPen ApoPen is offline
Registered User
 
Join Date: Jul 2004
Location: Ottawa, Canada
Posts: 58
Is your column GENERATED BY DEFAULT? That column type does not guarentee a unique number especially if there are gaps in the numbers...i.e deleted rows.
Reply With Quote
  #4 (permalink)  
Old 08-27-04, 10:19
martynhodgson martynhodgson is offline
Registered User
 
Join Date: Aug 2004
Posts: 4
The column is defined to allow us to insert specific values, but in fact we never do in this table (after the initial set up). Data is never deleted from the table in question.

My point is that the same insert statement sometimes fails, then simply retrying causes it to work. My initial thought was that two threads were hiting the table at the same instant. However, there is very little activity on the table and it keeps happening.

If there were a value in the table higher than the current identity column value, when we got to that value then the insert statement would always fail at that point.

I remain bemused!

Martyn
Reply With Quote
  #5 (permalink)  
Old 08-27-04, 12:06
venkat_dba venkat_dba is offline
Registered User
 
Join Date: Dec 2003
Posts: 19
Smile

Hi ,
If you specify GENERATED ALWAYS, this problem will not come, but the draw back would be u cant insert directly into the field with identity value. U need to do a load replace.

If u have replication, GENERATED ALWAYS will not work.

In GENERATED BY DEFAULT both inserts with values and replication will work. But my doubt is if u dont give default and do the insert, it would take the next value automatically. Please try this, remove the default and remove the identity column name.

It will start putting identity values from database.

Let me know if it solved ur problem
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