Generating a universally unique identifier (UUID/GUID) within a SQL query or default expression.
PostgreSQL built-in UUID v4 generation function, available without extensions. Returns a cryptographically random UUID. For time-ordered UUIDs, use the uuid-ossp extension.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | 5.0 | UUID() returns a v1 UUID (time-based). UUID_TO_BIN() and BIN_TO_UUID() added in 8.0 for compact 16-byte binary storage. No native UUID column type; store as CHAR(36) or BINARY(16). |
| PostgreSQL | ✓ Supported | 8.3 | gen_random_uuid() (v4, cryptographically random) is built-in since PG 13; earlier versions require the pgcrypto extension. Native UUID column type available. |
| SQL Server | ✓ Supported | 2000 | NEWID() generates a random GUID. NEWSEQUENTIALID() generates sequential GUIDs suitable as clustered index keys (reduces page splits). Native UNIQUEIDENTIFIER column type. |
| Oracle | ✓ Supported | 8i | SYS_GUID() returns a 16-byte RAW value, not a formatted UUID string. Extra formatting is required to produce the standard hyphenated form. |
| SQLite | ✗ Not Supported | — | No built-in UUID function. Common workaround: use the application layer to generate and insert UUIDs, or construct via lower(hex(randomblob(16))) with string manipulation. |
UUID generation is available in all five engines but with different ergonomics. PostgreSQL's gen_random_uuid() and SQL Server's NEWID() are the most convenient — both return a formatted UUID string and work natively as column defaults. Oracle's SYS_GUID() returns raw bytes, requiring extra formatting to match the standard 8-4-4-4-12 hyphenated representation. MySQL lacks a native UUID column type, making efficient storage a deliberate decision (BINARY(16) with UUID_TO_BIN is recommended over CHAR(36) for index performance). SQLite has no built-in function at all. A common architecture note: random UUIDs (v4) as clustered primary keys cause index fragmentation in SQL Server — use NEWSEQUENTIALID() or ULID-style values instead.
UUID() returns a v1 UUID (time-based). UUID_TO_BIN() and BIN_TO_UUID() added in 8.0 for compact 16-byte binary storage. No native UUID column type; store as CHAR(36) or BINARY(16).
gen_random_uuid() (v4, cryptographically random) is built-in since PG 13; earlier versions require the pgcrypto extension. Native UUID column type available.
NEWID() generates a random GUID. NEWSEQUENTIALID() generates sequential GUIDs suitable as clustered index keys (reduces page splits). Native UNIQUEIDENTIFIER column type.
SYS_GUID() returns a 16-byte RAW value, not a formatted UUID string. Extra formatting is required to produce the standard hyphenated form.