views:

338

answers:

1

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.

+1  A: 

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
stili