MySQL Duplicate Key Entry Error when there is not already an entry for this key

MySQLprimary-key

Duplicate entry '35839' for key 'PRIMARY'

I am receiving the above error message suddenly on a database I keep logs in. This is an auto-increment field which the last key for the last entry is 35838. If I perform a search for the key 35839, no results are returned. (Also, I'm looking at the database through phpMyAdmin and can see that the last record is 35838.)

The table structure is:

CREATE TABLE IF NOT EXISTS `raw_data` (
  `id` int(12) unsigned NOT NULL AUTO_INCREMENT,
  `oid` int(12) unsigned NOT NULL,
  `data` longtext NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `oid` (`oid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=35839 ;

You can see that the next ID it is expecting to make is 35839. Yet it thinks that it is already in the table…

Also, the oid that is trying to be inserted is also unique. It is an order ID being generated by another system and is never duplicated. Plus, I've checked to see if it is already in the database and it is also not in the database already.

Is there anything I could be overlooking as to why this would suddenly start telling me that it is trying to enter a duplicate? It has been working for two years now. haha~

Thanks!

Select Statement

Select * From raw_data Where id = 35839;

MySQL returned an empty result set (i.e. zero rows). (Query took 0.0012 sec)

Best Answer

From @ypercubeᵀᴹ's suggestion:

The table is MyISAM so this is probably a corruption issue. Try running CHECK and then REPAIR the table (taking the necessary backup before!)

Details and various options for both commands can be found in CHECK and the links from there.

I then ran Check Table raw_data Extended which resulted in:

livereps_webdb.raw_data     check   warning     1 client is using or hasn't closed the table prope...
livereps_webdb.raw_data     check   warning     Size of datafile is: 12921069568       Should be: ...
livereps_webdb.raw_data     check   error   Found 15839 keys of 15838
livereps_webdb.raw_data     check   error   Corrupt

I then ran Repair Table raw_data Extended which resulted in:

livereps_webdb.raw_data     repair  info    Found block that points outside data file at 12921...
livereps_webdb.raw_data     repair  info    Found block with too small length at 12921068496; ...
livereps_webdb.raw_data     repair  info    Found block that points outside data file at 12921...
livereps_webdb.raw_data     repair  info    Found block that points outside data file at 12921...
livereps_webdb.raw_data     repair  status  OK

This fixed the problem.