Skip to main content

How to Create UUID in MySQL

Complete guide with UUID() and BINARY storage

Reach for MySQL's built-in UUID() function when you need a unique identifier with zero setup — but know upfront that it doesn't produce a random v4 UUID the way most developers assume. This guide shows you how to generate a UUID in MySQL, convert a UUID string to compact BINARY(16) storage with UUID_TO_BIN(), and get a true random v4 value when your application actually needs one. Getting the storage type right matters because a UUID stored as a bloated 36-character string wastes space and slows down every lookup on that column. Use the postgresql uuid guide to learn how PostgreSQL stores UUIDs as 16-byte binary — saving 55% over VARCHAR.

Generate UUID for MySQL

SELECT UUID();
550e8400-e29b-41d4-a716-446655440000

How to Generate a UUID in MySQL

The built-in UUID() function is the zero-setup way to generate a UUID in MySQL, but it doesn't generate a random UUID — it encodes the current timestamp and the server's MAC address, making it a v1-style value rather than a v4 one. The oracle uuid guide covers SYS_GUID() function usage and RAW(16) storage for UUID primary keys in Oracle.

mysql
-- UUID() is timestamp + MAC-address based (v1-style), NOT a random v4 value
SELECT UUID();
-- Output: 550e8400-e29b-41d4-a716-446655440000

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

Explanation

Convert a String to a UUID in MySQL

MySQL 8.0 added UUID_TO_BIN() and BIN_TO_UUID() to convert a 36-character UUID string to and from compact BINARY(16) storage. For Spring Boot UUID primary key patterns, the java uuid guide includes JPA entity and repository examples.

mysql
-- Convert a UUID string to compact 16-byte binary storage
SELECT UUID_TO_BIN('550e8400-e29b-41d4-a716-446655440000');

-- Convert it back to a readable string
SELECT BIN_TO_UUID(UUID_TO_BIN('550e8400-e29b-41d4-a716-446655440000'));

-- Typical usage: store as BINARY(16), display as a string
CREATE TABLE orders (
    id BINARY(16) PRIMARY KEY,
    total DECIMAL(10,2) NOT NULL
);

INSERT INTO orders (id, total)
VALUES (UUID_TO_BIN('550e8400-e29b-41d4-a716-446655440000'), 49.99);

SELECT BIN_TO_UUID(id) AS uuid, total FROM orders;

Explanation

Other Options for Generating UUIDs in MySQL

Since UUID() isn't a random v4 value, get one by generating it in application code instead, then insert it as a literal string or through UUID_TO_BIN().

mysql
-- MySQL has no built-in v4 generator -- generate one in application code
-- (see the python uuid / rust uuid guides) and insert it as a literal string
INSERT INTO sessions (id, user_id)
VALUES ('9b74c989-f3a0-4b1a-9b6a-2f6a9b9a9c1e', 42);

-- Or convert the app-generated string to BINARY(16) on insert
INSERT INTO sessions (id, user_id)
VALUES (UUID_TO_BIN('9b74c989-f3a0-4b1a-9b6a-2f6a9b9a9c1e'), 42);

-- swap_flag=1 reorders UUID()'s own timestamp bytes for better index locality
CREATE TABLE events (
    id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID(), 1)),
    payload JSON
);

Explanation

Comments & Feedback

Share your experience or ask questions about this tool

Copied!