views:

409

answers:

2

EDIT: I seem to get the error listed below on every insert no matter what data I try to insert. So maybe my table was corrupted or something? Anyway, here's my question:

I have a MySQL table

CREATE TABLE `AcpConfig` (
  `ndss_id` int(11) NOT NULL default '0',
  `acp_id` int(11) NOT NULL default '0',
  `run_date` date NOT NULL default '0000-00-00',
  `hw_5_threshold` tinyint(1) NOT NULL default '0',
  `stp_on` tinyint(1) NOT NULL default '0',
  `sort_on` tinyint(1) NOT NULL default '0',
  `afcs_ocr_message_format` tinyint(1) NOT NULL default '0',
  `use_hw` tinyint(1) NOT NULL default '0',
  `test_mode` tinyint(1) NOT NULL default '0',
  `afcs_version` varchar(255) NOT NULL default '',
  `acp_build` varchar(255) NOT NULL default '',
  `id` int(11) NOT NULL auto_increment,
  `swstp_in_acp_rack` int(11) NOT NULL default '0',
  `acplookup_id` int(11) NOT NULL default '0',
  `bfind_cksum` varchar(255) NOT NULL default '',
  `tz_cksum` varchar(255) NOT NULL default '',
  `fetched` varchar(4) NOT NULL default '"NO"',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `ndss_id` (`ndss_id`,`acp_id`,`run_date`),
  KEY `ndss_acp` (`ndss_id`,`acp_id`),
  KEY `ndss_acp_rundate` (`ndss_id`,`acp_id`,`run_date`),
  KEY `run_date` (`run_date`),
  KEY `acplookup_rundate` (`acplookup_id`,`run_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

And it's got about half a million rows. I'm trying to perform a simple INSERT

INSERT INTO AcpConfig (ndss_id, acp_id, run_date, hw_5_threshold, stp_on, sort_on, afcs_ocr_message_format, use_hw, test_mode, afcs_version, acp_build, swstp_in_acp_rack, acplookup_id, bfind_cksum, tz_cksum) VALUES ('75', '5', '2009-07-22', '75', '1', '1', '0', '1', '0', '1.5.2', '041709', '2', '269', '0', '1950359846');

and it gives me the error

ERROR 1062 (23000): Duplicate entry '502831' for key 1

which implies that I'm violating my UNIQUE constraint on the three fields ndss_id, acp_id, and run_date. (The id 502831 is not a row in my table and seems to be the next id that would be used if the row had been inserted.) The problem is, if I SELECT against those fields with the same values

select * from AcpConfig where ndss_id=75 and acp_id=5 and run_date='2009-07-22';

then it returns no results. So I'm not actually duplicating anything. My other keys are all just indexes and not unique constraints; I also have the one UNIQUE constraint as you can see from my CREATE TABLE statement. So why is it telling me that I have a duplicate?

+3  A: 

Could it be possible your sequence numbers are off with regard to auto_increment on id? Try setting the key higher and retry the insert.

ALTER TABLE AcpConfig AUTO_INCREMENT = 1;

Apparently this will reset the next auto_increment to be the next highest available value.

northpole
Awesome suggestion; I was wondering how to do this exact thing, so this is a good reference. However, as I say in my own answer below the problem turned out to be fixed by a REPAIR TABLE statement, so I'm not sure whether this would have itself fixed things. Thanks anyway.
Eli Courtwright
+1  A: 

Apparently the table was corrupted. I ran CHECK TABLE and saw some corruption errors and then ran REPAIR TABLE and it seemed to work; afterward my INSERTS started working again.

mysql> check table AcpConfig;
+---------------+-------+----------+----------------------------------------------------------+
| Table         | Op    | Msg_type | Msg_text                                                 |
+---------------+-------+----------+----------------------------------------------------------+
| acp.AcpConfig | check | warning  | 8 clients are using or havent closed the table properly | 
| acp.AcpConfig | check | warning  | Size of datafile is: 32079848       Should be: 32079784  | 
| acp.AcpConfig | check | error    | Found 495762 keys of 495761                              | 
| acp.AcpConfig | check | error    | Corrupt                                                  | 
+---------------+-------+----------+----------------------------------------------------------+
4 rows in set (3.50 sec)

mysql> repair table AcpConfig;
+---------------+--------+----------+----------------------------------------------+
| Table         | Op     | Msg_type | Msg_text                                     |
+---------------+--------+----------+----------------------------------------------+
| acp.AcpConfig | repair | warning  | Number of rows changed from 495761 to 495762 | 
| acp.AcpConfig | repair | status   | OK                                           | 
+---------------+--------+----------+----------------------------------------------+
2 rows in set (13.14 sec)
Eli Courtwright
I am guessing REPAIR TABLE fixed your sequence numbers as well
northpole