Web Hosting Talk







View Full Version : Best SQL practice on UNIQUE field


oldunis
10-02-2009, 02:39 PM
Hi all,

I have a batch script that a CSV file to a Mysql database.

I have a unique row named CODE in my mysql table, and I was wondering what was the best thing to do:
Parse the entier csv file and:

Issue a COUNT(*) to know if the CODE is already in the database. If not, issue the insert command.
Just issue the INSERT command. Since it is unique, no duplicates will be made.



We are talking about 500 lines, every hour. I was wondering if it was easier to issue the count(*) and insert, or just insert and let mysql return an error if the code is already in the table ...

Thanks in advance,
JP

mattle
10-02-2009, 04:16 PM
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

01globalnet
10-02-2009, 05:37 PM
You could also use REPLACE.