← Back to SQL, page 10

sql-0189 · Hard senior

Why can SQLite's INSERT OR REPLACE lose data compared with ON CONFLICT DO UPDATE?

INSERT OR REPLACE deletes the conflicting row and inserts a new one. Columns not listed in the INSERT fall back to their defaults or NULL, ON DELETE actions on child rows fire if foreign keys are on, and delete triggers run when recursive triggers are enabled. ON CONFLICT DO UPDATE changes only the named columns of the existing row.

Full answer
sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
INSERT INTO users VALUES (1, 'Ana', 'ana@example.com');
INSERT OR REPLACE INTO users (id, name) VALUES (1, 'Ana R');
SELECT * FROM users;
idnameemail
1Ana RNULL

The REPLACE wiped email, because the new row did not include it. With foreign keys enabled, the delete also cascades:

sql
PRAGMA foreign_keys = ON;
CREATE TABLE u2 (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE notes (user_id INTEGER REFERENCES u2(id) ON DELETE CASCADE, body TEXT);
INSERT INTO u2 VALUES (1, 'Ana');
INSERT INTO notes VALUES (1, 'hello');
INSERT OR REPLACE INTO u2 VALUES (1, 'Ana R');
SELECT (SELECT COUNT(*) FROM notes) AS notes_left, (SELECT name FROM u2) AS name;
notes_leftname
0Ana R

The note attached to user 1 was deleted, even though "user 1" still exists afterwards. The upsert keeps both:

sql
PRAGMA foreign_keys = ON;
CREATE TABLE u3 (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
CREATE TABLE notes3 (user_id INTEGER REFERENCES u3(id) ON DELETE CASCADE, body TEXT);
INSERT INTO u3 VALUES (1, 'Ana', 'ana@example.com');
INSERT INTO notes3 VALUES (1, 'hello');
INSERT INTO u3 (id, name) VALUES (1, 'Ana R') ON CONFLICT (id) DO UPDATE SET name = excluded.name;
SELECT u.name, u.email, (SELECT COUNT(*) FROM notes3) AS notes_left FROM u3 u;
nameemailnotes_left
Ana Rana@example.com1

REPLACE is fine for cache-like tables where the new row fully defines the old one. For anything with references or partial updates, use ON CONFLICT DO UPDATE.

sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
INSERT INTO users VALUES (1, 'Ana', 'ana@example.com');
INSERT OR REPLACE INTO users (id, name) VALUES (1, 'Ana R');
SELECT * FROM users;

PRAGMA foreign_keys = ON;
CREATE TABLE u2 (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE notes (user_id INTEGER REFERENCES u2(id) ON DELETE CASCADE, body TEXT);
INSERT INTO u2 VALUES (1, 'Ana');
INSERT INTO notes VALUES (1, 'hello');
INSERT OR REPLACE INTO u2 VALUES (1, 'Ana R');
SELECT (SELECT COUNT(*) FROM notes) AS notes_left, (SELECT name FROM u2) AS name;

PRAGMA foreign_keys = ON;
CREATE TABLE u3 (id INTEGER PRIMARY KEY, name TEXT, email TEXT);
CREATE TABLE notes3 (user_id INTEGER REFERENCES u3(id) ON DELETE CASCADE, body TEXT);
INSERT INTO u3 VALUES (1, 'Ana', 'ana@example.com');
INSERT INTO notes3 VALUES (1, 'hello');
INSERT INTO u3 (id, name) VALUES (1, 'Ana R') ON CONFLICT (id) DO UPDATE SET name = excluded.name;
SELECT u.name, u.email, (SELECT COUNT(*) FROM notes3) AS notes_left FROM u3 u;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • REPLACE is delete plus insert
  • Unlisted columns lose their values
  • Delete actions on child rows can fire
  • ON CONFLICT DO UPDATE changes only named columns

Common mistakes

  • Using REPLACE for partial updates
  • Losing child rows through cascades

Follow-ups

  • When is REPLACE acceptable?
  • What does REPLACE do to a rowid?

Tips

  • Show the lost column concretely