How to Generate a UUID for Database Records, Explained

A UUID, short for Universally Unique Identifier, is a 128-bit label used to identify something, such as a database record, a file, or an upload, without needing a central counter to hand out numbers. It looks like this: a string of 32 hexadecimal characters split into five groups by hyphens, 36 characters in total. You have seen UUIDs in URLs, file names, and API responses, usually without noticing them.

Why not just count 1, 2, 3

Auto-incrementing IDs are simple, but they leak information and create problems in distributed systems. Sequential IDs reveal how many users or orders you have, which competitors can read from public URLs. They also break when two separate systems create records independently and later merge, because both systems will have a record numbered 42. UUIDs solve both problems: they carry no count information, and two systems can generate IDs independently with no coordination, because the chance of a collision is so small it can be ignored in practice.

How unique is unique

A version 4 UUID, the random kind, has 122 random bits, which means about 5.3 followed by 36 zeros possible values. Even generating a billion UUIDs per second for decades would still leave the chance of a duplicate far below one in a billion. That is why UUIDs are trusted without a central registry: the maths makes collisions a theoretical curiosity rather than a practical risk. This does not make a UUID a secret, though. A UUID is an identifier, not a password: anyone who sees it can use it, so do not rely on a UUID alone to protect private data. For reset links and invite tokens, pair the UUID with expiry and authentication checks.

Version 4 versus version 7

Version 4 UUIDs are fully random, which is ideal when you want no information embedded in the ID. Their drawback for databases is that random IDs scatter new rows across the index, which can slow inserts on very large tables. Version 7 UUIDs fix this by starting with a timestamp, so newer IDs sort after older ones, keeping inserts clustered and making time-ordered queries natural. Version 7 is the modern default for database primary keys in new systems. Version 1, based on time plus hardware address, is older and can leak timing and machine information, so it is rarely the right choice today. Version 5 generates the same UUID every time from the same name and namespace, which is useful when you need a stable ID derived from a known value, such as a category name.

How to generate UUIDs

For a handful of IDs, an online generator is fastest: choose the version, choose how many you need, copy the results, and paste them into your data. For code, every major language has a built-in or standard option: Python’s uuid module with uuid.uuid4() for version 4, JavaScript’s crypto.randomUUID() in browsers and Node.js, and database functions such as gen_random_uuid() in PostgreSQL or UUID() in MySQL. Generate the ID in your application code before inserting the record, so you know the ID before the row exists, which simplifies related records and API responses.

Storage trade-offs

Storing a UUID as text takes 36 characters, which is readable and easy to debug but larger than necessary. Storing it as 16 raw bytes halves the size and speeds up indexes, at the cost of readability: you will need to convert it for display. For small tables, text storage is fine and simpler. For large tables with millions of rows, binary storage is worth the extra conversion code. Whichever you choose, be consistent across tables, because mixing text and binary UUIDs in one system causes subtle join bugs.

Quick checklist

Use version 4 for general random IDs, version 7 for database keys where ordering matters, and version 5 for stable name-derived IDs. Never use a UUID as your only security control, and never expose sequential counts you meant to hide. Generate before insert, store consistently, and your IDs will stay unique without any central coordination.

Try it free right here

Use the free UUID Generator — Generate Random UUIDs (v4) Online in Bulk below, right on this page. It runs in your browser and needs no sign-up. You can also open the full tool here: UUID Generator — Generate Random UUIDs (v4) Online in Bulk.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top