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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:27:47+00:00 2026-06-18T07:27:47+00:00

I am inserting a row in two different tables depending upon a condition. Let

  • 0

I am inserting a row in two different tables depending upon a condition.

Let say, we have table named stories with 2 fields under consideration:
(story_id and comments_count)

and 2 other tables:

  1. regcomments (comment_id, story_id, comment_text)
  2. unregcomments (comment_id, story_id, comment_text)

Now, before inserting any new comment for any story_id, I just want to check if comments_count is less than 100. If so, the row should be inserted into regcomments, otherwise, the row should be inserted into unregcomments.

Here is the pseudo-code of how I’ve done that:

select comments_count from stories where story_id = x
if comment_count<100
    insert into regcomments
else
    insert into unregcomments

Now all what I am concerned about is if multiple users are accessing this script, Is there any chance this will fail? For example a story has 99 comments. 2 users call this function exactly at the same time. The first select query executes at the same time for both, and they both get 99. so both inserts will be made to regcomments, making count of regcomments 101, which is against my business rule. Any one has any idea, how the thing go. Or any ideas about how should I handle this scenario. (Don’t ask me why I am using 2 tables 😛 anyway)

  • 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-18T07:27:49+00:00Added an answer on June 18, 2026 at 7:27 am

    Yes, it can/will fail because 10 request may all receive 99 comments_count from the SQL query, and each time new row will be inserted to regcomments, and then you’ll have 109 rows in the table.

    One possible solution would be to move the checking logic into the SQL (use stored procedure with locking/mutex).

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

Sidebar

Related Questions

I have two database tables. One table stores data for a commitment that a
I have two SQL tables, each with a compound key made of three different
I am inserting into two tables. Into the first table goes a new 'item'.
lets say, i have two tables, one for object records and one for activity
When inserting a row into a table with an auto increment primary key column,
I want to make sure I'm not inserting a duplicate row into my table
How can I SELECT the last row in a MySQL table? I'm INSERTING data
First i gave all values of row in JTAble. While inserting table values into
I have two tables with same schema , and primary key as noc for
I have a table that contains two not null columns Created and Updated .

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.