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 6969827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:37:51+00:00 2026-05-27T16:37:51+00:00

A standard problem in applications is to insert a record if one doesn’t exist

  • 0

A standard problem in applications is to insert a record if one doesn’t exist – or update if it does. In cases where the PRIMARY KEY is unknown this is usally solved by issuing a SELECT and then running either an INSERT or UPDATE if the record was found.

However, there seems to be at least three ways I know of that you can insert a record into a database even when a record already exists. Personally, I would rather drop the new insert request if one already exists, but then there might be cases where you would rather drop the record in the database and use the new one.

CREATE TABLE IF NOT EXISTS `table` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `foo` int(10) unsigned NOT NULL,
  `bar` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `row` (`foo`,`bar`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Here are the three methods:

  1. INSERT IGNORE INTO table (foo, bar) VALUES (2,3);

  2. INSERT INTO table (foo, bar) VALUES (2,3) ON DUPLICATE KEY UPDATE;

  3. REPLACE INTO table (foo, bar) VALUES (2,3);

At what times should each of these methods be used?
Can someone give some examples of correct usage scenarios?

  • 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-27T16:37:51+00:00Added an answer on May 27, 2026 at 4:37 pm
    • INSERT should be used when you just want to insert a new row

      Lets say you are storing log entries, you’ll want to log every event, use INSERT.


    • INSERT IGNORE should be used when you just want there to be a specific key exists in the table, it doesn’t matter if it’s the current insert that creates it, or if it’s already present.

      Let’s say you have a table of phone-numbers and the number of uses, you find a new phone number that you are not sure exists in the table, but you want it to be there.

      You use INSERT IGNORE to make sure that it’s there.


    • REPLACE INTO should be used when you want to make sure that a specific key exists in the table, if it exists you’d like the new values to be used, instead of that present.

      You have another table with phone-numbers, this time you find a new number and a name to associate it with.

      You use REPLACE INTO to find and update a complete record, or just insert the new information.


    • INSERT INTO ... ON DUPLICATE KEY UPDATE ...

      Please not that this is not an alternative method of writing REPLACE INTO, the above should be used whenever you’d like to make sure that a specific key exists, but if it does update some of the columns, not all of them.

      For example if you are storing the numbers of visits from a certain IP, and the first page the user ever visited.

      INSERT INTO visitors (ip,visits,first_page) VALUES (<ip>,1,<current_page>) ON DUPLICATE KEY visits = visits +1;

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

Sidebar

Related Questions

I have the following problem that the standard library doesn't solve well, and I'm
Can standard pointers in .Net do this? Or does one need to resort to
we've got a problem with one of our non dpi aware MFC applications. If
I guess, the following is a standard problem on every school or university: It
I'm having a weird problem with index organized table. I'm running Oracle 11g standard.
In standard ASP.net applications ASP.net offered some protection from XSS attacks with validateRequest throwing
Since many years a GUI-standard are the menu-bars of applications with menus popping up,
I need to build several Rails applications for one client. I would like to
I am developing a Cocoa application and encountered a problem with highlighting. Standard highlighting
I have three tables objects , (primary key object_ID ) flags (primary key flag_ID

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.