UUID in SQL

SQL UUID Generator

Every major database can mint a UUID — but only one of them gives you a time-ordered one by default, and that choice is what decides how your index behaves.

PostgreSQL 18 uuidv7()MySQL UUID_TO_BINSQL Server NEWID()

Generated locally with the Web Crypto API — nothing leaves your browser.

Generating UUIDs in SQL

How to generate

  1. Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
  2. Copy one value with Copy, or set How many and use Copy all for a fixture list.
  3. Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.

Use Cases

How SQL UUID Generator Compares

uuidv7() / NEWSEQUENTIALID()Time-ordered: inserts land at the end of the index
gen_random_uuid() / NEWID()Random: unique, but writes scatter across the index
char(36)Readable, 36 bytes, compared as text — prefer a native type

Code Examples

PostgreSQL
-- PostgreSQL 13+: random. PostgreSQL 18+: also uuidv7()/uuidv4()
SELECT gen_random_uuid();
SELECT uuidv7();

CREATE TABLE events (
  id         uuid PRIMARY KEY DEFAULT uuidv7(),  -- time-ordered
  payload    jsonb NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);
MySQL
-- UUID() returns a v1 value; store 16 bytes, reordered for the index
CREATE TABLE t (
  id BINARY(16) PRIMARY KEY DEFAULT (UUID_TO_BIN(UUID(), 1)),
  name VARCHAR(100)
);

SELECT BIN_TO_UUID(id, 1) FROM t;

-- SQL Server
-- CREATE TABLE t (id UNIQUEIDENTIFIER DEFAULT NEWID());
-- CREATE TABLE t (id UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID());

Frequently Asked Questions

Which SQL function generates a UUID v7?
PostgreSQL 18's uuidv7(). MySQL and SQL Server have no v7 function — use UUID_TO_BIN(UUID(), 1) on MySQL, or generate the value in the application.
Should a UUID primary key be a string column?
No. Use PostgreSQL uuid, MySQL binary(16) or SQL Server uniqueidentifier — 16 bytes instead of 36, and compared as a fixed-size value.
Why is my random UUID primary key slow on insert?
Because the values are not ordered, so each insert lands in a different part of the B-tree and pages split. Switch to uuidv7() or NEWSEQUENTIALID() to make writes append.

Related tools