473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 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 45831
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

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

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
1
by: Cliff | last post by:
I'm trying to do multiple insert statements. The table looks like this: CREATE TABLE $table (CNTY_CNTRY_CD char(3),ST char(2), CNTY_CNTRY_DESCR varchar(50),CNTY_CNTRY_IND char(1),...
2
by: John | last post by:
The ASP application inserts transaction records in transaction table with the system time as the primary key. However, it is possible to have primary key violation because the records in...
1
by: greentopper | last post by:
Hi I am a first time user of sql server, and of late I am trying to build a database using sql server and I am starting to panic. This is what I am trying to do: I am creating a db to register...
3
by: Ashutosh Jha | last post by:
I have to Insert records through select record from a table with group by clause within a store procedure. but when i am executing the procedure, It is showing error" Violation of PRIMARY KEY...
2
by: embarkr | last post by:
I am getting the error: "Violation of PRIMARY KEY constraint 'PK_tblCustomsTariffTreeMap'. Cannot insert duplicate key in object 'dbo.tblCustomsTariffCodeTreeMap'." However, the record I am...
1
by: Zamdrist | last post by:
Violation of PRIMARY KEY constraint 'PK_CUSTOM2'. Cannot insert duplicate key in object 'MHGROUP.Custom2' Is there ANY other reason this violation of the primary key would happen OTHER than a...
7
by: akmaRudiliyn | last post by:
Hai everybody :). I have problem and need help. ERROR: ERROR Violation of PRIMARY KEY constraint 'PK_Table1_01'. Cannot insert duplicate key in object 'dbo.table1'. ERROR The statement has...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.