Solved

Primary Key Violation

Posted on 1998-08-23
6
313 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error when loading the database 16 55
ASP.NET 5 Templates 2 65
SQL Exceptions 3 37
How can i get data when i use where clause with group by? 3 20
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

912 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now