Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Database Design & Modeling
DBMS

Database Design & Modeling

Master the complete database design process — from requirement analysis to physical design — and learn how to create robust, scalable database schemas.

Database Design & Modeling

Database design is the process of defining the structure of a database. A well-designed database is fast, maintainable, and correct. A poorly designed one leads to slow queries, data inconsistency, and maintenance nightmares.

This chapter covers the complete design process: from understanding requirements to creating the physical schema.


Learning Objectives

After completing this chapter, you will be able to:

  • Describe the phases of database design.
  • Gather and analyze requirements.
  • Create conceptual, logical, and physical designs.
  • Choose appropriate primary keys and data types.
  • Follow naming conventions.
  • Understand design tradeoffs.
  • Answer design-related interview questions.

The Database Design Process

Database design follows a structured process with clear phases.

Requirement Analysis

Conceptual Design (ER Model)

Logical Design (Relational Model)

Normalization

Physical Design (Indexes, Partitions)

Implementation (SQL DDL)

Testing & Deployment

Maintenance

Phase 1: Requirement Analysis

Goal: Understand what data needs to be stored and how it will be used.

Questions to Answer

  • What are the main entities? (Customer, Order, Product)
  • What information about each entity matters? (Name, price, date)
  • How are entities related? (A customer places many orders)
  • What business rules apply? (An order must have at least one item)
  • How will the data be queried? (Find all orders by a customer)
  • What are the performance requirements? (Sub-second response on 10M rows)

Deliverables

  • List of entities and attributes.
  • List of business rules.
  • Sample queries and reports.
  • Performance and security requirements.

Phase 2: Conceptual Design

Goal: Create a technology-independent model of the data.

What You Do

  • Identify entities and attributes.
  • Define relationships and cardinality.
  • Identify primary keys.
  • Note weak entities and constraints.

Tool

Entity-Relationship (ER) Diagram.

Example

An e-commerce conceptual model:

Customer (1) ── Places ── (N) Order (1) ── Contains ── (N) Product

Customer: Customer_ID, Name, Email, Phone
Order: Order_ID, Order_Date, Total
Product: Product_ID, Name, Price

Phase 3: Logical Design

Goal: Convert the ER model into relational tables (independent of any specific DBMS).

What You Do

  • Map each entity to a table.
  • Map attributes to columns.
  • Define primary keys and foreign keys.
  • Apply normalization rules.
  • Define constraints (UNIQUE, NOT NULL, CHECK).

Example

CREATE TABLE Customers (
    Customer_ID INT PRIMARY KEY,
    Name VARCHAR(100) NOT NULL,
    Email VARCHAR(255) UNIQUE,
    Phone VARCHAR(20)
);

CREATE TABLE Orders (
    Order_ID INT PRIMARY KEY,
    Customer_ID INT NOT NULL,
    Order_Date DATE DEFAULT CURRENT_DATE,
    Total DECIMAL(10,2),
    FOREIGN KEY (Customer_ID) REFERENCES Customers(Customer_ID)
);

CREATE TABLE Order_Items (
    Order_ID INT,
    Product_ID INT,
    Quantity INT NOT NULL,
    Price DECIMAL(10,2),
    PRIMARY KEY (Order_ID, Product_ID),
    FOREIGN KEY (Order_ID) REFERENCES Orders(Order_ID),
    FOREIGN KEY (Product_ID) REFERENCES Products(Product_ID)
);

CREATE TABLE Products (
    Product_ID INT PRIMARY KEY,
    Name VARCHAR(200) NOT NULL,
    Price DECIMAL(10,2) CHECK (Price > 0)
);

Phase 4: Normalization

Goal: Eliminate redundancy and prevent anomalies.

Process

  • Apply 1NF (atomic columns).
  • Apply 2NF (no partial dependency).
  • Apply 3NF (no transitive dependency).
  • Apply BCNF if needed.

Normalization is covered in detail in the Normalization section.


Phase 5: Physical Design

Goal: Decide how data will be stored and accessed.

Decisions

DecisionOptions
Primary Key typeAuto-increment INT, UUID, natural key
IndexesB+ Tree, hash, bitmap, composite, covering
PartitioningRange, list, hash — by date or region
Storage engineInnoDB (row), MyISAM (older), Columnar
File organizationHeap, sequential, hash, clustered
CompressionPage-level, column-level
ReplicationSynchronous, asynchronous, read replicas

Example

-- Physical design decisions
CREATE TABLE Orders (
    Order_ID BIGINT AUTO_INCREMENT PRIMARY KEY,  -- BIGINT for scale
    Customer_ID INT NOT NULL,
    Order_Date DATE NOT NULL,
    Total DECIMAL(12,2)
) ENGINE=InnoDB
  PARTITION BY RANGE (YEAR(Order_Date)) (
    PARTITION p2023 VALUES LESS THAN (2024),
    PARTITION p2024 VALUES LESS THAN (2025)
  );

