Database Design
Entity Relationship Model
Master the foundational modeling approach that transforms business requirements into database structures through entities, attributes, and relationships.
What Is the Entity Relationship Model?
The Entity Relationship Model (ER Model) provides a structured way to represent real-world data and its connections. Think of it as the blueprint language for databases — just like architects use standard symbols to design buildings.
Every major database system today — from Netflix's recommendation engine to Stripe's payment processing — starts with ER modeling. Amazon's product catalog? Built on ER principles. Airbnb's booking system? Same foundation.
The model breaks down complex business scenarios into three core components:
Entities
Real-world objects like customers, books, or orders that store data
Attributes
Properties that describe entities — like a customer's email or book title
Relationships
Connections between entities — customers place orders, books belong to categories
Constraints
Rules that govern relationships — one customer can place many orders
Core ER Components
Understanding each component deeply prevents design disasters. Honestly, most failed databases trace back to unclear entity definitions or missing relationships.
Entities: The Building Blocks
An entity represents anything that exists independently and matters to your business. For BookVault, we identify five core entities:
first_name
last_name
city
created_at
FK customer_id
order_date
status
total_amount
Notice how each entity has a primary key (PK) — the unique identifier. Customer #12543 always refers to the same person. Order #98765 always refers to the same purchase. This uniqueness prevents data chaos.
Attributes: Entity Properties
Attributes describe what we know about each entity. But not all attributes behave the same way:
| Attribute Type | Description | BookVault Example |
| Simple | Cannot be broken down further | first_name, email |
| Composite | Made up of multiple parts | Full name (first + last) |
| Derived | Calculated from other attributes | Customer age (from birth_date) |
| Multi-valued | Can have multiple values | Author phone numbers |
Common Mistake
Storing composite attributes as single values. Don't store "John Smith" in one field — separate first_name and last_name for sorting and searching flexibility.
Relationships: Making Connections
Relationships connect entities in meaningful ways. They're not just lines on a diagram — they represent real business rules that your database must enforce.
Consider BookVault's core business relationship: customers place orders. This creates a one-to-many relationship because:
- One customer can place multiple orders
- Each order belongs to exactly one customer
- No shared orders between customers
sarah@email.com
Cardinality and Participation
Two critical concepts define how relationships actually work: cardinality (how many) and participation (must or optional).
Cardinality Types
One-to-One (1:1)
Each customer has exactly one profile record. Each profile belongs to one customer.
Use when: Splitting large entities
One-to-Many (1:M)
One customer places many orders. Each order belongs to one customer.
Most common relationship type
Pro Tip: Many-to-many relationships always require a junction table. Books and categories demonstrate this — one book can belong to multiple categories, one category contains multiple books.
Participation Constraints
Participation defines whether entities must participate in relationships:
| Constraint Type | Rule | BookVault Example |
| Total (Mandatory) | Entity must participate | Every order must have a customer |
| Partial (Optional) | Entity may participate | Customers don't need orders to exist |
From ER Model to Database Tables
The real magic happens when you transform your ER model into actual database tables. Each entity becomes a table. Each attribute becomes a column. Each relationship becomes a foreign key connection.
Here's how BookVault's customer-order relationship translates to SQL:
-- Create customers entity as a table
-- Primary key ensures each customer is unique
CREATE TABLE customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
city VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Table 'customers' created successfully.
Indexes: PRIMARY KEY (customer_id)
UNIQUE KEY (email)
What just happened?
The AUTO_INCREMENT ensures unique customer IDs. The UNIQUE constraint on email prevents duplicate accounts. Try this: Add a phone number attribute and decide if it should be required.
-- Create orders entity with foreign key relationship
-- customer_id links back to customers table
CREATE TABLE orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
status VARCHAR(20) DEFAULT 'pending',
total_amount DECIMAL(10,2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Table 'orders' created successfully.
Indexes: PRIMARY KEY (order_id)
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
Constraints: customer_id NOT NULL enforces total participation
What just happened?
The FOREIGN KEY constraint enforces our relationship rule — every order must belong to an existing customer. The NOT NULL on customer_id implements total participation. Try this: Attempt to insert an order without a valid customer_id and see the error.
Handling Complex Relationships
Many-to-many relationships require special handling. Books can belong to multiple categories, and categories contain multiple books. The solution? A junction table that holds both foreign keys:
-- Junction table for many-to-many relationship
-- Each row represents one book in one category
CREATE TABLE book_categories (
book_id INT NOT NULL,
category_id INT NOT NULL,
PRIMARY KEY (book_id, category_id),
FOREIGN KEY (book_id) REFERENCES books(book_id),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
Table 'book_categories' created successfully.
Indexes: PRIMARY KEY (book_id, category_id)
FOREIGN KEY (book_id) REFERENCES books(book_id)
FOREIGN KEY (category_id) REFERENCES categories(category_id)
Note: Composite primary key prevents duplicate book-category pairs
Data Insight
Junction tables typically contain 40-60% more rows than the sum of their parent tables. BookVault's 10,000 books across 50 categories might generate 15,000 book_categories records.
Common ER Modeling Mistakes
Even experienced developers make these errors. Recognizing them early saves massive refactoring pain:
Entity vs Attribute Confusion
Making "Address" an entity when it should be attributes. Unless you need to track address history or share addresses between entities, use street, city, postal_code columns instead.
Missing Business Rules
Forgetting to model constraints like "customers can only have one active subscription" or "orders cannot be modified after shipping." These become expensive bugs later.
The key principle: model what matters to the business, not just what exists today. Think about future requirements during initial design.
Quiz
1. BookVault needs to track which authors write which books, where one author can write multiple books and one book can have multiple authors. How should this many-to-many relationship be implemented?
2. In BookVault's customer entity, the email attribute "sarah@bookstore.com" would be classified as which type of attribute?
3. In the customer-order relationship where "customers place orders," what participation constraint should orders have in this relationship?
Up Next
ER Diagram Symbols
Master the visual language of database design with standardized symbols for entities, relationships, and constraints that communicate your data model clearly.