Setting a unique MySQL key or checking for duplicate in the application part?

Which one is more reliable and has better performance? Installing a unique MySQL key and using INSERT IGNORE or the first check for data in the database and actions according to the result?

If the answer is second, is there a way to make one SQL query instead of two?

UPDATE: I ask because my colleagues from the company I work with believe that the solution to such issues should be carried out in the part of the application that is more reliable in accordance with them.

0
source share
3 answers

No duplication will be detected in the application .

, , . , , .

- , . , , script -

, . , , , NULL/NIOT NULL, - .

- , .

. programers.se: - ? (SO)

+3

. mysql, , , , .

+2

MySQL, , .

, ignore , , .

.. , , , , .

On the other hand, if there is some kind of decision branch based on whether the change is an update or a new value, I think you will have to go with option 2.

those. If changes to the table are recorded in some other table (for example, table: change_log with columns: id, table, column, old_val, new_val), you cannot just use INSERT IGNORE because you can never determine what values ​​were changed or updated.

+1
source

All Articles