Solved

Primary Key Violation

Posted on 1998-08-23
6
315 Views
Last Modified: 2010-03-19
I have a table with Identity(1,1) as Primary Key and been using this table for some months now. All of a sudden, I got the following error message from my app (VB5) saying that there's a primary key violation :
"[Microsoft][ODBC SQL Server Driver][SQL Driver] Violation of PRIMARY KEY constraint 'PK_Staff_Work_Hours_2__78'. Attempt to insert duplicate key in object 'Staff_Work_Hours' (#2627) [Microsoft][ODBC SQL Server Driver][SQL Server] Command has been aborted. (#2721) "
How did that happen?? What is the cause of it?? and how can I fix it???
0
Comment
Question by:ppmb
  • 3
  • 2
6 Comments
 
LVL 2

Accepted Solution

by:
lmorris earned 100 total points
ID: 1089693
Do a

DBCC checkident(tablename)

I encounter this problem every now and then after shuting down my SQL 6.5. When I bring it back up, the identity value on a table that was being inserted into prior to the shutdown is screwed up. This always fixes it.

I'm at home at the moment, but I'm pretty sure that's the syntax. If that doesn't work try dbcc checkident 'tablename'.

0
 

Author Comment

by:ppmb
ID: 1089694
When you say "shutting down" the SQL Server 6.5, do you mean the SQL Server itself being shut down or the NT Server is being shut down??? Because both situations happen from time to time. DBA of SQL Server will kick out everyone when it is doing a gigantic job and then restarts the SQL Server. Sometimes the NT crashes and will be restarted. So under what situation the identity column will be screwed up??? Thanks!
0
 

Author Comment

by:ppmb
ID: 1089695
Forgot to ask you this:
Does dbcc automatically fix the error existed in the identity column???
Thanks!
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 4

Expert Comment

by:mitek
ID: 1089696
No, this will not fix an error. The proposed answer does not solve your problem, and you may feel free to reject it.

This is a very well-known problem, and it cannot be solved with DBCC, because it doesn't cause logical corruption of table structure.
We had this problem a big way, and developed a workaround against it.
0
 
LVL 4

Expert Comment

by:mitek
ID: 1089697
Actually, you MIGHT fix it with DBCC checkident once, but it will surely re-appear some time later. So, if you only care about just fixing it once, by all means, you should accept lmorris's answer, because it probably will solve the problem once, but it fixes only the consequence, not the cause.
0
 

Author Comment

by:ppmb
ID: 1089698
So ,mitek, what suggestion do you have if I insist in using Identity as Primary Key?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question