How to Generate UUIDs in MySQL

Generate UUID values in MySQL with UUID(), validate text with IS_UUID(), and store compact binary values with UUID_TO_BIN() and BIN_TO_UUID().

Important: MySQL UUID() generates UUID v1 values. If you need UUID v4 or UUID v7 semantics, generate them in the application layer and store them in MySQL.

How to generate UUID values in MySQL

SELECT UUID() AS uuid_value;

According to the MySQL reference manual, UUID() returns a UUID version 1 value.

How to store UUIDs efficiently in MySQL

CREATE TABLE orders (
    id BINARY(16) PRIMARY KEY,
    description VARCHAR(200) NOT NULL
);

INSERT INTO orders (id, description)
VALUES (UUID_TO_BIN(UUID()), 'first order');

SELECT BIN_TO_UUID(id) AS id, description
FROM orders;

Use BINARY(16) for compact storage. UUID_TO_BIN() and BIN_TO_UUID() handle the string-to-binary round trip.

Native support notes

  • MySQL does not expose a dedicated UUID column type.
  • UUID() returns UUID v1 values.
  • UUID_TO_BIN() and BIN_TO_UUID() are the standard conversion helpers.
  • UUID_TO_BIN(uuid, 1) only helps with time-part swapping for UUID v1 layouts.

Practical notes

  • Use BINARY(16) instead of CHAR(36) when you care about storage and index size.
  • If you need UUID v4 or UUID v7, generate them in app code and store them as binary.
  • Read database performance guidance before using random UUIDs as clustered keys in InnoDB.

Frequently Asked Questions

Not with built-in generation functions in current MySQL documentation. Native generation is UUID(), which returns UUID v1 values.

Usually no. BINARY(16) is smaller and indexes better than storing CHAR(36) or VARCHAR(36) UUID strings.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.