Choosing a primary key looks like a small decision and behaves like a permanent one. The key ends up in every foreign key, every URL and every log line the table ever produces, and changing it later means touching all of them. The two realistic candidates for most tables are an auto-increment integer handed out by the database, or a UUID the application generates itself. This guide weighs the UUID option honestly, then shows how to produce the keys with the free UUID Generator on Ovatly.

Contents

  • Quick answer
  • What changes when the key is a UUID
  • The case for UUID keys
  • The case against UUID keys
  • How to generate UUID keys, step by step
  • Worked example: seeding a table with a batch
  • Limitations and trade-offs
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Use UUID primary keys when identifiers need to be created in more than one place, shown publicly, or merged between systems later. Accept the costs where they bite: a 128-bit key is larger than an integer, and random values scatter inserts across an index instead of appending at the end. The UUID Generator creates version 4 UUIDs in batches of 1 to 100, in the canonical lowercase hyphenated form most databases expect, generated locally and stored nowhere.

What changes when the key is a UUID

With an integer key, the database owns identifier creation. A row has no ID until it is inserted, and only that one database can safely hand out the next number. With a UUID key, the application owns it. Any component with a good random source can mint a key that the whole system accepts, because a version 4 UUID carries 122 random bits and collisions are practically impossible: you would need to generate billions per second for years to expect one.

That single change, who mints the key, drives every pro and con below. The value itself is simple: 32 hexadecimal digits in five hyphenated groups following the pattern xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where the 4 marks version 4 and the y position is one of 8, 9, a or b. The generator returns the canonical lowercase form databases expect.

The case for UUID keys

No coordination. Rows can be created on any server, in any order, without asking a central counter for the next number. Sharded systems, offline-first apps and event producers depend on this.

Known before the insert. Because the application mints the key, it knows a row's identifier before the row exists. Code can build object graphs, files or URLs that reference the ID and insert afterwards, without a round trip to learn what number the database assigned.

Merge-friendly. Two databases that both used UUID keys can be combined without remapping: the key spaces do not overlap, so rows keep their identifiers and every foreign key stays valid. With integer keys, a merge means renumbering one side and rewriting every reference.

Unguessable and non-revealing. A UUID in a URL does not disclose how many rows exist, which record came before, or what the next identifier will be. An attacker cannot increment it to walk the table. Version 4 values encode no timestamp or machine address either, so the key leaks nothing about its origin.

The case against UUID keys

Size. A UUID is a 128-bit value, against the handful of bytes of an integer key. The primary key is repeated in every index on the table and in every foreign key on other tables, so the difference multiplies across a schema, and on very large tables it is a genuine cost in storage and memory.

Scattered inserts. A clustered index stores rows in key order. Integer keys always append at the end, the cheapest write pattern. Random UUIDs land all over the index, so inserts touch pages throughout the table instead of one hot page at the end. How much this costs depends on the database and the workload, and it is the most common technical argument against random keys.

No ordering information. Sorting by a version 4 key gives a random order, not a creation order. Tables that lean on the key for newest-first listings need a separate timestamp column. If time-ordered identifiers are the priority, UUID v7 in a dedicated library is the tool for that job; this generator creates version 4 only.

Harder to read. Nobody quotes a UUID over the phone the way they quote integer 4127. Debugging, support calls and hand-written queries are all slightly more awkward, and URLs get longer.

How to generate UUID keys, step by step

  1. Open the UUID Generator and choose how many keys you need, from 1 to 100.
  2. Press Generate UUIDs. Values come from your browser's cryptographic random source, and batches of 100 are instant.
  3. Use Copy all to take the whole list, one per line, or copy a single value.
  4. Paste the keys into your seed script or fixture file. Generate again whenever you need a fresh batch; values are never repeated or stored.

Worked example: seeding a table with a batch

Say a products table uses UUID primary keys and you need fifty rows for a demo dataset. Set the generator to 50 and press Generate UUIDs. You get fifty version 4 values, one per line, each in canonical lowercase form. Copy all and paste the list into your seed file, one key per product row.

Check two values against the pattern before you run the seed. The third group of each starts with 4, confirming version 4, and the fourth group starts with 8, 9, a or b. Nothing in any value says which product it belongs to or which was generated first, which is the property you wanted: the demo data can later merge into a staging database that generated its own keys, with no collisions and no remapping.

Limitations and trade-offs

The generator creates version 4 (random) UUIDs only. If your design settles on a time-ordered scheme such as v7 to soften the scattered-insert cost, you need a dedicated library for generation instead. The generator also stores nothing: values exist only in the page until you close it, so a batch you did not copy is gone, a deliberate privacy property, not a missing feature.

One boundary deserves repeating. A UUID key is an identifier, not a credential. Its unguessability is a useful privacy property, but it does not replace authorisation checks on your routes, and a bare UUID is no substitute for an API key or password where real credential strength is required.

Common mistakes

  • Choosing UUID keys by fashion. The benefits are specific: multi-writer creation, merging, public identifiers. If none of those apply, an integer key is simpler and smaller.
  • Storing the key as free-form text in whatever case arrives. Mixed formats make equality checks and lookups flaky. Keep the canonical lowercase hyphenated form everywhere.
  • Assuming the key sorts by time. Version 4 is random. Add a created-at timestamp column for ordering instead of reading meaning into the key.
  • Treating an unguessable key as access control. Anyone who obtains the identifier can present it. Check permissions on every request regardless of key type.
  • Generating keys one at a time for bulk work. The tool makes up to 100 per batch with Copy all; repeated single generations waste your time, not the tool's.

FAQs

How do I get enough UUIDs to seed a table or a fixture file? Generate a batch with the UUID Generator: choose from 1 to 100 values, press Generate UUIDs, then use Copy all to take the whole list, one per line, ready to paste into a seed script. Generate again for each further batch; values are never repeated or stored.

Are UUID primary keys unguessable? Yes, for practical purposes. Version 4 UUIDs carry 122 random bits, so a key reveals nothing about neighbouring rows and cannot be walked to reach the next record. A UUID is still an identifier, not a credential, so it does not replace access checks.

Can two databases ever mint the same UUID key? Version 4 UUIDs carry 122 random bits. The chance of an accidental duplicate is so small that the whole industry treats them as unique; you would need to generate billions per second for years to expect one collision. That is what makes merging databases practical.

Do UUID keys take more space than integer keys? Yes. A UUID is a 128-bit value, larger than a typical integer key, and every index and foreign key that carries it grows with it. The difference is modest on small tables and a real consideration on very large ones.

Should every table use a UUID primary key? Not necessarily. Tables whose identifiers stay inside one database and are never shown publicly can be fine with integers. UUID keys earn their cost where identifiers are public, created in more than one place, or merged between systems.

Conclusion

UUID primary keys trade bytes and tidy ordering for freedom: the freedom to create rows anywhere, know identifiers before inserts, and merge data without renumbering. Where those freedoms matter, the trade is usually worth it, and the UUID Generator supplies the keys in batches of up to 100, locally, in the form databases expect. If you are still deciding whether random identifiers fit your application at all, the companion guide explains why version 4 became the default.

Related guide: Why UUID v4 Is the Default Choice for Most Apps.

More tools for this kind of work live in the Developer Tools archive.