Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608884
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:29+00:00 2026-05-22T00:54:29+00:00

Possible Duplicate: How do I update if exists, insert if not (aka upsert or

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:54:30+00:00Added an answer on May 22, 2026 at 12:54 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do I update if exists, insert if not (aka upsert or
Is it possible to INSERT...ON DUPLICATE KEY UPDATE into a table on the current
Possible Duplicate: Data not writing out to Database I'm trying to update a bit
Right now I have: INSERT INTO mytable (a,b,c,d) VALUES(1,2,3,4) ON DUPLICATE KEY UPDATE c=VALUES(c),d=VALUES(d)
Possible Duplicate: update django database to reflect changes in existing models I've used Django
Possible Duplicate: How to update a MySql DB using Flex I want to connect
Possible Duplicate: PHP & MySQL Pagination Update Help So, in my site, there are
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Is it possible to decrypt md5 hashes? i used md5.new() ; md5.update(aaa)
Possible Duplicate: Cross-thread operation not valid: Control accessed from a thread other than the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.