Skip to main content

How to Create UUID in SQLite

Complete guide for SQLite 3.38+ and older versions

Unlike PostgreSQL or SQL Server, SQLite has no built-in UUID generation function — there's no UUID() or gen_random_uuid() waiting for you in the core engine. That's not really a problem: you either generate the UUID in your application code before inserting it as TEXT, or build one directly with a well-known SQL expression using randomblob() and hex(). Below you'll find copy-paste ready examples for creating a SQLite UUID, storing it efficiently, and the schema-level alternatives SQLite offers instead. The oracle uuid page includes Oracle-specific index performance analysis for UUID primary keys at scale.

Generate UUID for SQLite

SELECT uuid();
550e8400-e29b-41d4-a716-446655440000

How to Generate a UUID in SQLite

SQLite core ships with no UUID() or gen_random_uuid() equivalent, so you have two honest options: generate the UUID in application code before inserting it, or build one directly with a pure-SQL expression. The postgres uuid generator reference covers UUID primary keys, BRIN indexes, and partitioning strategies in PostgreSQL.

sqlite3
# Recommended: generate the UUID in application code, then insert it as TEXT.
# Python's sqlite3 and uuid modules are both in the standard library --
# no extra packages required.
import sqlite3
import uuid

conn = sqlite3.connect('app.db')
conn.execute(
    'INSERT INTO users (id, email) VALUES (?, ?)',
    (str(uuid.uuid4()), 'jane@example.com')
)
conn.commit()
sqlite3
-- Pure-SQL alternative: a well-known community pattern that builds a
-- v4-shaped UUID directly with randomblob() and hex(), no app code needed
SELECT lower(
    hex(randomblob(4)) || '-' ||
    hex(randomblob(2)) || '-4' ||
    substr(hex(randomblob(2)), 2) || '-' ||
    substr('89ab', abs(random()) % 4 + 1, 1) ||
    substr(hex(randomblob(2)), 2) || '-' ||
    hex(randomblob(6))
);
-- Output: 7e57ac1d-9b3f-4a2e-8f1c-6d4b8a9e0f12

Explanation

Convert a String to a UUID in SQLite

SQLite is dynamically typed with no dedicated UUID column type, so a UUID received as a string — from an API request or another system — is simply stored as TEXT, or converted to BLOB when storage efficiency matters more than readability. Use the sql server uuid guide to implement SQL Server GUIDs with Entity Framework Core and Dapper ORM.

sqlite3
-- Simplest: store the UUID as TEXT, no conversion needed
CREATE TABLE users (
    id TEXT PRIMARY KEY,
    email TEXT NOT NULL
);

INSERT INTO users (id, email)
VALUES ('550e8400-e29b-41d4-a716-446655440000', 'jane@example.com');

SELECT * FROM users WHERE id = '550e8400-e29b-41d4-a716-446655440000';
sqlite3
-- Storage-efficient alternative: convert to 16 raw bytes and store as BLOB
CREATE TABLE users_compact (
    id BLOB PRIMARY KEY,
    email TEXT NOT NULL
);

-- unhex() is available in SQLite 3.41+; strip the hyphens first
INSERT INTO users_compact (id, email)
VALUES (
    unhex(replace('550e8400-e29b-41d4-a716-446655440000', '-', '')),
    'jane@example.com'
);

-- Convert back to the readable string form on read
SELECT lower(hex(id)) FROM users_compact;

Explanation

Other Options for Generating UUIDs in SQLite

Beyond application-side generation and the pure-SQL v4 expression, SQLite supports a column default that produces a random value automatically on every INSERT, without touching application code at all.

sqlite3
-- SQLite 3.38+: a simple random-value default, generated on every INSERT
CREATE TABLE sessions (
    id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),
    user_id TEXT NOT NULL,
    expires_at DATETIME
);

-- Insert without specifying id -- SQLite fills it in automatically
INSERT INTO sessions (user_id, expires_at)
VALUES ('u_042', datetime('now', '+1 day'));

SELECT id FROM sessions LIMIT 1;
-- Output: 3f9a7c21e8b4419fa02d6c7e1b5f8a90

Explanation

Comments & Feedback

Share your experience or ask questions about this tool

Copied!