It's okay to use IGNORE
in any single key, the effect is the same.
IGNORE
acts on both PK violations and other constraints , such as UNIQUE. So much so that you can use INSERT IGNORE if your table has some constraint in this format, including:
alter table vote add constraint unique (ponto, endereço);
In case of a constraint violation, the insert simply does not happen, and only a warning is generated.
Just to be aware, remember also that INSERT … ON DUPLICATE KEY UPDATE
exists which may be more interesting in some cases (maybe not in your example, which seems to me to be a kind of key-value relationship).
Note:
If you plan to use LAST_INSERT_ID()
, you will need some care. According to documentation :
If you use INSERT IGNORE and the row is ignored, the LAST_INSERT_ID () remains unchanged from the current value (or 0 is returned if successful)
That translating freely is
If you use INSERT IGNORE and the line is ignored, the LAST_INSERT_ID () remains as it was previously (or 0 is returned if there was no INSERT successful)
This means that you should check if there was even a real insert, because if it does not exist, the LAST_INSERT_ID () value may be contaminated and this can be disastrous for the application.
It may be the case to combine the result with MYSQL_AFFECTED_ROWS ( ) to know if the returned value is actually usable. If the returned value is 0
, it is a sign that the returned ID does not match the query that has just been performed.