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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:26:48+00:00 2026-06-14T05:26:48+00:00

Why MySQL does the insert row and then delete it if a rollback is

  • 0

Why MySQL does the insert row and then delete it if a rollback is called?

For e.g. with the following code:

declare exit handler for not found rollback;
declare exit handler for sqlwarning rollback;
declare exit handler for sqlexception

BEGIN
select last_insert_id();
rollback;
END;

START TRANSACTION;

INSERT INTO test (text) VALUES ('this_insert_works');
INSERT INTO test (id, text) VALUES (3,'this_insert_fails');

On the following code on a store procedure, if I execute it, and then I execute it again without the last line (it will work) I can see a “missing auto_increment ID” because the row was inserted and then, removed. The select returned returns an ID that doesn’t exists.

How can avoid this behavior?

  • 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-06-14T05:26:49+00:00Added an answer on June 14, 2026 at 5:26 am

    You can’t avoid this behavior.

    Say you have two concurrently-running transactions and they both insert some records into test inside of a transaction. What will their id values be? What should happen if you commit one but roll back the other?

    The only feasible option is to increment the table’s auto_increment counter; since each transaction can’t see each other’s rows (but the rows do exist inside of their respective transactions), this is the only way to ensure that they both get a unique id.

    While this behavior may appear undesirable, it’s actually there for a very good reason. The only other option would be to lock the entire table when doing an INSERT inside of a transaction, preventing every other connection from inserting rows into that table at the same time. This would be terrible for performance.

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

Sidebar

Related Questions

In the following SQL statement, SQLite sorts correctly but MySQL does not: However, if
What does MYSQL mean in the following code. require_once (MYSQL);
Is there an easy way to INSERT a row when it does not exist,
I have a method which does a simple mysql insert, when I tried to
I found a weird way of how mysql does something. Someone please explain why
I am trying to set up a MySQL trigger that does the following: When
Supposing I am inserting a row into a table in MySql by php code,
I am trying to setup a trigger so an insert does not occur if
Some code starts with selecting data then check if row numbers are 0 to
I need some help figuring out why the following scenario does not work. I'm

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.