Skip to main content

How to Create UUID in MySQL

Complete guide with UUID() and BINARY storage

How to Create UUID in MySQL. Call UUID() to generate a MySQL UUID in one line. SYS_GUID() returns RAW(16) with no extension required, and the oracle uuid page shows it as a column default.

Generate UUID for MySQL

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

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.

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 VARCHAR(36) versus UUID column type trade-off is part of the page on uuid in postgresql, including the ::uuid cast.

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 anyone working in mongosh, a helper that creates a native binary value is covered in the mongodb uuid guide for beginners.

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

About the author

Vinitha Pu

Founder and designer

Vinitha Pu is a graphic designer based in Palakkad, Kerala, who builds free, privacy-friendly browser tools. She studied Textile and Apparel Design at Avinashilingam University and completed a C-Tech in Graphic Design at Image Creative Education in 2025. She designs, builds and maintains the UUID generators, validator, decoder and the language and database UUID guides on UUID Generator.

  • Content author: Vinitha Pu
  • Fact checked by: Vinitha Pu. Every generator is checked against the RFC 4122 and RFC 9562 layouts and every code example is run before it goes live.
  • Published Updated

Vinitha Pu on Behance · Vinitha Pu on LinkedIn

Comments & Feedback

Share your experience or ask questions about this tool

Copied!