typestar

How a write can handle a conflict in SQL

Ignore it, replace the row, or update the parts you meant to update.

INSERT OR IGNORE INTO tags (name)
VALUES ('sql');

INSERT OR REPLACE INTO daily_counts (day, hits)
VALUES ('2026-07-29', 1);

INSERT INTO daily_counts (day, hits)
VALUES ('2026-07-29', 1)
ON CONFLICT (day)
DO UPDATE SET
    hits = daily_counts.hits + 1
WHERE daily_counts.hits < 1000;

How it works

  1. INSERT OR IGNORE skips the row that would violate a constraint.
  2. INSERT OR REPLACE deletes the old row first — including its other columns.
  3. ON CONFLICT DO UPDATE is the one that merges rather than clobbers.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
280
Tokens
66
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 42 seconds.

Type this snippet

Step 4 of 5 in Transactions, step 14 of 17 in Plans & performance.

← Previous Next →