Practice and reinforce the concepts from Lesson 9
The Challenge: FoodRush, a food delivery startup, needs to design their first database. They need to store information about customers, restaurants, and orders. Your job is to create an ER (Entity-Relationship) diagram to visualize how these entities relate to each other.
Learning Goal: Master the basics of ER diagram creation and understand entity relationships.
Create a simple ER diagram for a food delivery system with three main entities: Customer, Restaurant, and Order.
💡 Tip: For advanced data modeling theory, Docker deployment, and production-scale ER diagrams, see Reference/Database-Theory/Data-Modeling-Theory.mdx
FoodRush needs to track the following information:
About Customers:
About Restaurants:
About Orders:
Let's break down our business requirements into database entities:
Now let's determine how these entities relate to each other:
Customer to Order Relationship
Restaurant to Order Relationship
Customer to Restaurant Relationship
Create your ER diagram using these symbols:
Rectangles = Entities (Customer, Restaurant, Order) Ovals = Attributes (name, email, phone, etc.) Diamonds = Relationships (Places Order, Fulfills Order) Lines = Connect entities to relationships
CUSTOMER ORDER RESTAURANT
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ customer_id │◄─────────┐ │ order_id │ ┌─────────►│restaurant_id│
│ name │ │ │customer_id │ │ │ name │
│ email │ └─│restaurant_id│─┘ │cuisine_type │
│ phone │ │ order_date │ │ address │
│ address │ │total_amount │ │ phone │
└─────────────┘ │ status │ └─────────────┘
└─────────────┘
│ │ │
│ │ │
▼ ▼ ▼
"Places" "Connects" "Fulfills"
Relationship Customers & Relationship
(1:M) Restaurants (1:M)
You can also create ER diagrams using:
Transform your ER diagram into actual database tables:
-- Create Customer table
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
phone VARCHAR(20),
address TEXT
);
-- Create Restaurant table
CREATE TABLE restaurants (
restaurant_id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
cuisine_type VARCHAR(50),
address TEXT,
phone VARCHAR(20)
);
-- Create Order table with foreign key relationships
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INTEGER REFERENCES customers(customer_id),
restaurant_id INTEGER REFERENCES restaurants(restaurant_id),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10,2),
status VARCHAR(20) DEFAULT 'pending'
);
Notice how:
Insert sample data to verify your design works:
-- Insert sample customers
INSERT INTO customers (name, email, phone, address) VALUES
('John Smith', 'john@email.com', '555-1234', '123 Main St'),
('Alice Johnson', 'alice@email.com', '555-5678', '456 Oak Ave');
-- Insert sample restaurants
INSERT INTO restaurants (name, cuisine_type, address, phone) VALUES
('Pizza Palace', 'Italian', '789 Food St', '555-PIZZA'),
('Burger Barn', 'American', '321 Eat Ave', '555-BURG');
-- Insert sample orders (linking customers and restaurants)
INSERT INTO orders (customer_id, restaurant_id, total_amount, status) VALUES
(1, 1, 25.99, 'delivered'), -- John ordered from Pizza Palace
(2, 1, 18.50, 'preparing'), -- Alice ordered from Pizza Palace
(1, 2, 12.75, 'pending'); -- John ordered from Burger Barn
-- Test relationships with queries
SELECT
c.name AS customer_name,
r.name AS restaurant_name,
o.total_amount,
o.status
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN restaurants r ON o.restaurant_id = r.restaurant_id;
Test your ER diagram by answering these business questions:
SELECT COUNT(*)
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.name = 'John Smith';
SELECT DISTINCT r.name
FROM restaurants r
JOIN orders o ON r.restaurant_id = o.restaurant_id;
SELECT SUM(o.total_amount) as total_revenue
FROM orders o
JOIN restaurants r ON o.restaurant_id = r.restaurant_id
WHERE r.name = 'Pizza Palace' AND o.status = 'delivered';
If you can answer these questions easily, your ER diagram is working correctly!
✅ Entities Identified: Customer, Restaurant, Order ✅ Attributes Defined: Each entity has appropriate attributes ✅ Primary Keys: Each entity has a unique identifier ✅ Relationships Mapped: One-to-Many relationships established ✅ Foreign Keys: Links between tables created ✅ Business Rules Enforced: Design supports business requirements
To extend this ER diagram, you could add:
Congratulations! You've created your first ER diagram and transformed it into working database tables. You understand how entities relate to each other and can create foreign key relationships to maintain data integrity.
This foundation prepares you for more complex database designs with multiple entities and relationships.
📚 Theory Reference: For advanced data modeling concepts, Docker deployment strategies, automated ER diagram generation, and production-scale database architecture, see Reference/Database-Theory/Data-Modeling-Theory.mdx
Complete this activity and submit your work through the Activity Submission Form