Skip to content

Instantly share code, notes, and snippets.

@acosonic
Last active January 9, 2023 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acosonic/0d5ba06ae42c55c403b6660529517c7f to your computer and use it in GitHub Desktop.
Save acosonic/0d5ba06ae42c55c403b6660529517c7f to your computer and use it in GitHub Desktop.
Procedure for adding truly random UUID to existing tabe in mysql database with empty UUID varchar field Existing uuid function does not generate enough randomness
#change the exit criteria 61564999 to fit your needs
CREATE DEFINER=`root`@`localhost` PROCEDURE `adduuid`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE a INT Default 1 ;
simple_loop: LOOP
UPDATE table SET uuid=(LOWER(CONCAT(
LPAD(HEX(ROUND(rand()*POW(2,32))), 8, '0'), '-',
LPAD(HEX(ROUND(rand()*POW(2,16))), 4, '0'), '-',
LPAD(HEX(ROUND(rand()*POW(2,16))), 4, '0'), '-',
LPAD(HEX(ROUND(rand()*POW(2,16))), 4, '0'), '-',
LPAD(HEX(ROUND(rand()*POW(2,48))), 12, '0')
))) WHERE ID=a;
SET a=a+1;
IF a=61564999 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment