UPDATE操作出现主键冲突错误,如果出现了这样的问题,你一定会感到匪夷所思!
mysql> UPDATE `GC_Price` SET `Price` = '155.00', `OptTime` = '2013-07-01 16:12:36', `CRCode` = '0109' WHERE (PRID = 364706189);
ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'
我的GC_Price表的主键PRID的最大值为:437573062,而求表中也没主键为'2147483647' 的记录。
2147483647:注意,这个int的最大值,不信,你用计算器算一算。应该是表的自增主键达到了最大值!!!外键关联或触发器等存储代码???
mysql> show triggers like '%Price%'\G;
*************************** 1. row ***************************
Trigger: t_price_changed_prid_insert
Event: INSERT
Table: GC_Price
Statement: INSERT INTO BE_PRID_Changed(PRID,Type) VALUES (NEW.PRID,1);
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Trigger: t_price_changed_prid_update
Event: UPDATE
Table: GC_Price
Statement: INSERT INTO BE_PRID_Changed(PRID,Type) VALUES (NEW.PRID,2)
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
2 rows in set (0.00 sec)
查看表BE_PRID_Changed的结构
mysql> show create table BE_PRID_Changed\G;
*************************** 1. row ***************************
Table: BE_PRID_Changed
Create Table: CREATE TABLE `BE_PRID_Changed` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`PRID` int(12) NOT NULL COMMENT '来源PRID',
`Type` tinyint(1) NOT NULL COMMENT '1 from insert 2 from update',
`CreatedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
`Status` tinyint(1) DEFAULT '1' COMMENT '1待处理 2已处理 ',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2147483648 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
AUTO_INCREMENT=2147483648的值已经达到了最大,UPDATE出现主键冲突的“真”凶也找到了。
解决办法,很多:
1)直接删除该触发器(如果没用!!!)
2)rename该表,重新创建一个相同的空表。