views:

255

answers:

1

While building my application with relational tables I ran into the following problem :

I have the following table, for this example named "valores":

-----------------------
| id  |     value     |
-----------------------
|  1  |  Unique VAL   |
|  2  |  Unique VAL2  |
-----------------------
ID = AUTOINCREMENT
VALUE = UNIQUE

What I'm trying to do is insert the a NEW value if it doesn't exist already, the way I'm doing it right now is:

INSERT IGNORE INTO valores (id, value) VALUES (NULL, "Unique VAL2");

So it won't do anything. Because the value already exists.. now my question is:

¿Is this the best and the fastest way to do it?

+2  A: 

I think this is the best way for your scenario.

For different purposes you could use ON DUPLICATE KEY UPDATE or REPLACE INTO

Read more: insert, replace, on duplicate.

daremon
Thank you very much for the answer :)
kuroir