奇怪的更新语句行为

时间:2016-11-23 11:52:22

标签: mysql sql

执行以下查询

UPDATE tbl_outcome SET  `type` = 'do', `statement` = 'NAREs, private sector organizations and NGOs develop innovation platforms, disseminate  market information to processors and develop their marketing skills', 
`actionsite_id` = 440, `code` = 'G&T-I' WHERE (outcome_id = '546');

会导致此错误

SQLSTATE[23000]: Integrity constraint violation:
 1062 Duplicate entry 'NAREs, private sector organizations and NGOs develop innovation ' for key 'statement'

注意到有另一行共享相同的语句值,但为什么它拒绝重复

CREATE TABLE `tbl_outcome` (
      `outcome_id` int(11) NOT NULL AUTO_INCREMENT,
      `type` varchar(2) NOT NULL,
      `statement` varchar(255) DEFAULT NULL,
      `actionsite_id` int(11) NOT NULL,
      `code` varchar(10) DEFAULT NULL,
      PRIMARY KEY (`outcome_id`),
      UNIQUE KEY `statement` (`statement`,`actionsite_id`),
      KEY `actionsite_id` (`actionsite_id`),
      CONSTRAINT `tbl_outcome_ibfk_1` FOREIGN KEY (`actionsite_id`) REFERENCES `tbl_actionsite` (`actionsite_id`) ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=551 DEFAULT CHARSET=utf8;

mysql> mysql> SELECT statement FROM crpcoreix.tbl_outcome limit 5;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| statement                                                                                                                                                                                                                            |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|   Female and male farmers/shakeholders use/adopt/implement methods/tools/approaches/technologies/NRMpractices/Varieties/framework/concepts                                                                                            |
|   Female and male farmers/stakeholders (inc.youth) awareness, skills and knowledge increased                                                                                                                                          |
|   Female and male farmers/stakeholders (inc.youth) awareness, skills and knowledge increased                                                                                                                                          |
|   Interventions better targeted/prioritized by stakeholders                                                                                                                                                                           |
|  At least (3-15?) potato candidate varieties (short season, heat tolerant, and pest/disease resistant) compatible  with cereal crops: identified and recommended for release in at least  three countries (China, Bangladesh, India) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

2 个答案:

答案 0 :(得分:1)

因为名为{/ p>的Statement列上有唯一的键约束

UNIQUE KEY `statement` (`statement`,`actionsite_id`),

希望这有帮助!!!

答案 1 :(得分:0)

试试这个

 SHOW INDEX FROM tbl_outcome

这将为您提供唯一键的index_name

然后运行以下查询

ALTER TABLE tbl_outcome DROP INDEX index_name

这将删除唯一键约束