Inserting into a mysql table and overwritng any current data

MySQL has a "INSERT ... ON DUPLICATE KEY UPDATE" command. You can find it here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

INSERT INTO `table` VALUES ('a', 'b') ON DUPLICATE KEY UPDATE `field1`='a', `field2`='b'

You can use REPLACE INTO in MySQL to do this.

REPLACE INTO table
SET name = 'Treffynnon'

Just a little cheatsheet.
Mysql has 3 different scenarios for handling unique key duplicates:

If you want to...

  • do nothing - use INSERT IGNORE
  • delete existing and create new - use REPLACE INTO
  • update existing - use ON DUPLICATE UPDATE

Tags:

Mysql