views:

77

answers:

2

How to Check for record existence and do Insert or Update in MYSQL ?

I have a script, which has set of Insert statements for multiple tables. Now when i try to execute the Insert statement, i want to do the following:

  • Check for the record existence and then do Insert or Update.
  • If the Record is not exist do Insert.
  • If the Record is already do not do anything.

How to accomplish this ?

Note : The script with Insert Statements are generated programmaticaly using SP

A: 

This is what mysqls replace is for

http://dev.mysql.com/doc/refman/5.0/en/replace.html

Galen
A: 

Use INSERT IGNORE :

INSERT IGNORE INTO table_name
SET column = value
   ...
madgnome