473,735 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Violation of PRIMARY KEY constraint...Ca nnot insert duplicate key Problem

37 New Member
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_n umbers(serial_n um,system_model )
select [sys #], [model #] from EXCEL...Master$
where [sys #] not in (select serial_num from system_serial_n umbers)
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_ser ial_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 45874
1,017 Recognized Expert Top Contributor
You have duplicates in Excel that are not in your table yet. Just use distinct in select query...

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

Good Luck.
Jan 29 '07 #2
168 Recognized Expert New Member
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
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

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 an existing username. I have a database full of the same usernames!
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), HOME_CNTRY_IND char(1), CONSTRAINT cnty_key PRIMARY KEY (CNTY_CNTRY_CD, ST)) I'm using 2 fields for the primary key constraint My insert statement looks like this:
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 transaction table come from different sources. The application can show error message and the user can file a transaction again manually, but I want the application can have multiple re-tries to perform insert statement until there is no primary key...
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 incoming students into a course. 1. I have two tables. Table A has all the fields for the student personal information in which I have Record # as the primary key. Table B has fields for the incoming student Educational Background (high school...
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 constraint 'PK_Insured_Person'. Cannot insert duplicate key in object 'dbo.Insured_Person'." Code is like that: Insert Into Table ( Id, Columns ) Select Min(Id) as Id,
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 inserting does not represent a duplicate on the primary key. To ensure this I ran the following and still got the error: update tblCustomsTariffCodeTreeMap set TariffCodeID = (SELECT max(tariffcodeid)+1 from tblCustomsTariffCodeTreeMap) where...
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 trying to insert a duplicate record? This sql statement false due to the primary key violation: Insert Into MHGROUP.Custom2 Select
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 been terminated. MY QUESTION:
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 there's already one with that ID, but the problem is a bit more complicated. The table is supposed to auto-increment the value for the primary key when a new record is inserted. But no matter what I do, I can't seem to insert more than one record...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.