Typefully

Choosing Between UUIDs and Serial Values for Database Primary Keys

Avatar

Share

 • 

A year ago

 • 

View on X

UUIDs vs Serial values for Primary Keys/row identifiers in a database ? Image Source ~ @PlanetScale
What is UUID ? > Short for Universally Unique Identifier > 36-character alphanumeric string > Often used to identify rows of data within a database table, with each row assigned a specific UUID > Example of a UUID: acde070d-8c4c-4f0d-9d8a-162843c10333
What is Serial value ? > A simple autoincremented sequence starting from 1 for first row, 2 for second row & so on
Problem with Serial value ? > Consider a ecommerce database with Orders table where order_id as row identifier. Having a serial value for order_id makes it easier to guess the next value > A REST API call /orders/<order_id> easily becomes vulnerable to attacks
Using UUID solves this problem by uniquely identifying each row with a randomly generated 36 character string make is impossible to guess the other data rows.
Using UUIDs does have it's own challenges such as > More storage required & hence using it for storage critical apps can be an issue > Indexing will take longer
What are your thoughts on using UUIDs as row identifiers ???
Avatar

Jaydeep

@_jaydeepkarale

Follow Me If You Like 🐍 Python 🤖 Machine Learning/AI ⚙️ Software Engineering 📊 Data Visualization 📩 DM For Collab youtube.com/@jaydeepkarale