Database.Middle.What are surroget keys ?

Surrogate keys are artificial, system-generated keys used to uniquely identify rows in a database table. They are not derived from application data and have no business meaning — typically auto-increment integers or UUIDs.


🧠 Definition

A surrogate key is a standalone identifier created solely for the purpose of being a primary key, usually implemented as an ID column.


✅ Example

Table: users

id (surrogate key)emailusername
1john@example.comjohnny
2alice@example.comalice88

Here, id is a surrogate key — it’s:

  • Unique
  • System-generated
  • Has no meaning to the user

🆚 Surrogate Key vs Natural Key

FeatureSurrogate KeyNatural Key
Based on data?❌ No (synthetic)✅ Yes (e.g. email, SSN, VIN)
Stability✅ Very stable❌ Can change over time
Performance✅ Fast joins/indexes❌ Slower if long or complex
Human meaning❌ None✅ Usually has semantic meaning
Exampleuser_id SERIALemail, SSN, username

🧰 Why Use Surrogate Keys?

  • Immutable: won’t change even if business data does
  • Uniform type: predictable size (e.g., INT, UUID)
  • Simpler relationships: easier to define foreign keys
  • Avoid composite keys: simplifies joins and indexing

⚠️ When Not to Use

  • When the natural key is compact and immutable (e.g., ISO country codes, VINs, UUIDs)
  • When you want to prevent duplicates naturally with meaningful columns

🧾 Example in SQL

CREATE TABLE users (
  id SERIAL PRIMARY KEY,      -- surrogate key
  email VARCHAR(100) UNIQUE,  -- natural candidate key
  name TEXT
);

Or with UUID:

CREATE TABLE orders (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  customer_id INT,
  total_amount NUMERIC
);

🧠 Summary

TermMeaning
Surrogate KeyArtificial primary key with no business meaning
PurposeStable, consistent, unique row identifier
Common FormsSERIAL, BIGSERIAL, UUID
Best Use CaseGeneral-purpose primary key in most tables
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.