471,579 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,579 software developers and data experts.

Violation of PRIMARY KEY constraint...Cannot insert duplicate key Problem

37
Okay I have 2 tables:

Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes)

Table B - holds a distinct list of current serial numbers and the corresponding model numbers (primary key on serial_numbers)

Since Table A has no constraints duplicates may exist.

Additionally, table A is actually an Excel spreadsheet that is maintained by an employee that records new hardware as they come in. I am pulling the data from Table A to insert into Table B and so construct the insert query to prohibit any entries into Table B (which has a primary key set on the serial_number column) so duplicate entries aren't attempted on the insert.

insert into system_serial_numbers(serial_num,system_model)
select [sys #], [model #] from EXCEL...Master$
where [sys #] not in (select serial_num from system_serial_numbers)
and [model #] is not null

When I run this query I receive the following error and the insert fails

Violation of PRIMARY KEY constraint 'pk_serial'. Cannot insert duplicate key in object 'dbo.system_serial_numbers'.
The statement has been terminated.

If I take one of the records retrieved from the select statement and insert it into the table it succeeds which leads me to believe that maybe one of the records returned is a duplicate but theoretically this shouldn't be happening because I specified that only records not in the existing destination table be inserted.

Anyone have any ideas why this problem is happening or have ever seen this before???
Jan 28 '07 #1
2 45676
iburyak
1,017 Expert 512MB
You have duplicates in Excel that are not in your table yet. Just use distinct in select query...

insert into system_serial_numbers(serial_num,system_model)
select distinct [sys #], [model #] from EXCEL...Master$
where [sys #] not in (select serial_num from system_serial_numbers)
and [model #] is not null



Good Luck.
Jan 29 '07 #2
almaz
168 Expert 100+
You have duplicates in Excel that are not in your table yet. Just use distinct in select query...
iburyak, unfortunately it may not help as unique constraint is set to serial_num field only, and you are using distinct to select unique serial_num, system_model pairs (so if there are different records with the same serial_num and different system_model values there still will be a problem).

mivey4, There is no ideal solution for your problem. If Excel file contains different system_model values for the same serial_num value, you'll have to either manually or based on some automatic logic decide what is the most appropriate value for system_model.
Following query selects these problematic records:
Expand|Select|Wrap|Line Numbers
  1. select  [sys #], [model #]
  2. from    EXCEL...Master$
  3. where   [sys #] in (select  [sys #]
  4.                     from    EXCEL...Master$
  5.                     where   [sys #] not in (select  serial_num
  6.                                             from    system_serial_numbers) and [model #] is not null
  7.                     group by [sys #]
  8.                     having  count(*) > 1) and [model #] is not null
  9.  
Following solution is one of the possible automatic solutions
Expand|Select|Wrap|Line Numbers
  1. insert into system_serial_numbers (serial_num, system_model)
  2. select [sys #], max([model #])
  3. from   EXCEL...Master$
  4. where  [sys #] not in (select  serial_num
  5.                         from    system_serial_numbers) and [model #] is not null
  6. group by [sys #]
Jan 29 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by John Simmons | last post: by
1 post views Thread by Zamdrist | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.