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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:06:07+00:00 2026-05-16T04:06:07+00:00

Consider the following table: +————-+——————+——+—–+———+—————-+ | Field | Type | Null | Key |

  • 0

Consider the following table:

+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| vendor_id   | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| vendor_name | varchar(100)     | NO   | UNI | NULL    |                |
| count       | int(10) unsigned | NO   |     | 1       |                |
+-------------+------------------+------+-----+---------+----------------+

I have the following MySQL query:

INSERT INTO `table` 
   (`vendor_name`) 
VALUES 
   ('foobar') ON DUPLICATE KEY UPDATE `count` = `count` + 1

The intent of this query is to insert a new vendor name to the table and in case the vendor name already exists, the column count should be incremented by 1. This works however the primary key of the current column will also be auto-incremented. How can I prevent MySQL from auto-incrementing the primary key in these cases? Is there a way to do this with one query?

Thank you.

  • 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-16T04:06:07+00:00Added an answer on May 16, 2026 at 4:06 am

    This works however the primary key of the current column will also be auto-incremented. How can I prevent MySQL from auto-incrementing the primary key in these cases?

    By using an UPDATE statement when the value already exists:

    IF EXISTS(SELECT NULL
                FROM TABLE
               WHERE vendor_name = $vendor_name) THEN
    
        UPDATE TABLE
           SET count = count + 1
         WHERE vendor_name = $vendor_name
    
    ELSE
    
        INSERT INTO TABLE
           (vendor_name)
        VALUES
           ($vendor_name
    
    END IF
    

    I tried the alternative to ON DUPLICATE KEY UPDATE, REPLACE INTO:

    REPLACE INTO vendors SET vendor_name = 'foobar', COUNT = COUNT + 1
    

    It updates the count, and the vendor_id so it’s worse…

    The database & data doesn’t care if the numbers aren’t sequential, only that the values are unique. If you can live with that, I’d use the ON DUPLICATE UPDATE syntax though I admit the behaviour is weird (understandable considering using an INSERT statement).

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

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can document stuff that doesnt exist in the code… May 16, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer readfile() reads a file and outputs it to STDOUT. I… May 16, 2026 at 8:56 am
  • Editorial Team
    Editorial Team added an answer Because you are adding literal quotes to the string. Instead… May 16, 2026 at 8:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type
Consider the following table: create table temp ( name int, a int, b int
Consider the following table: Song ---- SongID GUID Primary Key Name Varchar dateAdded DateTime
Consider the following table which has the fields - id (int) and date_created (datetime):
Consider the following table with a constraint that's a bit daft but simple enough
Consider the following table structure with data - AdjusterID | CompanyID | FirstName |
Consider the following table: <table> <thead> <tr> <th scope=col /> <th scope=col>A</th> <th scope=col>B</th>
Consider the following table structure: id name rank position ------------------------------- 1 Steve 5 0
Consider the following table structure: titles(titleID*, titleName) platforms(platformID*, platformName) products(platformID*, titleID*, releaseDate) publishers(publisherID*, publisherName)
Consider the following table.... hotel facilities 1 internet 1 swimming pool 1 wi-fi 1

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.