Tutorial :java servlets : duplicate key row in object with unique index 'XAK1timItem'



Question:

I've written this java servlet which inserts items into a table however it fails. I think it might be due to my insertion and deleting which got me in trouble some how. The java servlet runs an insert statement into sql server. In my error log, it says:

com.microsoft.sqlserver.jdbc.sqlserverexception: cannot insert duplicate key row in object 'dbo.timitem' with unique index 'XAK1timitem'.   

any ideas?

UPDATE: i found there is an index called "XAK1timItem (Unique, Non-Clustered)" which I'm not really sure what to do with.. hope this helps the question.


Solution:1

The unique index will enforce uniqueness for the combination of the rows included in the index. So if you have a row in the database which has, for the indexed column, values equal to those you are trying to insert, you will get an error back from the database.

The AK part indicates that this is an alternative key which probably means that the table has a regular primary key, and does not need to rely on the AK for unique identification of a row.

Some options:

  • drop the index if not needed
  • add another column to the unique index
  • make the index not unique, so that it allows duplicate values
  • check if there is already a row that matches the one you are about to insert and abort the insert, but I guess you don't want to do this

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »