I have an asp.net web app where a user can update a table in a database. I would like to know the proper method of doing this. As you can see in the image below, the red shaded area is the data that I have in common that I do not need to update.

Here are the steps that I need to take:
- Don’t update data if it exists
- Delete data that is not in the user update
- Add data from the user update that is not already in the table
I need to audit this table also, so I will have a trigger on it. I would like to know in theory what is the best way to do this kind of update to a table?
thank you to Aaron who introduced the concept of merge.
Here is what the table data looks like:
foreignkey model primarykey
1 AA 1
1 AA1 2
1 AA3 3
23 B 4
22 C 5
The user will be adding data that looks like this:
foreignkey model primarykey
1 A1 1
1 AA1 2
1 AA3 3
22 C 5
As you can see, the first row was updated, and the second to last row was deleted entirely. Can you please help me with this merge statement?
Yes, it’s possible and it’s called a merge. See the documentation for the SQL Server MERGE Statement.
If you don’t want to perform any updates if the data already exists, then just don’t add a
WHEN MATCHEDclause. USEWHEN NOT MATCHED BY SOURCEandWHEN NOT MATCHED BY TARGETto insert/delete the missing/removed rows.