473,231 Members | 1,742 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,231 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 45827
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.