Solved

Primary Key Violation

Posted on 1998-08-23
6
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

615 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