MERGE and Upsert in SQL Server and MySQL: Insert or Update Safely
How to insert a row if it is new and update it if it exists: the race condition in IF EXISTS, safe single-row and whole-table upserts, MERGE and its traps, and INSERT … ON DUPLICATE KEY UPDATE in MySQL.









