1

Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?

how to insert into mysql database, if records already exists, then update...I know there is a solution on this page: http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

sib_table

+=======================================================+
| sib_id | std_id  |  sib_name  |  sib_sex  |  sib_age  |
+=======================================================+
| 1      | 77      |  Sajjad    |   m       | 5/17/1990 |
| 1      | 77      |  Farah     |   f       | 9/14/1980 |
| 1      | 77      |  Bilal     |   m       | 1/10/1995 |
+=======================================================+

What sql would be if I want to add anther sibling to this table.

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES 
  ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;
0

3 Answers 3

3

You're close, but you need to treat the ON DUPLICATE KEY clause just like an UPDATE statement. That means you need to setup a unique key so that trying to do:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  

... will only work once. Then you add the ON DUPLICATE KEY UPDATE clause to change the rest of the fields (i.e. the ones that aren't part of the key) to match.

So, for example, assuming I read the table structure correctly, if you put a unique composite key on the columns std_id and sib_name, this will ensure that you can't add two siblings of the same name. And that means that when you go to add another like this:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE sib_sex = 'f', sib_age = '1/27/2005'

... it will do one of two things:

  1. add the new row if Aamna doesn't exist in family #77.
  2. or update Aamna's sex and birthday if she's been added before.

This structure is more powerful than MySQL's REPLACE because it lets you do something different to the conflicting row than merely overwrite it with what you tried to insert. Of course, most of the time the functionality of REPLACE is what is actually wanted. But it is better to know the more generic statement.

3
  • ..Thank you very much for making me understand this query.. but how does this query knows... that its' only name that we don't need duplicate... ... i think, if we update "name and sex" then duplication will be bound with age... right... ??? actually sib_id was to be.. 1, 2, 3 ... but wrote by mistake (1,1,1)
    – MFarooqi
    Commented May 10, 2011 at 15:44
  • this method is only adding... not updating
    – MFarooqi
    Commented May 10, 2011 at 16:27
  • If you do ALTER TABLE sib_table ADD UNIQUE KEY (std_id, sib_name) then you can't add a second sibling of the same name under the same std_id. Then the SQL I provided will update correctly.
    – staticsan
    Commented May 11, 2011 at 1:15
1

Try the following:

REPLACE INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')
0

check number of rows in a case:

SELECT * where ...

if (number of rows == 0) {
   INSERT ...
} else {
   Update ...
}
2
  • but i want the "on duplicate key " feature
    – MFarooqi
    Commented May 8, 2011 at 22:43
  • @jobsz.. in fact i want to learn the above INSERT INTO WITH UPDATES statement... can you please help me in that...
    – MFarooqi
    Commented May 8, 2011 at 23:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.