duplicate key error

  • what is an example of duplicate key object

    when i try a insert into a table i get the error:

    Violation of PRIMARY KEY constraint 'Customers_PK'. Cannot insert duplicate key in object 'dbo.Customers'.

    For example if have i 5 columns that all have the value 2, is this the problem?

  • Hi,

    Primary Key is a constraint that 'uniquely' identifies a row in a table. Check Books On Line for more information.

    [font="Verdana"]Renuka__[/font]

  • Not sure if I am getting your question right...

    but it seems that you have 5 different columns having value 2,

    but the issue is the column that is the Primary Key; will not allow another record with value 2 for the same primary key column.

  • Five different columns that all have a value of 2 would not create the problem. Five different rows that all have a value of 2 in the primary key colum would be a source of that error.

    By definition, a primary key must uniquely identify each row. If we create a customers table and make customer number the primary key, or place a unique constraint over that column, there can be one and only one row for any given customer number.

    It is possible to have a primary key consist of multiple columns. For example an invoice_detail table might have a key that consists of the invoice number column and a product number column.

    Since this is really a fundamental concept, it might be a good idea to find a good book and do a little studying before proceeding further. It may save you a lot of frustration in the long run.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • what if im trying to do an insert of records that already there, then will i get the message about duplicate key objects?

  • That is exactly the problem. If you have a customer 12345 already in your table, you can't insert a second customer with 12345. If you are wanting to change the information associated with customer 12345, you have to UPDATE it, not INSERT it a second time.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • let see if i got this correct,

    if i run update with the insert with the same records, will i get the same duplicate error, or will it overwrite the records with the same values over and over?

  • UPDATE and INSERT are mutually exclusive operations. You either run an update OR an insert query. Updates overwrite existing information, and will not produce the error.

    Again, these are very elementary concepts. You need to get an intro manual and do some reading on your own.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

  • got it thanks

Viewing 9 posts - 1 through 8 (of 8 total)

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