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

  • Home
  • SEARCH
  • 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 8841623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:45:56+00:00 2026-06-14T10:45:56+00:00

I want to execute the following two MySQL statements: 1) SELECT * FROM table1

  • 0

I want to execute the following two MySQL statements:

1) SELECT * FROM table1 WHERE field1=val1 FOR UPDATE;
2) UPDATE table1 SET field2=val2 WHERE field1=val1;

It is important that the second statement exactly changes the rows returned by the first statement (no additional rows and no one less). Therefore I execute the transactions with auto_commit=false and use the “for update” version of the select statement.

“For Update” locks all rows it returns, so they are in the original state when the second statement is executed. But what about insertions? Is it possible that another thread inserts a new row with field1=val1 inbetween, which then gets changed by the second statement?

And another question: Does it make a difference if the second statement doesn’t changes the rows itself but does something like the following?

3) INSERT INTO table2 (SELECT * FROM table1 WHERE field1=val1)

If (3) is in the same transaction as (1), is then ensured that both selects return exactly the same elements?

edit:

I’m using InnoDB and I read some stuff about next key locking and gap locking.
As far as I understood it, while executing (1), InnoDB would not only lock the selected rows but also the accessed indices.

So am I right to say that this problem doesn’t occur if I have an index over column “field1”? What if there is no index for it? Is it different then?

  • 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-14T10:45:58+00:00Added an answer on June 14, 2026 at 10:45 am

    I can’t speak to the mySQL case completely, but I rather doubt it. The only thing that SELECT FROM ... FOR UPDATE (appears) to do is preventing other transactions from modifying the given set of rows.
    It doesn’t restrict future statements – if another row is inserted (by another transaction) before that UPDATE statement runs, it’ll be updated as well.
    Needless to say, the third statement you’ve given will also fall prey to the same issue.

    What are you attempting to actually do here? It’s possible there’s another way to accomplish this. For example, if there’s some sort of ‘insertedAt’ timestamp, you could probably just add that as an extra condition.

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

Sidebar

Related Questions

I want to execute the following two functions at exactly the same time. from
I want to execute python code from C# with following code. static void Main(string[]
I want to execute one mysql statement where I have a select that is
I want to build a shell script to execute the following two commands, but
I have two tables in my SQL database: mysql> select *from crop; +------+-----------+----------+ |
I am trying to execute the following statement: DELETE a FROM db.table1 a INNER
I want the following code to execute when the myString is having either of
I want to execute a code helloword.cpp which takes in some argument from console
I want to write the following two lines on the console: x = 3
I want to create one MSBuild that will execute two others.. How can i

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.