DB2 Duplicate Key Debugging

If you’ve ever gotten a DB2 error like this…

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 "XX.TABLE_NAME" from having duplicate values for 
the index key.  SQLSTATE=23505

You may be asking yourself, but exactly which unique index is causing the problem?

Use the info from the error along with this query to find out:

SELECT *
FROM   SYSCAT.INDEXES
WHERE  IID = 1
       AND TABSCHEMA = 'XX'
       AND TABNAME = 'TABLE_NAME';

Of course you will want to replace the WHERE clause conditions with your values.

Leave a Reply