Part 48: The Upsert Pattern in MySQL: ON DUPLICATE KEY UPDATE

Part 48 Technology Updated September 12, 2026

Part 48: The Upsert Pattern in MySQL: ON DUPLICATE KEY UPDATE

An Upsert inserts a row if it doesn't exist, or updates it if a duplicate unique key is encountered.


1. MySQL Syntax

INSERT INTO page_views (page_url, view_count)
VALUES ('/blog/react-tutorial', 1)
ON DUPLICATE KEY UPDATE view_count = view_count + 1;

Key Takeaways

  • Eliminates race conditions by performing insert-or-update atomically in a single statement.

In Part 49, we explore The Upsert Pattern in PostgreSQL: ON CONFLICT DO UPDATE!