How to Upsert in MySQL (INSERT ... ON DUPLICATE KEY UPDATE)
Last updated July 24, 2026 · By the SaturnSQL team
INSERT ... ON DUPLICATE KEY UPDATE inserts a row, or updates it when a primary or unique key already matches. REPLACE INTO also exists but deletes and reinserts, firing triggers and resetting defaults.
INSERT INTO customers (id, email, updated_at)
VALUES (101, '[email protected]', NOW())
ON DUPLICATE KEY UPDATE
email = VALUES(email),
updated_at = NOW();On MySQL 8.0.19+ the VALUES() function is deprecated in this position; use a row alias instead: INSERT ... VALUES (...) AS new ON DUPLICATE KEY UPDATE email = new.email.
Run this in SaturnSQL
SaturnSQL is a browser-based SQL editor for teams: shared query library, schema-aware autocomplete, and scheduled exports to Google Sheets and Slack.
Try it free