MySQL ‘UPDATE ON DUPLICATE KEY’ without a unique column?

You can create a PRIMARY or UNIQUE key out of multiple columns (called a composite key) in MySQL, which’ll allow ON DUPLICATE KEY to work just fine.

// create a composite index
CREATE INDEX my_composite_index ON my_table (column1, column2);

// insert or update
INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2') ON DUPLICATE KEY UPDATE column3=column3+1;

Leave a Comment