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.

Compatibility

Show:
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.

Details

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.

Standard Syntax

-- PostgreSQL SELECT gen_random_uuid(); -- SQL Server SELECT NEWID();

Version Support

MySQL: Since 5.0 PostgreSQL: Since 8.3 SQL Server: Since 2000 Oracle: Since 8i SQLite: Not supported

Per-Database Syntax & Notes

MySQL

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).

SELECT UUID(); -- Efficient binary storage (8.0+): INSERT INTO t (id) VALUES (UUID_TO_BIN(UUID(), 1));

PostgreSQL

gen_random_uuid() (v4, cryptographically random) is built-in since PG 13; earlier versions require the pgcrypto extension. Native UUID column type available.

SELECT gen_random_uuid(); CREATE TABLE t (id UUID DEFAULT gen_random_uuid() PRIMARY KEY);

SQL Server

NEWID() generates a random GUID. NEWSEQUENTIALID() generates sequential GUIDs suitable as clustered index keys (reduces page splits). Native UNIQUEIDENTIFIER column type.

SELECT NEWID(); CREATE TABLE t (id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY);

Oracle

SYS_GUID() returns a 16-byte RAW value, not a formatted UUID string. Extra formatting is required to produce the standard hyphenated form.

SELECT SYS_GUID() FROM dual; -- Hyphenated format: SELECT REGEXP_REPLACE(LOWER(RAWTOHEX(SYS_GUID())), '([a-f0-9]{8})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{4})([a-f0-9]{12})', '\1-\2-\3-\4-\5') FROM dual;