MySQL to many many. To delete or edit?

I have a Many-to-Many table in which I entered some form information. I recently made this form dynamic, so when the value of the input elements has changed, it is sent to the database via AJAX.

So my question is: Is it faster to try to find the values ​​that exist, edit them, create those that don't work, and delete the ones that are no longer in use OR Should I delete all the values ​​for id and insert all new ones?

In response to a comment, development.

  • A form containing about 10 fields. Some of them are required, some are not. Each time you access it, it generates a random identifier.
  • When the user begins to fill out the form, after the focus the element is lost, the entire form is submitted via AJAX, and all values ​​that are not empty are entered in many tables.
  • The table has three fields: identifier forms , item name , item value ;

The question is rephrased: Delete all records with the required form identifier or try to find them and change them?

+3
source share
2 answers

This will require less code to remove all existing relationships and add new ones.

  • Make sure you do this in a transaction
  • Handle errors correctly.

Less code == less errors, less developer time. So it is definitely faster.

+3
source

. , , , , .

:

INSERT INTO table (field1, field2) VALUES ('Value1', 'Value2')
ON DUPLICATE KEY UPDATE field1 = 'Value1'

- , / - , .

+1

Source: https://habr.com/ru/post/1788199/


All Articles