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
- PostgreSQL has had
gen_random_uuid()since 13, and version 18 addeduuidv7()together withuuidv4()as an alias — so a table can now default to a time-ordered key with no extension and no client code:DEFAULT uuidv7(). MySQL offersUUID(), which returns a v1 value, andUUID_TO_BIN(uuid, 1)to store 16 bytes with the timestamp field reordered so the index stays sequential. SQL Server hasNEWID()for random GUIDs andNEWSEQUENTIALID(), which only works as a column default. - The storage decision matters as much as the generator. A
char(36)orvarchar(36)column costs 36 bytes per row and compares as a string; PostgreSQL's nativeuuid, MySQL'sbinary(16)and SQL Server'suniqueidentifierall cost 16 bytes and compare as fixed-size values. On a table with millions of rows that difference shows up in both disk and index depth.
How to generate
- Press Generate UUID above to mint a value in this browser — nothing is sent anywhere.
- Copy one value with Copy, or set How many and use Copy all for a fixture list.
- Switch Version if your code needs v7 instead of the v4 default, and turn on Formatting to match your stack's conventions.
Use Cases
- Table defaults —
DEFAULT uuidv7()so inserts need no identifier from the application. - Migration from integer keys — backfill with generated values, then switch the column type.
- Event and audit tables where a time-ordered key keeps writes appending.
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
UUID v1 GeneratorUUID v3 GeneratorUUID v4 GeneratorUUID v5 GeneratorUUID v6 GeneratorUUID v7 GeneratorUUID v8 GeneratorBulk UUID GeneratorShort UUID GeneratorUUID ValidatorUUID FormatterJavaScript UUID GeneratorTypeScript UUID GeneratorNode.js UUID GeneratorReact UUID GeneratorPython UUID GeneratorJava UUID GeneratorC# UUID GeneratorGo UUID GeneratorPHP UUID GeneratorRuby UUID GeneratorRust UUID GeneratorKotlin UUID GeneratorSwift UUID GeneratorSQL UUID Generator