Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REPLACE versus INSERT in SQL

Tags:

replace

sql

mysql

I am doing the following SQL tutorial: http://sql.learncodethehardway.org/book/ex11.html

and in this exercise the author says in the second paragraph:

In this situation, I want to replace my record with another guy but keep the unique id. Problem is I'd have to either do a DELETE/INSERT in a transaction to make it atomic, or I'd need to do a full UPDATE.

Could anyone explain to me what the problem is with doing an UPDATE, and when we might choose REPLACE instead of UPDATE?

The UPDATE code:

UPDATE person SET first_name = "Frank", last_name = "Smith", age = 100
    WHERE id = 0;

Here is the REPLACE code:

REPLACE INTO person (id, first_name, last_name, age)
    VALUES (0, 'Frank', 'Smith', 100);

EDIT: I guess another question I have is why would you ever do a DELETE/INSERT instead of just an UPDATE as is discussed in the quoted section?

like image 942
jcm Avatar asked Apr 09 '15 13:04

jcm


People also ask

What does replace () do in SQL?

The REPLACE() function replaces all occurrences of a substring within a string, with a new substring.

What is difference between update and replace in SQL?

The REPLACE policy will create the record or replace it entirely if a previous version of the record already existed. This will delete potential already existing bins for this record. The UPDATE policy will create the record or update the record if it already exists.

How do you replace a record in SQL?

The Replace statement is used to replace all occurrences of a specified string value with another string value. The Replace statement inserts or replaces values in a table. Use the Replace statement to insert new rows in a table and/or replace existing rows in a table.

What is the difference between insert and update in SQL?

The main difference between INSERT and UPDATE in SQL is that INSERT is used to add new records to the table while UPDATE is used to modify the existing records in the table.


3 Answers

According to the documentation, the difference is:

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

So what it does:

  • Try to match the row using one of the available indexes;
  • If the row doesn't exist already: add a new one;
  • If the row exists already: delete the existing row and add a new one afterwards.

When might using this become useful over separate insert and update statements?

  • You can safely call this, and you don't have to worry about existing rows (one statement vs. two);
  • If you want related data to be removed when inserting / updating, you can use replace: it deletes all related data too);
  • When triggers need to fire, and you expect an insert (bad reason, okay).
like image 200
Patrick Hofman Avatar answered Oct 19 '22 19:10

Patrick Hofman


First Replace isn't widely understood in all database engines.

Second replace inserts/updates a record based on the primary key. While with update you can specify more elaborate conditions:

UPDATE person SET first_name = 'old ' + first_name WHERE age > 50

Also UPDATE won't create records.

like image 7
FooLman Avatar answered Oct 19 '22 17:10

FooLman


UPDATE will have no effect if the row does not exist.

Where as the INSERT or REPLACE will insert if the row doesn't exists or replace the values if it does.

like image 2
Stanislovas Kalašnikovas Avatar answered Oct 19 '22 18:10

Stanislovas Kalašnikovas