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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:28:59+00:00 2026-05-29T22:28:59+00:00

I have a simple power system as users can borrow to each other: mysql_query(INSERT

  • 0

I have a simple power system as users can borrow to each other:

mysql_query("INSERT INTO power (sender, receiver, amount)
    VALUES ('$sender', '$receiver', '$amount')");
mysql_query("UPDATE users SET power=power-$amount WHERE user_id='$sender'");
mysql_query("UPDATE users SET power=power+$amount WHERE user_id='$receiver'");

I check if the sender has enough credit to transfer by a SELECT query before running the above-mentioned set of queries.

Issues 1: I think, it is better to put all these four queries into a Transaction; as InnoDB ACID will guarantee a safer performance. What will be the best transaction to do so?

Issue 2: power is unsigned int. If by any chance (even unlikely), user does not have enough credit power-$amount will not set 0; instead it will be cycled through the largest value of int(), which is 4294967295. This means that the user will be granted almost unlimited power (credit).

  • 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-29T22:29:02+00:00Added an answer on May 29, 2026 at 10:29 pm

    First of all, don’t bother with this:

    I check if the sender has enough credit to transfer by a SELECT query before running the above-mentioned set of queries.

    That leaves you open to a race condition that you’ll have to deal with anyway. Instead, set up constraints in your database to make it impossible to get into an invalid state.

    Issue 2: power is unsigned int.

    Don’t do that either, there’s no need. A 4 byte signed integer should have more than enough space on the positive side; if it doesn’t, you can switch to a signed 8 byte integer. The reason you want a signed value is that it makes integrity checking fairly easy: if a balance drops below zero then something is wrong. If you use an unsigned value, you’d have to reserve 4294967295-n (for some n) to detect underflow and overflow instead of a simple < 0.

    As far as constraining your data is concerned, normally you’d use a CHECK constraint like this:

    check (power >= 0)
    

    but MySQL doesn’t support CHECK constraints. However, you can write a BEFORE INSERT OR UPDATE trigger that can check that new.power >= 0 and raise an exception if it isn’t.

    Now you have a users table that doesn’t allow invalid power values so we’re done with issue 2.

    On to issue 1: transactions. Yes, you absolutely do want to use a transaction for the transfer. You’ll want a sequence like this:

    1. start transaction
    2. INSERT INTO power (sender, receiver, amount) VALUES ('$sender', '$receiver', '$amount')
    3. UPDATE users SET power=power-$amount WHERE user_id='$sender'
    4. UPDATE users SET power=power+$amount WHERE user_id='$receiver'
    5. Have there been any errors from the “check trigger”?
      • If there have then send a rollback to the database and tell the user what went wrong.
      • If there haven’t then send a commit to the database.

    The transaction will ensure that all three operations will either succeed or fail as a single unit and the CHECK constraint (implemented as a trigger) ensures that no one can give away more power than they have.

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

Sidebar

Related Questions

I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I am trying to setup a system where users can submit an equation to
I'm looking for a simple CAS system for scala. It should have the following
I have two simple java codes.The first one defines constant power as power =
I have this simple query: SELECT POWER(( 1 + 3 / 100 ), (
I have a fairly simple ruby syntax question (and a couple other clarifications), and
I have a TextBox which should only accept numbers (they can be simple int
I have simple regex \.*\ for me its says select everything between and ,
I have simple win service, that executes few tasks periodically. How should I pass

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.