INSERT ... ON DUPLICATE KEY UPDATE
INSERT ... ON DUPLICATE KEY UPDATE is a SQL statement in the DML Statements category. INSERTs a row, but if a unique key conflict occurs, UPDATEs the existing row (MySQL "upsert"). The syntax is INSERT INTO table (columns) VALUES (values) ON DUPLICATE KEY UPDATE column=value;. It returns rows affected. A typical example: INSERT INTO users (id, name, count) VALUES (1, 'Alice', 1) ON DUPLICATE KEY UPDATE count = count + 1; -- If user exists, increment count instead of error A close relative is INSERT INTO, which inserts new rows into a table. A close relative is UPDATE, which modifies existing rows in a table. Always use WHERE to avoid updating all rows. A close relative is DELETE, which removes rows from a table. Always use WHERE to avoid deleting all rows. More about this category: Data Manipulation Language — INSERT, UPDATE, DELETE, REPLACE, MERGE. Related DML Statements entries: INSERT INTO, UPDATE, DELETE, REPLACE INTO, MERGE, INSERT INTO SELECT, UPSERT (PostgreSQL), DELETE with JOIN.