Search code examples
c#asp.netsql-server-ce

error while inserting primary key


I'm trying (SQL Server Compact) to add primary key constraint on existing table that has some rows in it. While adding primary key I'm getting the error:

"A duplicate key cannot be inserted into a unique index"

I don't what this is, can anyone help me with this?


Solution

  • Try this

    select id_column, count(*) from your_table group by id_column having count(*) > 1
    

    If there are any records returned from this above query you cannot add a primary key on id_column since duplicate IDs exist.

    Of course you will need to replace id_column and your_table with the appropriate names.