Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does sql update do a delete then insert?

How exactly does Sql server perform an update operation? Does it do a in place update? or it does a delete then insert for each of the rows affacted? I understand that an update operation is similar to that of delete then insert for the old value is stored in the deleted and new value is stored to the inserted...

I was told the update operation is accomplished by deleting then inserting, but I am not sure why it would be implemented as so. Please enlighten me.

like image 499
junli antolovich Avatar asked Apr 21 '15 22:04

junli antolovich


People also ask

Does update in SQL overwrite?

If you tell SQL Server to UPDATE some rows - it will update those rows. SQL Server doesn't do any "matching" of its own. That's up to you - you control the WHERE clause for the UPDATE . If you want to avoid updating some rows - make sure the WHERE clause excludes them.

Can we insert using update in SQL?

You can use INSERT OR UPDATE with a SELECT to populate a table with existing data extracted from other tables. This statement inserts the Name row from the Sample. Customer table into the Sample.

What are insert update and delete called?

INSERT , UPDATE , and DELETE , as well as SELECT and MERGE, are known as Data Manipulation Language (DML) statements, which let SQL users view and manage data.

What does the update SQL clause do?

The UPDATE statement is used to modify the existing records in a table.


2 Answers

MSDN Update section:

The Database Engine converts a partial update to a full update when the UPDATE statement causes either of these actions:

  • Changes a key column of the partitioned view or table.

  • Modifies more than one row and also updates the key of a nonunique clustered index to a nonconstant value.

And partial update

Partial-update transaction directly writes a character string at a user-defined position of a table column without issuing a delete and replace command, as would happen in a full update.

Partial update does not support multibyte character set conversion. Partial update support is restricted to Microsoft SQL Server.

I did some searching and it looks like the answer to your question depends on what you are updateing.

Hope this helps.

like image 110
Teis Lindemark Avatar answered Nov 03 '22 06:11

Teis Lindemark


Depends what you mean. If you're asking if an delete and insert are similar to an update in performance, the answer would be no. People have already asked that question. They say Update is faster because of it must only be logged once and they might have listed other reasons but I didn't read on. In SQL, is UPDATE always faster than DELETE+INSERT?

In relation to a trigger that fires after an update statement, you can access the "deleted" and "inserted" tables which will contain the old and new row respectively. So it may seem like it deletes then updates.

For me I don't really care what happens in the background. My best guess is that SQL Server simply changes the old value to the new value because that's probably the most efficient. But either way, just choose whichever performs the best.

like image 2
Stephan Avatar answered Nov 03 '22 05:11

Stephan