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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:07:53+00:00 2026-06-16T21:07:53+00:00

I have a mysql trigger that I have a concern about having noticed some

  • 0

I have a mysql trigger that I have a concern about having noticed some “unsynced” data in my db after deploying the trigger to my production site a few days ago.

I have simplified my code for the purposes of posting here.

There are 3 tables:

score table: ID, UserID, Score, GameID (users enter a score each time they play a game, they can play the same game a number of times)

score_summary table: ID, UserID, GameID, SummaryValue (this table keeps a running score for each user for each game)

game_summary table: ID, GameID, SummaryValue (this table keeps a running overall score for each game)

When a game score is entered for a user into the score table, the trigger is there to update the user’s running total score (SummaryValue) in table score_summary and also to update the game_summary table for given GameID.

CREATE TRIGGER scores_insert_trigger AFTER INSERT ON scores
FOR EACH ROW BEGIN

    UPDATE scores_summary
    SET SummaryValue=SummaryValue + NEW.Score 
    WHERE UserID=NEW.UserID
    SELECT ROW_COUNT() INTO rowCount;
    IF (rowCount=0) THEN
        INSERT INTO scores_summary
        (UserID, GameID, SummaryValue)
        VALUES
        (NEW.UserID, NEW.GameID, NEW.Score);
    END IF;

    UPDATE game_summary
    SET SummaryValue=SummaryValue + NEW.Score 
    WHERE GameID=NEW.GameID
    SELECT ROW_COUNT() INTO rowCount;
    IF (rowCount=0) THEN
        INSERT INTO game_summary
        (GameID, SummaryValue)
        VALUES
        (NEW.GameID, NEW.Score);
    END IF;
END;

My concerns are with regards to the trigger running into a race condition if a number of users enter scores at the same time – in particular for the update of the game_summary when no scores exist for the particular game – if two users attempt this at the same time, they will both get rowCount=0 and will then both do an insert?

Are my concerns justified and if so is there anything I can do about this?

Thanks in advance guys.

  • 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-16T21:07:55+00:00Added an answer on June 16, 2026 at 9:07 pm

    I would like to provide an answer for my particular situation.

    My update statements are atomic updates which means mysql engine handles any potential race conditions.

    To avoid race condition on insert, I have opted for ON DUPLICATE KEY UPDATE which will again be atomic and will avoid two or more threads causing multiple rows being created.

    Thanks for your help.

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

Sidebar

Related Questions

I have some stored procedures and a trigger that work great in MySQL 5.5.8
I have a mysql trigger that logs every time a specific table is updated.
Possible Duplicate: mysql getting last_insert_id() in a trigger I have MySQL TRIGGER firing AFTER
MYSQL: I have a table with a AFTER INSERT ON trigger on it. I
I want to have a select statement inside the mysql trigger that has the
I have a mysql table that is updated through a web page. Some columns
I have this code (mySql 5.1.36) CREATE TRIGGER problem_description AFTER INSERT ON problem_description FOR
Here is the MYSQL Trigger that I have written: DELIMITER // CREATE TRIGGER updtrigger
I want added some like trigger to mysql when mysql start. So I have
I am trying to create a trigger in MySQL that is fired after a

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.