Overriding unique indexed values

Posted by Yeti on Stack Overflow See other posts from Stack Overflow or by Yeti
Published on 2010-06-17T09:38:45Z Indexed on 2010/06/17 9:43 UTC
Read the original article Hit count: 240

Filed under:
|

This is what I'm doing right now (name is UNIQUE):

SELECT * FROM fruits WHERE name='apple';

Check if the query returned any result. If yes, don't do anything. If no, a new value has to be inserted:

INSERT INTO fruits (name) VALUES ('apple');

Instead of the above is it ok to insert the value into the table without checking if it already exists? If the name already exists in the table, an error will be thrown and if it doesn't, a new record will be inserted.

Right now I am having to insert 500 records in a for loop, which results in 1000 queries. Will it be ok to skip the "already-exists" check?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql