SQLite "INSERT OR REPLACE INTO" vs. "UPDATE ... WHERE"

nevan king picture nevan king · Feb 12, 2010 · Viewed 123k times · Source

I've never seen the syntax INSERT OR REPLACE INTO names (id, name) VALUES (1, "John") used in SQL before, and I was wondering why it's better than UPDATE names SET name = "John" WHERE id = 1. Is there any good reason to use one over the other. Is this syntax specific to SQLite?

Answer

Adriaan Stander picture Adriaan Stander · Feb 12, 2010

UPDATE will not do anything if the row does not exist.

Where as the INSERT OR REPLACE would insert if the row does not exist, or replace the values if it does.