Database Design
Primary Keys
Data Management
SQL Best Practices
Data Modeling

How do you like your primary keys?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In the world of database design, primary keys play a crucial role. They uniquely identify each record in a table, serving as the cornerstone for establishing relationships between tables and ensuring data integrity. But not all primary keys are created equal. Preferences and strategies for primary keys can vary widely depending on requirements and contexts.

Types of Primary Keys

There are generally two types of primary keys used in databases:

  1. Natural Keys: These are derived from real-world data and are inherently meaningful. For example, a Social Security Number or an ISBN can serve as a natural key.
  2. Surrogate Keys: These are artificially generated keys that have no intrinsic meaning outside the database. An auto-incremented integer ID or a universally unique identifier (UUID) often serves as a surrogate key.

Comparisons and Considerations

Natural Keys

Pros:

  • Readability: Natural keys can be more intuitive and easier to understand because they are derived from real-world identifiers.
  • Integrity: Any changes to the key reflect changes in the real-world property it represents.

Cons:

  • Volatility: Real-world identifiers may change, affecting the stability of the key.
  • Complexity: They often require multiple columns to guarantee uniqueness.
  • Duplication Handling: Real-world data might not be as unique as intended, causing issues.

Surrogate Keys

Pros:

  • Stability: Since they are generated uniquely, they are unlikely to change over time, providing stable identifiers.
  • Simplicity: Typically requires only a single column, reducing complexity.

Cons:

  • Loss of Meaning: Surrogate keys have no inherent meaning, which may necessitate additional metadata or documentation.

Selection Criteria

Choosing between natural and surrogate keys often boils down to use cases and system requirements. Here are some criteria:

  • Volatility of Real-World Data: If the natural identifiers are subject to change, consider surrogate keys.
  • Data Model Complexity: Surrogate keys are preferred in complex systems to simplify relationships.
  • Readability versus Stability: Determine if the need for human-readable keys outweighs the benefits of stable surrogate keys.

Example Scenarios

Scenario 1: E-commerce Orders

In an e-commerce platform, each order might have a unique Order Number provided by the system. While the user sees this number, internally, a surrogate key is usually preferred for database operations to maintain consistency, especially when integrating with multiple external systems where the public order number might change or be formatted differently.

Scenario 2: University Enrollment

A university might use student IDs as natural keys, as these are unique within the context of the university. However, if the system needs to integrate globally with other institutions, surrogate keys assist in maintaining uniqueness across differing ID systems.

Best Practices

  1. Ensure Uniqueness: Whether using natural or surrogate keys, ensure that keys are unique within their respective contexts.
  2. Minimize Key Size: Particularly with composite natural keys, try to minimize the number of columns to avoid performance issues.
  3. Consider Indexing: Index primary keys for improved database performance and faster query execution.
  4. Assess Relationships: Understand constraints between tables and choose primary keys emphasizing relational integrity.

Summary Table

CriteriaNatural KeysSurrogate Keys
ReadabilityHigh due to real-world meaningLow, no inherent meaning
StabilityCan be volatile if source changesHighly stable, unlikely to change
ComplexityHigh, may require multiple columnsLow, usually single column
DuplicationIssues if real-world values repeatNo duplication issues
Use Case SuitabilitySmall, simple datasetsLarge, complex systems

Conclusion

The selection of primary keys is pivotal in database design and can significantly impact the performance, clarity, and integrity of the database. While natural keys offer readability by aligning with real-world data, surrogate keys provide stability and simplicity. Understanding the context and requirements of your database allows for informed choices in primary key selection, enhancing both functionality and efficiency in your database systems.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.