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?
views:
598answers:
2
+4
A:
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.
astander
2010-02-12 12:19:53
Thanks for the answer. Is this an SQLite specific thing?
nevan
2010-02-13 10:11:12
I am not ware of this inSql server, although Sql Server now has a MERGE command. And rom MS Access I havent seen anything like this either.
astander
2010-02-13 10:14:02
A:
The insert or replace query would insert a new record if id=1 does not already exist.
The update query would only oudate id=1 if it aready exist, it would not create a new record if it didn't exist.
Anonym
2010-02-12 12:21:28
I don't think this is right. The sqlite documentation (as of 1st July 2010, version 3.6.23.1) says that `REPLACE is an alias for INSERT OR REPLACE`, and would hence have the same behaviour. In my experience this is the case..
fostandy
2010-06-30 16:36:21