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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:22:24+00:00 2026-06-18T01:22:24+00:00

I’m developing a stock control database and wish to know what would be the

  • 0

I’m developing a stock control database and wish to know what would be the best way to do relationship between products and stocks entities.

Which of below is appropriate? If none, please let me know why and what is your suggestion.

Thanks in advance

PRODUCTS – STOCKS

Based on above entities:

  • If I have 1:1 relationship, looks like I’ll have to reduce stock
    amount in same row by updating the row with UPDATE statement.

  • If I have 1:n relationship, looks like I’ll have to add a new row
    with updated stock amount in it with INSERT statement.

IMPORTANT POINTS

  • There will be concurrent users to manipulate stock like increasing and/or reducing so [ACID][1] is important point.

  • Stock amount is non-negative value.

  • 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-18T01:22:25+00:00Added an answer on June 18, 2026 at 1:22 am

    Depends on what you are trying to accomplish.

    If you need a simple number that describes the available quantity of a product, just store it as a field in the product table. Updating it in a single statement is atomic:

    UPDATE PRODUCT
    SET AMOUNT = AMOUNT + <change>
    WHERE PRODUCT_ID = <whatever>
    

    Alternatively, you could do something like this:

    START TRANSACTION;
    
    SELECT AMOUNT
    FROM PRODUCT
    WHERE PRODUCT_ID = <whatever>
    FOR UPDATE;
    
    (Calculate new amount.)
    
    UPDATE PRODUCT
    SET AMOUNT = <new amount>
    WHERE PRODUCT_ID = <whatever>;
    
    COMMIT;
    

    Note the FOR UPDATE which locks the row until the COMMIT and prevents the anomalies that would otherwise be possible in a concurrent environment.

    Without FOR UPDATE, just the regular ACID guarantees are not enough. For example:

    • The transaction A SELECTs the current AMOUNT, let’s say it’s 5.
    • The transaction B SELECTs the current AMOUNT, which is still 5 because no other transaction has committed any change yet.
    • The transaction A adds 2 to the amount, resulting in 7.
    • The transaction B adds 3 to the amount, resulting in 8.
    • The transaction A UPDATEs the row to 7 and commits.
    • The transaction B UPDATEs the row to 8 and commits.

    Suddenly, the resulting AMOUNT in the table is 5 + 3 = 8, even though it should have been 5 + 2 + 3 = 10. One of the changes is lost – whoever commits last wins.


    If you need a history of product amounts, or need to track individual “instances” of products, then yes, you’d need a separate table in a 1:N relationship, which may have its own locking challenges in the concurrent environment.


    Stock amount is non-negative value.

    Unfortunately, MySQL doesn’t enforce CHECK constraints, but for this specific case, you can simply use one of the UNSIGNED data types.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.