
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
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:
1CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
2 NOT DETERMINISTIC
3 NO SQL
4BEGIN
5 -- 16 random bytes = 32 random hex characters, generated all at once
6 DECLARE rnd CHAR(32) DEFAULT HEX(RANDOM_BYTES(16));
7
8 -- 1st group is 8 characters = 4 bytes
9 DECLARE g1 CHAR(8) DEFAULT SUBSTR(rnd, 1, 8);
10
11 -- 2nd group is 4 characters = 2 bytes
12 DECLARE g2 CHAR(4) DEFAULT SUBSTR(rnd, 9, 4);
13
14 -- 3rd group is 4 characters = 2 bytes, starting with a: '4'
15 DECLARE g3 CHAR(4) DEFAULT CONCAT('4', SUBSTR(rnd, 13, 3));
16
17 -- 4th group is 4 characters = 2 bytes, starting with a: '8', '9', 'A', or 'B'
18 DECLARE g4 CHAR(4) DEFAULT CONCAT(HEX(CONV(SUBSTR(rnd, 16, 1), 16, 10) & 3 | 8), SUBSTR(rnd, 17, 3));
19
20 -- 5th group is 12 characters = 6 bytes
21 DECLARE g5 CHAR(12) DEFAULT SUBSTR(rnd, 20, 12);
22
23 RETURN LOWER(CONCAT(g1, '-', g2, '-', g3, '-', g4, '-', g5));
24END;
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:
1CREATE FUNCTION uuid_v4() RETURNS CHAR(36)
2 NOT DETERMINISTIC
3 NO SQL
4BEGIN
5 DECLARE rnd CHAR(32) DEFAULT HEX(RANDOM_BYTES(16));
6 RETURN LOWER(CONCAT(
7 SUBSTR(rnd, 1, 8),
8 '-', SUBSTR(rnd, 9, 4),
9 '-4', SUBSTR(rnd, 13, 3),
10 '-', HEX(CONV(SUBSTR(rnd, 16, 1), 16, 10) & 3 | 8), SUBSTR(rnd, 17, 3),
11 '-', SUBSTR(rnd, 20, 12)
12 ));
13END;
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.
1CREATE TABLE users
2(
3 id CHAR(36) NOT NULL,
4 name VARCHAR(255) NOT NULL,
5 PRIMARY KEY (id)
6);
7
8INSERT INTO users (id, name) VALUE (uuid_v4(), 'David Axmark');
9
10SELECT *
11FROM users;

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:
1+--------------------------------------+--------------+
2| id | name |
3+--------------------------------------+--------------+
4| bdffcd96-a52e-40ed-8c3f-806899e5b6b4 | David Axmark |
5+--------------------------------------+--------------+
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
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.


