I have a tab separated text file in the format
id | field 1 | field 2 ...
I want to insert this into a mysql database with id as the primary key but the text file may contain duplicate id’s .
- How to make sure that there’s just one entry corresponding to each id.
- How to make a choice between two lines having the same id (Yes, they might not be consistent, but it’s okay to choose one over other like the first or the last occurrence )
I would do a
SELECTbeforeINSERTand count the number of rows returned by theSELECT. Something like this:If that returns any row, don’t insert and go to next. Otherwise insert it.
Edit: This would be a post strategy. It would be good if you could add a Unique Constraint to guarantee uniqueness. Something like: