Storing a UUID in SQLite involves choosing how to generate it and how to represent it. A UUID is a 128-bit identifier, and SQLite can store one as TEXT or BLOB while you generate it in SQL or application code.
This article shows how to generate UUIDv4 in SQL or use application code for UUIDv4 and UUIDv7.
TL;DR: Store UUIDs as TEXT or BLOB
A UUID is a 128-bit identifier. SQLite has no dedicated UUID storage class, so use a 36-character canonical string or a 16-byte BLOB.
- Use an application UUID library for UUIDv4 or UUIDv7 generation.
- Use SQLite randomblob() only when you also set the UUID version and variant bits.
- Choose BLOB for compact storage, or TEXT when direct inspection matters.
What Is a UUID in SQLite?
A UUID is a 128-bit identifier commonly written as 32 hexadecimal digits grouped by hyphens. SQLite associates storage classes with values rather than enforcing a native UUID type.
SQLite can store the canonical string as TEXT or the 16 underlying bytes as BLOB. The values can identify rows, but SQLite does not generate them automatically as a column type.
How to Generate and Store SQLite UUIDs
Application code can generate and bind either UUID version, while SQL can produce UUIDv4 with randomblob(). Generate the value before INSERT so each row receives a fresh identifier.
Step 1: Generate UUIDv4 with randomblob()
UUIDv4 uses random bits, with fixed bits identifying its version and variant. This query formats 16 random bytes, replaces the version nibble with 4, and selects a valid variant nibble.
WITH r(x) AS (SELECT hex(randomblob(16)))
SELECT lower(
substr(x, 1, 8) || '-' || substr(x, 9, 4) || '-4' ||
substr(x, 14, 3) || '-' ||
substr('89ab', (random() & 3) + 1, 1) || substr(x, 18, 3) || '-' ||
substr(x, 21, 12)
) AS uuid;
The output has the canonical hyphenated form, with 4 in the version position and 8, 9, a, or b at the start of the variant group. The SQL result changes on each execution.
Step 2: Generate UUIDv4 or UUIDv7 in application code
Python’s uuid module provides UUIDv4. The current Python documentation also defines uuid7(), though the installed interpreter may not provide it yet. Check your runtime before calling that function.
import sqlite3
import uuid
connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE items (id TEXT PRIMARY KEY, label TEXT)")
item_id = str(uuid.uuid4())
connection.execute("INSERT INTO items VALUES (?, ?)", (item_id, "sample"))
print(connection.execute("SELECT id, label FROM items").fetchone())
UUIDv7 can be useful when time-ordered identifiers matter, but SQLite does not generate that version by itself. It is supplied by the application or an extension, so confirm the generator’s version and byte representation before storing values in a BLOB column.
Step 3: Store the UUID as TEXT or BLOB
TEXT is readable in database tools and uses 36 ASCII characters in the canonical form. BLOB stores the 16 UUID bytes and needs a consistent byte order when the application reads and writes it.
import sqlite3
import uuid
connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE ids (id BLOB PRIMARY KEY)")
value = uuid.uuid4()
connection.execute("INSERT INTO ids VALUES (?)", (value.bytes,))
row = connection.execute("SELECT id FROM ids").fetchone()[0]
print(len(row), uuid.UUID(bytes=row))
UUID.bytes is 16 bytes in big-endian field order. Convert it back with UUID(bytes=row) using the same convention. A TEXT column can instead hold str(value), including the hyphens.
SQLite UUID Edge Cases and Storage Trade-Offs
A UUID primary key affects the table layout as well as the stored value. In an ordinary rowid table, a non-INTEGER primary key is held in a separate index, while the table itself is keyed by rowid.
TEXT keeps the UUID’s 36-character spelling visible in query results. BLOB preserves only the 16 identifier bytes, so an application can save 20 bytes per stored UUID value before SQLite’s record and index overhead. That comparison describes the column payload, not the full database file size.
| Choice | What to check | Practical consequence |
|---|---|---|
| UUIDv4 from randomblob() | Version nibble is 4 and variant starts with 8, 9, a, or b | Raw random bytes alone do not mark a UUID version |
| TEXT | Keep the canonical hyphens and letter case consistent | Readable 36-character identifier |
| BLOB | Use one byte order for every insert and lookup | Stores 16 bytes and can be displayed as UUID text in application code |
| Ordinary rowid table | Inspect the schema and primary-key index | Non-INTEGER primary key has its own index |
| WITHOUT ROWID | Primary key is required and AUTOINCREMENT is unavailable | Primary key defines the table’s clustered key |
WITHOUT ROWID makes the primary key the table’s key. It can reduce duplicate key storage in some schemas, but SQLite documents restrictions, including a required primary key and no AUTOINCREMENT support. Measure with your schema and workload before choosing it.
Raw randomblob(16) returns 16 bytes, but by itself it does not mark the value as UUIDv4. If the version and variant bits are missing, use an application UUID library or the complete SQL expression above.
UUIDv7 sorts by its time-based leading fields, unlike random UUIDv4 values. That ordering may suit insertion workloads, but this property alone does not establish a performance result for your database. Keep one representation consistent across inserts, lookups, and comparisons.
Conclusion: Choose One UUID Representation
Generate identifiers in a UUID library where practical, then pick TEXT for readability or BLOB for the 16-byte form. For more SQLite functions, see the SQLite functions reference and SQLite primary keys guide.
FAQ
The FAQ below answers whether SQLite has a UUID type and how to store identifiers.
Does SQLite support UUIDs?
SQLite has no dedicated UUID storage class. Store a UUID as TEXT or as its 16-byte BLOB representation.
Can SQLite generate a UUID?
SQLite provides randomblob(), but a valid UUIDv4 requires the version and variant bits to be set. Application UUID libraries are usually clearer.
Should I store UUIDs as TEXT or BLOB?
TEXT is readable and uses the canonical hyphenated form. BLOB stores 16 bytes, with the application responsible for a consistent byte order.