CREATE INDEX idx_orders_customer ON Orders(Customer_ID);
CREATE INDEX idx_orders_date ON Orders(Order_Date);

Phase 6: Implementation

Goal: Write the DDL and create the database.

CREATE DATABASE ECommerce;
USE ECommerce;

-- Create tables (as designed above)
-- Create indexes
-- Create views for reporting
-- Create users and set permissions
-- Load initial data

Primary Key Selection

Choosing the right primary key is critical.

Key TypeExampleProsCons
Auto-increment INTINT AUTO_INCREMENTSimple, small, fastHard for replication, exposed sequential IDs
UUIDCHAR(36)Globally unique, good for distributed systemsLarger (36 bytes), slower inserts
Natural KeySSN, EmailHas business meaningChanges over time, reuse issues
Composite Key(Order_ID, Product_ID)Naturally unique for junction tablesLarger FK references

Recommendation

Use BIGINT AUTO_INCREMENT for most tables. Use UUID only when data comes from distributed sources. Avoid natural keys as primary keys (use UNIQUE constraint instead).


Data Type Selection

DataRecommended TypeReason
IDsINT or BIGINTFast, auto-increment
NamesVARCHAR(100-255)Variable length, saves space
EmailVARCHAR(255)Standard max length
PhoneVARCHAR(20)Includes country codes, dashes
DatesDATE (no time) or TIMESTAMPTimezone-aware with TIMESTAMP
MoneyDECIMAL(10,2) or BIGINT (paise)DECIMAL avoids floating-point errors
Large textTEXT or VARCHAR(10000)TEXT for unlimited
BooleanBOOLEAN or TINYINT(1)MySQL: TINYINT; PostgreSQL: BOOLEAN

Naming Conventions

Consistent naming makes databases easier to understand.

ElementConventionExample
TablesPlural nounsCustomers, Orders, Products
ColumnsSnake_casecustomer_id, order_date
Primary Keytable_idcustomer_id, order_id
Foreign KeySame as referenced PKcustomer_id
Indexesidx_table_columnidx_orders_customer_id
Constraintschk_table_rulechk_products_price

Common Design Tradeoffs

TradeoffNormalized (3NF)Denormalized
StorageLess (no redundancy)More (duplicate data)
Write speedSlower (more tables, FKs)Faster (single table)
Read speedSlower (more joins)Faster (no joins)
Data integrityHigher (constraints)Lower (potential anomalies)
MaintenanceEasier (one fact, one place)Harder (update multiple copies)

When to Denormalize

  • Reporting tables: Pre-joined for analytics.
  • High-read, low-write systems: Cache joins in the table.
  • Time-series data: Store pre-computed aggregates.

Interview Deep Dive

Q: What are the main steps in database design?

A: (1) Requirement analysis — understand what data is needed, (2) Conceptual design — create an ER model, (3) Logical design — convert ER to tables, apply normalization, (4) Physical design — choose indexes, partitions, storage, (5) Implementation — write SQL DDL.

Q: Should you use INT or UUID as a primary key?

A: Use INT (or BIGINT) for most applications — it is smaller (4-8 bytes), faster for indexing, and auto-increment is simple. Use UUID only in distributed systems where multiple servers generate IDs independently and need global uniqueness. UUIDs are 16 bytes (or 36 as string) and cause index fragmentation.

Q: When should you denormalize a database?

A: Denormalize when read performance is critical and the data is read-heavy with infrequent writes. Examples: reporting dashboards (pre-join tables), caching layers (store pre-computed totals), and read-heavy APIs where join overhead is unacceptable. Always start normalized, then denormalize only when performance measurements justify it.

Q: Why should primary key columns match foreign key columns?

A: Matching names make joins obvious — ON customer_id = customer_id is immediately clear. It also helps tools (ORMs, query builders) auto-detect relationships. If the PK is id in every table, joins look like ON id = customer_id which is confusing.


Key Takeaways

  • Database design follows 6 phases: requirements → conceptual → logical → normalization → physical → implementation.
  • Always start with requirement analysis — understand what your data looks like before designing.
  • The ER model is technology-independent; the relational model is DBMS-independent.
  • Choose primary keys carefully — prefer synthetic keys (BIGINT) over natural keys.
  • Follow naming conventions for consistency.
  • Design for normalized form first; denormalize only when measured performance requires it.
  • The right data types and indexes make a significant performance difference.

My Private Notes

Notes are auto-saved locally to this device.