No problem to use IGNORE
in any single key, the effect is the same.
the IGNORE
acts on both PK violations and other constraints, like UNIQUE. So much so that you can use INSERT IGNORE if your table has any Constraint in this format, including:
alter table vote add constraint unique (ponto, endereço);
In the event of infringement of Constraint, simply Insert does not happen, and only one is generated Warning (that will be ignored without problems by the application).
For the record, also remember that there is a INSERT … ON DUPLICATE KEY UPDATE
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 intend to use the LAST_INSERT_ID()
, will need some care. According to the 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 the Connection has not yet performed a Successful INSERT)
Which translating freely is
If you use INSERT IGNORE and the line is ignored, LAST_INSERT_ID() remains as before, (or 0 is returned if there has been no successful INSERT)
This means that you should check if there was even a real insertion, because if there is not, the value of LAST_INSERT_ID() can be contaminated and this can be disastrous for the application.
Perhaps it is the case to combine the result with the MYSQL_AFFECTED_ROWS() to know if the returned value is actually usable. If the returned value is 0
, is a sign that the returned ID does not match the query which has just been held.
It seems that the
IGNORE
is not the solution, my hope is that had some way to get the "duplicate" ID without having to make aSELECT
. Thanks, practically already solved, I’ll wait until tomorrow to see if it appears any more interesting answer.– KaduAmaral
@Kaduamaral IGNORE is kind of boring in this aspect. I even wondered if I could solve it with subquery, but on second thought, there are things that are more prone to problems that we want to simplify. As for waiting for more answers, I always think it’s good, because it encourages people to post interesting things and complement them with new information.
– Bacco
I think the same about the problems as about the answers. :)
– KaduAmaral