UUID in SQL — PostgreSQL, MySQL, and More
Using UUIDs as primary keys or identifiers in SQL databases is common. Here's how the main engines handle them and how to generate UUIDs in SQL.
PostgreSQL
PostgreSQL has a native uuid type (16 bytes). Use it for columns: id UUID PRIMARY KEY DEFAULT gen_random_uuid(). gen_random_uuid() (PG 13+) produces v4 UUIDs. For v7 or bulk data, generate UUIDs in your app or with our bulk generator and API, then insert. Indexing UUID columns works like any other type; for high insert rates, consider UUID v7 for better index locality.
MySQL / MariaDB
MySQL 8.0 has UUID() which returns a v1-like UUID string. Store it in CHAR(36) or BINARY(16) (see binary vs string). There's no built-in UUID type; use CHAR(36) for readability or BINARY(16) with UNHEX(REPLACE(uuid, '-', '')) for size. Generate v4 or v7 in application code or via our API.
SQL Server
Use NEWID() for a random (GUID) value or NEWSEQUENTIALID() for a sequential GUID to reduce index fragmentation. Column type: uniqueidentifier. For RFC UUID strings, generate in the app and insert as string.
Summary
Use the native uuid type where available (e.g. PostgreSQL); elsewhere use CHAR(36) or BINARY(16). Prefer app-side or API generation for v4/v7 and bulk inserts. Validate with our validator or validation API before insert if needed.
Storing UUIDs: binary vs string · UUID as primary key · Bulk generator