Solved

Primary Key Violation

Posted on 1998-08-23
6
312 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

757 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

18 Experts available now in Live!

Get 1:1 Help Now