ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE is a SQL statement in the MySQL Specific category. MySQL extension: if INSERT causes a duplicate key error, it performs an UPDATE instead. The syntax is INSERT INTO table VALUES (...) ON DUPLICATE KEY UPDATE col=value;. It returns rows affected. A typical example: INSERT INTO counters (page, views) VALUES ('home', 1) ON DUPLICATE KEY UPDATE views = views + 1; -- If 'home' exists, increment views; otherwise insert new A close relative is AUTO_INCREMENT, which mySQL auto-generates sequential integer values for this column. Commonly used with PRIMARY KEY. A close relative is ENGINE, which specifies the MySQL storage engine. InnoDB (default) supports transactions and foreign keys. MyISAM is faster for read-heavy. A close relative is LAST_INSERT_ID(), which returns the AUTO_INCREMENT value generated by the most recent INSERT in the current connection. More about this category: MySQL-specific features — AUTO_INCREMENT, ENGINE, SHOW TABLES, LIMIT.