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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:36:17+00:00 2026-06-04T14:36:17+00:00

I am using the trigger shown below to automatically assign a random key to

  • 0

I am using the trigger shown below to automatically assign a random key to inserted rows, retrying the random generation if an existing row is found. 99.9% of the time this works exactly as expected. However, every couple of weeks or so the trigger locks up, the REPEAT loop within it apparently becoming infinite; the query continues indefinitely, effectively locking up the application, and has to be KILLed.

As far as I can see, there is nothing about the logic involved that makes it seem as if this should ever happen. The keyspace involved is certainly nowhere near exhaustion and never will be. What is going on?

The trigger:

DELIMITER //;
CREATE TRIGGER `mytable_insert` BEFORE INSERT ON `mytable`
FOR EACH ROW
BEGIN
    DECLARE `curr` BIGINT UNSIGNED;
    DECLARE `sel` BIGINT UNSIGNED;
    IF NEW.`mykey` IS NULL THEN
        REPEAT
            SELECT FLOOR(RAND() * 18446744073709551615) INTO `sel`;
            SELECT `id` INTO `curr` FROM `mytable` WHERE `mykey` = `sel` LIMIT 1;
        UNTIL `curr` IS NULL END REPEAT;
        SET NEW.`mykey` = `sel`;
    END IF;
END
  • 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-04T14:36:19+00:00Added an answer on June 4, 2026 at 2:36 pm

    The error is this line

    SELECT `id` INTO `curr` FROM `mytable` WHERE `mykey` = `sel` LIMIT 1;
    

    If no row matches mykey = sel then curr isn’t assigned at all.
    So curr will never be null if it’s ever assigned anything else.

    Replace with something like this (untested)

    DELIMITER //;
    CREATE TRIGGER `mytable_insert` BEFORE INSERT ON `mytable`
    FOR EACH ROW
    BEGIN
        DECLARE `count` INT UNSIGNED;
        DECLARE `sel` BIGINT UNSIGNED;
        IF NEW.`mykey` IS NULL THEN
            REPEAT
                SELECT FLOOR(RAND() * 18446744073709551615) INTO `sel`;
                SELECT count(1) INTO `count` FROM `mytable` WHERE `mykey` = `sel`;
            UNTIL `count` = 0 END REPEAT;
            SET NEW.`mykey` = `sel`;
        END IF;
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a trigger function to write data into a new table in
I am using UpdatePanel to trigger a button click event, which saves some 100+
Using the Northwind sample: I would like to create an insert trigger on the
I am using following PHP code for trigger creation but always get error, please
I want to trigger javascript alert using PHP. Is it possible I want to
I am using jquery ui datepicker icon trigger. I want to give the icon
I am trying to trigger the onScroll event this way using prototype: Event.observe(document, 'scroll',
Using jQuery 1.2.x and jQuery UI 1.5.x, one was able to trigger dragging manually
Is there a potential pitfall in using jquery on/off method and trigger to execute
I am using SQL Server 2000. I am writing a trigger that is executed

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.