0

I am having a small problem- How would I write a function in mysql to insert a record if it doesn't exist or if its a case where it exists then say for example the record is updated a fresh copy of the default record should be reinserted leaving the updated record in the table. Let me give a small scenario, please read carefully before bashing me, I'm trying to explain as best as I can:

table name= Computer.
Columns(2) = comp_id(Auto-increment), comp_name. 

Default Data I'd want to be in the table=

comp_id= 1, comp_name='Dell'

Now if I should Update this information to:

comp_name='HP'

The function should automatically add the previous record which was:

comp_name='Dell'

I thought of using update if not exist but I was reading a blog and they stated Mysql does not support update if not exist. If you could provide any suggestions I'd really appreciate, Thanks.

dames
  • 1,421
  • 8
  • 35
  • 55
  • 2
    possible duplicate of [How do I update if exists, insert if not (aka upsert or merge) in MySQL?](http://stackoverflow.com/questions/1218905/how-do-i-update-if-exists-insert-if-not-aka-upsert-or-merge-in-mysql) – Mike Christensen Aug 24 '12 at 00:06

1 Answers1

7

You want INSERT ON DUPLICATE UPDATE in mysql.

http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html

Cargo23
  • 3,064
  • 16
  • 25