MySQL query giving duplicate entry error 1062

血红的双手。 提交于 2019-12-11 16:53:04

问题


Why is this query giving an error? The error is: SQL Error (1062): Duplicate entry '0' for key 'PRIMARY'

INSERT INTO `static_number_source` (`IDString`, `source`) VALUES
('RUS-001A', 'Thub'), #one
('RUS-001A', 'Fort'), #two
('RUS-002A', 'Thub'), #three
('RUS-002A', 'Fort'), #four
('RUS-003A', 'Thub'), #five
('RUS-003A', 'Fort'), #six
('RUS-004A', 'Thub'), #seven
('RUS-004A', 'Fort'); #eight

回答1:


You can do either Alter the table to add AUTO_INCREMENT TO THE ID field, or always provide an Id on Inserts

For adding AUTO_INCREMENT just find the largest value of id in the table and set id it to one more.




回答2:


IDString seem to be set as PRIMARY. PRIMARY must have unique value. If you want to use multiple value with the same value, use a regular non-unique INDEX.

By the way, if you have a UNIQUE index on both field, thoses are similar :

('RUS-002A', 'Fort'), #three
('RUS-002A', 'Fort'), #four


来源:https://stackoverflow.com/questions/11367532/mysql-query-giving-duplicate-entry-error-1062

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!