问题
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