// dialect comparison

Generate a UUID in SQL in SQL Server, PostgreSQL, MySQL, SQLite and Oracle

Four named functions with different versions and shapes, plus one engine that makes you roll your own.

Filed under Types & casting

The verdict

All five mint something unique but none agree on shape or version: random v4 in SQL Server and PostgreSQL (plus v7 in PostgreSQL 18), time-based v1 in MySQL, a RAW GUID in Oracle, and hand-rolled hex in SQLite. For cross-engine code, generate UUIDs in the application.

Where they differ

Every engine can mint a unique id, but the versions differ (v1-style time-based vs v4 random), the shapes differ (dashes, case, RAW), and SQLite alone has no function at all.

How each engine writes it

SQL Server

SELECT NEWID();

Random v4-style GUID as a UNIQUEIDENTIFIER. NEWSEQUENTIALID() gives index-friendly values but only works as a column default.

PostgreSQL

SELECT GEN_RANDOM_UUID();

Built in since PostgreSQL 13 (pgcrypto before that) and returns v4. PostgreSQL 18 adds uuidv7() for time-ordered ids; uuid_generate_v1() and friends come from the uuid-ossp extension.

MySQL

SELECT UUID();

Version 1: timestamp plus node id, so values reveal the host and creation time. The text form starts with the low time bits and does not sort by time; UUID_TO_BIN(UUID(), 1) swaps the time fields so the binary form does.

SQLite

SELECT LOWER(HEX(RANDOMBLOB(16)));

No uuid function: 32 random hex chars with no dashes and no version bits. Real UUID formatting needs an app-side value or a custom function.

Oracle

SELECT RAWTOHEX(SYS_GUID()) FROM dual;

SYS_GUID() returns a 16-byte RAW that is not an RFC 4122 UUID; RAWTOHEX renders 32 uppercase hex chars with no dashes. Recent 23ai releases add UUID(), which returns a random v4 UUID as RAW(16).

Questions that come up

Which UUID version do the built-ins produce?

SQL Server's NEWID, PostgreSQL's GEN_RANDOM_UUID and Oracle's newer UUID() are random v4, and PostgreSQL 18's uuidv7() is time-ordered. MySQL's UUID() is v1 (timestamp plus node id), which leaks the host. Oracle's SYS_GUID() follows no UUID version at all.

Is the SQLite RANDOMBLOB trick a real UUID?

Not strictly: it is 16 random bytes formatted as hex, with no version/variant bits set. Uniqueness is fine at 128 bits; interop with strict UUID parsers is not.

Should the database or the app generate UUIDs?

App-side generation keeps IDs available before insert (batching, offline clients) and is identical across engines; DB-side defaults guarantee no app can forget it.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.