Inserting duplicate indexed field in mysql

2007 Feb 8

#1062 - Duplicate entry indexed_field' for key 2

Mysql will return a duplicate entry error when trying to insert a duplicate value in an indexed field in the database. We could modify our sql insert state to check for primary key duplicate or index field duplicate and automatically update the first field with the updated value like this.


INSERT INTO table1 (a,b,c)
SELECT a,b,c
FROM table2
WHERE a IS NOT NULL
ON DUPLICATE KEY
UPDATE table1.a = table2.a

Tweet this post

1 Comment

u guys are the best!

Leave a comment


About Me


Alfredo Sanchez is an internet professional focusing on the study search engines behavior in particular. Supports Free Open Source Software and currently develops applications with it using XAMPP.

Recent Entries

Close