sqlcmd.net validated sql reference
beginner selecting MySQL MariaDB SQL Server PostgreSQL SQLite

Generate a UUID

Produce a random UUID using `UUID()` (MySQL/MariaDB), `NEWID()` (SQL Server), `gen_random_uuid()` (PostgreSQL), or a `randomblob`-based expression (SQLite).

Docker-validated Not currently validation-green

Generate a single UUID

Each engine generates a globally unique identifier on every call. MySQL and MariaDB use a time-based algorithm (version 1) that encodes the MAC address and a timestamp, which means consecutive UUIDs are somewhat ordered. SQL Server's NEWID() and PostgreSQL's gen_random_uuid() use a random algorithm (version 4) that gives no ordering guarantees. To use a UUID as a column default, place the function call in the column definition: id CHAR(36) DEFAULT (UUID()) in MySQL 8.0.13+, id UNIQUEIDENTIFIER DEFAULT NEWID() in SQL Server, or id UUID DEFAULT gen_random_uuid() in PostgreSQL. For high-insert-rate workloads on PostgreSQL, consider UUIDv7 (available via extensions) which is time-ordered and causes fewer B-tree index page splits.

The value differs on every call. The format is always 8-4-4-4-12 lowercase hex digits separated by hyphens.

Where this command helps.

  • inserting rows with a UUID primary key generated by the database before returning the ID to the application
  • generating correlation IDs for distributed tracing without a central sequence generator

What the command is doing.

A UUID (Universally Unique Identifier) is a 128-bit value rendered as 32 hex digits grouped in the pattern 8-4-4-4-12, e.g. 550e8400-e29b-41d4-a716-446655440000. UUIDs are popular as primary keys when row identity must be globally unique across independent systems or when the application needs the key before the INSERT completes. MySQL and MariaDB provide UUID(), which returns a version-1 (time-based) UUID. SQL Server provides NEWID(), which returns a version-4 (random) UUID as a UNIQUEIDENTIFIER; cast it to VARCHAR(36) to get the standard string. PostgreSQL 13+ provides gen_random_uuid() built-in; older versions need the pgcrypto or uuid-ossp extension. SQLite has no UUID function, but randomblob(16) generates 16 random bytes and hex() converts them to 32 hex characters — wrapping that in printf inserts the four dashes at the standard positions. The SQLite result has the right shape but is not a standards-compliant version-4 UUID because the version and variant bits are not set; generate UUIDs in application code if strict compliance is required.