没有重复时,MySQL重复输入错误

时间:2009-07-23 13:52:13

标签: mysql duplicates unique-constraint

编辑:无论我尝试插入什么数据,我似乎都会在每个插入中得到下面列出的错误。也许我的桌子被腐坏了什么?无论如何,这是我的问题:

我有一个MySQL表

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

它有大约五十万行。我正在尝试执行一个简单的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');

它给了我错误

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

这意味着我违反了我对三个字段ndss_id,acp_id和run_date的UNIQUE约束。 (id 502831在我的表中不是一行,并且似乎是插入行时将使用的下一个id。)问题是,如果我对具有相同值的那些字段进行SELECT

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

然后它不返回任何结果。所以我实际上并没有复制任何东西。我的其他键都只是索引而不是唯一的约束;从我的CREATE TABLE语句中可以看到,我也有一个UNIQUE约束。那么为什么它告诉我我有重复?

2 个答案:

答案 0 :(得分:3)

关于id上的auto_increment,你的序列号是否可以关闭?尝试将键设置得更高,然后重试插入。

ALTER TABLE AcpConfig AUTO_INCREMENT = 1;

显然,这会将下一个auto_increment重置为下一个最高可用值。

答案 1 :(得分:2)

显然桌子已损坏。我运行CHECK TABLE并看到一些损坏错误,然后运行REPAIR TABLE它似乎工作;之后我的INSERTS再次开始工作。

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)