473,395 Members | 2,192 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,395 software developers and data experts.

"Violation of PRIMARY KEY constraint" when no duplicate exists?

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:
Expand|Select|Wrap|Line Numbers
  1. update tblCustomsTariffCodeTreeMap
  2. set TariffCodeID = (SELECT max(tariffcodeid)+1 from tblCustomsTariffCodeTreeMap)
  3. where TreeNodeID = 955
If I'm explicitly inserting a value which is 1 greater than the max value, I'm puzzled as to how it can be a duplicate...?

The primary key is defined as follows:
  • index_name : PK_tblCustomsTariffTreeMap
    index_description : clustered, unique, primary key located on PRIMARY
    index_keys : TariffCodeID, TreeNodeID

Any suggestions?
Sep 26 '07 #1
2 12115
ck9663
2,878 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. update tblCustomsTariffCodeTreeMap
  2. set TariffCodeID = (SELECT max(tariffcodeid)+1 from tblCustomsTariffCodeTreeMap)
  3. where TreeNodeID = 955
If I'm explicitly inserting a value which is 1 greater than the max value, I'm puzzled as to how it can be a duplicate...?

The primary key is defined as follows:
  • index_name : PK_tblCustomsTariffTreeMap
    index_description : clustered, unique, primary key located on PRIMARY
    index_keys : TariffCodeID, TreeNodeID

Any suggestions?
first the why...your query is an update and a select....your select will run first..so if the max+1, let's say is = 16...it's the same as doing:

Expand|Select|Wrap|Line Numbers
  1. update tblCustomsTariffCodeTreeMap
  2. set TariffCodeID = 16
  3. where TreeNodeID = 955
so if you have two or more records with TreeNodeID = 955, you'll experience this problem with PK violation....what are you trying to do on your table?
Sep 26 '07 #2
first the why...your query is an update and a select....your select will run first..so if the max+1, let's say is = 16...it's the same as doing:

Expand|Select|Wrap|Line Numbers
  1. update tblCustomsTariffCodeTreeMap
  2. set TariffCodeID = 16
  3. where TreeNodeID = 955
so if you have two or more records with TreeNodeID = 955, you'll experience this problem with PK violation....what are you trying to do on your table?
Thank you ck9663. This is a sure sign that I've been staring at the screen too long! I was originally dealing with an INSERT which I changed to an UPDATE but did not modify the criteria properly. Then when I got the error I didn't see the obvious problem (a classic case of seeing what you expect to see!). thanks again - I'm back from lala land now.
Sep 26 '07 #3

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

Similar topics

1
by: ATS | last post by:
ERR - WebClient.DownloadData returns "protocol violation" Please help, I'm getting a "protocol violation" from a site that is working perfectly fine when I issue WebClient.DownloadData as...
1
by: Mike Hunter | last post by:
(Please CC me on any replies as I'm not on the list) Hi, After a recent power failure, a program that uses a pgsql backend (netdisco) started to send me nastygrams. I tried the author's...
2
by: borajetta | last post by:
ERROR: duplicate key violates unique constraint "pg_class_oid_index" I found a post earlier but wanted to post up my info as the other persons did not look like the same type of error: I tried...
4
by: Toufik | last post by:
Hi, I've the folowing error "Concurency violation, The update command affected 0 records", when I execute the save command "DA.Update(DS, "tblName")" DA is a dataadapter linked to a...
3
by: Scott McDermott | last post by:
I have an application that is making an HTTP request with HttpWebRequest.GetRequest. Unless I set 'httpWebRequest useUnsafeHeaderParsing="true"' in the web.config, I get a 'The server committed a...
2
by: Curten | last post by:
Hi, I want to read data from a txt-file that looks like this: aaa 4 12.45 bbb 3 7.34 ccc 12 3.45 and store the data in an array of structures. The struct and array are defined...
2
by: Morgan Cheng | last post by:
I have a crawler in C#. The main functionality relies on HttpWebRequest and HttpWebResponse. It shows that some webpages are not downloaded successfully. A WebException is thrown with info...
3
by: weird0 | last post by:
I have two tables accounts and ATM and i am trying to insert a tuple in ATM with accountId as foreign key. But even this simple work,I encounter the following error: The INSERT statement...
6
by: hadad.yaniv | last post by:
Hello, i am new to c++, i hav a vector of typed object: vector<Man*People; When i do a second pushback, even for the same object the program crash say: "An Access Violation (Segmentation...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.