Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting into a mysql table and overwritng any current data

Tags:

mysql

I am inserting some data into a table, but it occasionally clashes with other data in the table (ie. it has the same primary key).
I would like to be able to just overwrite this data if it is there, instead of having mysql send me an error message saying that there is a duplicate primary key. I know that I can just delete these values beforehand, but it would take a somewhat large query.
Is it possible to overwrite these values and suppress any warnings, or am I forced to remove these values?

like image 263
Jonathon Vandezande Avatar asked Aug 26 '11 15:08

Jonathon Vandezande


1 Answers

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
like image 62
Your Common Sense Avatar answered Nov 16 '22 02:11

Your Common Sense