Generating v4 UUIDs in MySQL

Christian Emmer
Christian Emmer
Nov 13, 2020* · 4 min read
Generating v4 UUIDs in MySQL

MySQL's UUID() function generates v1 UUIDs , which have a time component that make them unevenly distributed over short periods of time. We can define our own function to generate v4 UUIDs , which are random and therefore more evenly distributed.

Check out "Generating v7 UUIDs in MySQL" for a time-ordered alternative.

Generating v7 UUIDs in MySQL
Generating v7 UUIDs in MySQL
Nov 9, 2026 · 5 min read

MySQL's UUID() function generates v1 UUIDs, which are time-based, but not time-ordered. We can define our own function to generate v7 UUIDs, which are more appropriate for primary keys.

A custom uuid_v4() function

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

sql


CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
    NOT DETERMINISTIC
    NO SQL
BEGIN
    -- 16 random bytes = 32 random hex characters, generated all at once
    DECLARE rnd CHAR(32) DEFAULT HEX(RANDOM_BYTES(16));


    -- 1st group is 8 characters = 4 bytes
    DECLARE g1 CHAR(8) DEFAULT SUBSTR(rnd, 1, 8);


    -- 2nd group is 4 characters = 2 bytes
    DECLARE g2 CHAR(4) DEFAULT SUBSTR(rnd, 9, 4);


    -- 3rd group is 4 characters = 2 bytes, starting with a: '4'
    DECLARE g3 CHAR(4) DEFAULT CONCAT('4', SUBSTR(rnd, 13, 3));


    -- 4th group is 4 characters = 2 bytes, starting with a: '8', '9', 'A', or 'B'
    DECLARE g4 CHAR(4) DEFAULT CONCAT(HEX(CONV(SUBSTR(rnd, 16, 1), 16, 10) & 3 | 8), SUBSTR(rnd, 17, 3));


    -- 5th group is 12 characters = 6 bytes
    DECLARE g5 CHAR(12) DEFAULT SUBSTR(rnd, 20, 12);


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

This generates all the random bytes with a single call to RANDOM_BYTES(), rather than one call per group, and then splits them up. It uses local variables , which are cheaper than @ user-defined variables , and they don't leak into the caller's session.

Here's a version with only one variable, to remove any overhead the others might add:

sql


CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
    NOT DETERMINISTIC
    NO SQL
BEGIN
    DECLARE rnd CHAR(32) DEFAULT HEX(RANDOM_BYTES(16));
    RETURN LOWER(CONCAT(
            SUBSTR(rnd, 1, 8),
            '-', SUBSTR(rnd, 9, 4),
            '-4', SUBSTR(rnd, 13, 3),
            '-', HEX(CONV(SUBSTR(rnd, 16, 1), 16, 10) & 3 | 8), SUBSTR(rnd, 17, 3),
            '-', SUBSTR(rnd, 20, 12)
        ));
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.

MariaDB doesn't have RANDOM_BYTES(), but MariaDB v11.7.0 (2024) added a native UUID_v4() function, so you don't need any of this.

Putting it all together

Here's an example where a table users uses UUIDs for its primary key. Using CHAR(36) simplifies this example, but see "Making UUIDs More Performant in MySQL" for why you don't want to use strings for performance reasons.

sql


CREATE TABLE users
(
    id   CHAR(36)     NOT NULL,
    name VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);


INSERT INTO users (id, name) VALUE (uuid_v4(), 'David Axmark');


SELECT *
FROM users;
Making UUIDs More Performant in MySQL
Making UUIDs More Performant in MySQL
Nov 14, 2020 · 3 min read

MySQL does not have a native UUID type, so strings are often used instead, which come with large storage and performance costs.

If everything went well, at the end you should have table contents similar to:

text


+--------------------------------------+--------------+
| id                                   | name         |
+--------------------------------------+--------------+
| bdffcd96-a52e-40ed-8c3f-806899e5b6b4 | David Axmark |
+--------------------------------------+--------------+

Why not choose v4?

The random nature of v4 UUIDs makes them a poor choice for clustered indexes. See "Why You Should Use UUIDs for Your Primary Keys" for more information.

Why You Should Use UUIDs for Your Primary Keys
Why You Should Use UUIDs for Your Primary Keys
Jan 30, 2020 · 4 min read

Universally unique identifiers (UUIDs) are 128-bit (16-byte) numbers that are designed to be globally unique, and as a result they make for great primary keys.