Cannot insert duplicate key in object

  • In table tUSERI primary key was added in the column [Email].

    I deleted a user abc@gmail.com from tUSER.

    When I try to add the same user abc@gmail.com got the following error.

    What causes the error?

    Please help clean and update the table tUSER.

    Violation of PRIMARY KEY constraint 'PK_tuser_2'. Cannot insert duplicate key in object 'dbo.tuser'. The duplicate key value is (abc@gmail.com)

  • Doesn't look like you deleted the user since abc@gmail.com still exists in the table. The error is what the message is telling you - abc@gmail.com already exists in the table and with that being the primary key, the values for Email need to be unique in the table.

    Sue

  • I did not use code to delete it.

    I open table in edit mode and then hit delete key to delete it.

    Does it make difference?

    I checked it many times the record is not in table but still causes error.

  • Run this query, and see what is returned:

    SELECT * FROM dbo.tuser WHERE email = 'abc@gmail.com'

    If results are returned, then run this:

    DELETE dbo.tuser WHERE email = 'abc@gmail.com'

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • adonetok wrote:

    I did not use code to delete it.

    I open table in edit mode and then hit delete key to delete it.

    Does it make difference?

    I checked it many times the record is not in table but still causes error.

    When you open a table in SSMS in edit mode, you lock the entire table.  Which is probably not a good thing to do in a production environment.

    So, yes, it does make a difference.

    Michael L John
    If you assassinate a DBA, would you pull a trigger?
    To properly post on a forum:
    http://www.sqlservercentral.com/articles/61537/

  • I did many times in SQL 2012.

    From now on I will do it in code.

    Thank you.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply