Warning: unlink(./data/secure/tokens/1ea5c716065582a057487ba2603d5ec6522bd3d4c55c9cf5f1c41f6d761270063003a71a10e4ba8312c19f5945c28db0b2dfafdbd264edcfab8e86613c8df823.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/2ede9d0a408d34afbe74d9f2516caff10030f8719bef2b90ae8dd1e096c170adedb588da641eca09ebe3bc31812cdbe7f16e3204b1dd2cf782889dc05197a180.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/4b72566b90dda4c4999de7a90ae48d6d3bdccae3586c0d967c30459ae3c4903a85bb9e552ce0d109f29205e4ea53447604ce8e5bf223b58164d04f1d62818d77.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/552c5761e537974478b1ef69acc20d82925abbc6181518f75cb1af5226ed09ecef9990461d3557aad336262dbc68b28534f3ceafec198f1a1ca0afb242eb327c.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/6bb3ee3b0f40393f54cc19668a70e6b63496987a263494051970c35c85a347a84ce26355c66ecf830e0ee8620aa2a935ba105c9ef56232ea499b69e974d326ed.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865

Warning: unlink(./data/secure/tokens/f0f5ad8fb93ef6125c9ee047bfab685569f527ee7e8667ddc73220bc29802985968860019c3657e2d43ebc662843e96a4919c76bd8ab8dc7dc7580cb78357d2b.token): No such file or directory in /home/sigesthaxo/snippetvamp/assets/php/class/Helium_secure_class.php on line 865
SnippetVamp - open
#key - Generating v4 UUIDs in MySQL | Christian Emmer
A custom uuid_v4() function

Here's the function, with comments explaining each group:

CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
BEGIN
    -- 1st group is 8 characters = 4 bytes
    SET @g1 = HEX(RANDOM_BYTES(4));

    -- 2nd group is 4 characters = 2 bytes
    SET @g2 = HEX(RANDOM_BYTES(2));

    -- 3rd group is 4 characters = 2 bytes, starting with a: 4
    SET @g3 = CONCAT('4', SUBSTR(HEX(RANDOM_BYTES(2)), 2, 3));

    -- 4th group is 4 characters = 2 bytes, starting with a: 8, 9, A, or B
    SET @g4 = CONCAT(HEX(FLOOR(ASCII(RANDOM_BYTES(1)) / 64) + 8), SUBSTR(HEX(RANDOM_BYTES(2)), 2, 3));

    -- 1st group is 12 characters = 6 bytes
    SET @g5 = HEX(RANDOM_BYTES(6));

    RETURN LOWER(CONCAT(@g1, '-', @g2, '-', @g3, '-', @g4, '-', @g5));
END;

Here's a version without variables, to remove any overhead they might add:

CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
BEGIN
    RETURN LOWER(CONCAT(
            HEX(RANDOM_BYTES(4)),
            '-', HEX(RANDOM_BYTES(2)),
            '-4', SUBSTR(HEX(RANDOM_BYTES(2)), 2, 3),
            '-', HEX(FLOOR(ASCII(RANDOM_BYTES(1)) / 64) + 8), SUBSTR(HEX(RANDOM_BYTES(2)), 2, 3),
            '-', hex(RANDOM_BYTES(6))
        ));
END;

This uses RANDOM_BYTES() instead of RAND() because the former is non-deterministic and therefore more cryptographically secure, resulting in fewer UUID collisions in the end.

RANDOM_BYTES() was introduced in MySQL v5.6.17 (2014) , and is currently not available in MariaDB.