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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:16:41+00:00 2026-05-24T06:16:41+00:00

$q = $db->prepare(‘INSERT IGNORE INTO Cities (Name,Stamp) VALUES (?,?)’); $q->execute(array($city,$stamp)); I am running this

  • 0
$q = $db->prepare('INSERT IGNORE INTO Cities (Name,Stamp) VALUES (?,?)');
$q->execute(array($city,$stamp));

I am running this code to insert new cities in the Cities table. I just noticed that every query increments the id field, and that’s not what I want. It’s not like I rely on the id values being gapless, but it’s ridiculous to increment every time the code is run.

So, what kind of smart SQL query can I run to give me the same functionality without doing two queries (first SELECT, then INSERT) and that does not auto_increment?

Table description:

    Field       Type         Null   Key Default Extra
    id          int(11)      NO     PRI NULL    auto_increment
    Name        varchar(100) YES    UNI NULL     
    countryid   int(11)      YES        NULL     
    Stamp       int(11)      YES        NULL     
    uid         int(11)      YES        NULL    

The code:

INSERT INTO Cities (Name,Stamp) VALUES (?,?) ON DUPLICATE KEY UPDATE Name = Name

increments the value on my table. The MySQL version is 5.5.9. The database engine is innoDB.


Possibly it can be this bug: http://bugs.mysql.com/bug.php?id=28781
If you scroll down to the bottom you can see that it’s still an issue, three years later…

  • 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-24T06:16:42+00:00Added an answer on May 24, 2026 at 6:16 am

    See Bug #42497: V5.1 AUTO_INCREMENT gaps with InnoDB when using INSERT IGNORE (which is not actually a bug).

    You either need to configure innodb_autoinc_lock_mode=0, or change your statement. For more information, see the related AUTO_INCREMENT Handling in InnoDB documentation.

    This is a summary of the behaviour that you are seeing. The new method for allocating auto-increment values in InnoDB was introduced with MySQL 5.1.22:

    For INSERT statements where the number of rows to be inserted is known
    at the beginning of processing the statement, InnoDB quickly allocates
    the required number of auto-increment values without taking any lock,
    but only if there is no concurrent session already holding the
    table-level AUTO-INC lock (because that other statement will be
    allocating auto-increment values one-by-one as it proceeds). More
    precisely, such an INSERT statement obtains auto-increment values
    under the control of a mutex (a light-weight lock) that is not held
    until the statement completes, but only for the duration of the
    allocation process.

    In short, MySQL is able to determine how many rows will be inserted by your statement, and pre-allocates the auto-increment value in advance. This prevents multiple concurrent statements from interleaving auto-increment values. However, once that number has been allocated, it cannot be used again. This introduces the possibility of gaps in auto-increment values.

    The following statement works for me, without incrementing the auto-increment value, and with innodb_autoinc_lock_mode = 1 (the default setting):

    INSERT INTO Cities (Name, Stamp)
    SELECT $Name, $Stamp
      FROM Cities
     WHERE NOT EXISTS (
        SELECT NULL
          FROM Cities
         WHERE Name = $Name
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so i want to do a prepared insert such as: prepare(INSERT INTO `analyzeditemsdetails`
$stmt = mysqli_prepare($con,INSERT INTO friend_request (ToUID, FromUID) VALUES (?,?)); mysqli_stmt_bind_param($stmt, ii, $fid, $uid); mysqli_stmt_execute($stmt);
I need to prepare for my exam and this is the probable question for
I have this partial code: if ($getRecords = $con->prepare(SELECT * FROM AUCTIONS WHERE ARTICLE_NO
We need to prepare part of our EAR for deployment, we usually do this
When running the following prepare statement for a SQLite3 db-selecct query I get a
Its a dumb question. Prepare yourself. My main.xml is this: <?xml version=1.0 encoding=utf-8?> <RelativeLayout
How to prepare an array of element for $.ajax submit? Here images return [val1,val2]
I use a prepare statement to execute an update statement. PreparedStatement stmt = null;
When querying using mysqli_stmt::prepare() and execute() , it will not return the result set.

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.