Why ER diagrams?
An ER (Entity–Relationship) diagram is a picture of a database: which tables exist, what columns they have, and how they are related.
- To explain the design to non-technical people. Suppose a senior person in your company isn't technical and you need to explain how the database is organised. You can't open phpMyAdmin and say "look, these are my tables". An ER diagram shows what's going on behind the scenes in a way anyone can follow.
- As a blueprint. You design the ER diagram first, discuss it with your team and seniors, and only then create the database. Changing a drawing is cheap; changing a live database isn't.
Designing a database from requirements is called schema design, and it's very important in industry. People who are good at it have a better chance of becoming software architects.
The tool: Lucidchart
Many tools can draw ER diagrams, and some must be installed. Nitish uses Lucidchart because it's online and needs no setup:
- Sign up (the free plan is enough; it limits how many documents you can have at once). Logging in with Google works.
- Click New → Start with a template.
- Search for "ER diagram" and pick the Database ER diagram (crow's foot) template.
- Delete the sample content, rename the document (e.g. Swiggy database) and start drawing.
How to build one: the Swiggy example
Nitish designs the database behind a food delivery app like Swiggy. Think about how you order food and which "things" are involved.
Step 1 · Find the entities
An entity is a real-world thing you can make a table about. For Swiggy: the customer who places the order, the restaurant, the food items, and the order itself. You could add more (delivery partner, payments, reviews…), but four are enough to learn the idea.
Step 2 · Give each entity its attributes and types
| Entity | Attributes (type) |
|---|---|
| customers | cid INT (PK) · name VARCHAR(255) · email VARCHAR(255) · phone VARCHAR(10) |
| restaurants | rid INT (PK) · name VARCHAR(255) · cuisine VARCHAR(255) · rating DECIMAL(2,1) · address VARCHAR(255) |
| food | fid INT (PK) · name VARCHAR(255) · type VARCHAR(10) (veg / non-veg) · price INT |
| orders | oid INT (PK) · cid INT (FK) · rid INT (FK) · order_time DATETIME |
- Phone as VARCHAR(10), not INT. A student asked why. You never do maths on a phone number, so store it as text.
- Rating as DECIMAL. Values like 4.2 or 3.5.
- Price as INT. Prices like ₹125; no paise needed here.
- Address as a long VARCHAR. Addresses can be long.
- In
orders,cidandridare foreign keys: they reuse the primary keys ofcustomersandrestaurants, and unlike a primary key they can repeat.
Step 3 · Connect entities with relationships
For every pair of related entities, just ask: what's the cardinality between these two, 1:1, 1:N or M:N? Then draw the line with the right crow's foot ends.
One-to-many. One customer can place many orders, but every order belongs to exactly one customer. On the order side it's "zero or many" (some customers never order); on the customer side it's "one and only one".
One-to-many. A restaurant receives many orders, but each order is placed with exactly one restaurant (you can't order from multiple restaurants in one order).
Many-to-many. An order contains one or more food items, and a food item can appear in zero, one or many orders.
As we learned in Chapter 4, M:N needs a third table. The dashed order_items(oid, fid, quantity) table in the diagram is that junction table. (I've added it explicitly; the class drew the M:N link directly between orders and food.)
This diagram was made quickly while talking in class, so it isn't perfectly accurate. In industry you'd build it after a proper discussion with your team and seniors. The ER diagram is the database's blueprint, which is why the step matters so much.
Step 4 · Export to SQL and create the database
Once the diagram is done, Lucidchart can export it as SQL (choose MySQL). Nitish then:
- created a new empty database in phpMyAdmin,
- opened its SQL tab, pasted the exported code and clicked Go,
- fixed a typo the export had picked up from the diagram (a misspelled
DECIMAL), and ran it again.
All four tables appeared, empty but with the full structure and relationships. Making a database from a diagram is that easy. The exported SQL looks roughly like this (you'll learn every line in Chapter 7):
CREATE TABLE customers (
cid INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
email VARCHAR(255),
phone VARCHAR(10)
);
CREATE TABLE restaurants (
rid INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
cuisine VARCHAR(255),
rating DECIMAL(2,1),
address VARCHAR(255)
);
CREATE TABLE food (
fid INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255),
type VARCHAR(10),
price INT
);
CREATE TABLE orders (
oid INT PRIMARY KEY AUTO_INCREMENT,
cid INT,
rid INT,
order_time DATETIME,
FOREIGN KEY (cid) REFERENCES customers(cid),
FOREIGN KEY (rid) REFERENCES restaurants(rid)
);
-- junction table for the M:N between orders and food
CREATE TABLE order_items (
oid INT,
fid INT,
quantity INT,
PRIMARY KEY (oid, fid),
FOREIGN KEY (oid) REFERENCES orders(oid),
FOREIGN KEY (fid) REFERENCES food(fid)
);
Practice idea
Nitish's suggestion: take apps you use every day (WhatsApp, Instagram, Uber…) and try drawing their ER diagram. When you study computer science you'll build such applications yourself, and thinking about how their database could look is excellent practice for schema design.
Conceptual questions
These check whether you understand why, not just what. Several go one step beyond the video.
Why design the ER diagram before writing CREATE TABLE statements?
The ER diagram is the blueprint.
In the Swiggy design, why is cid stored in orders rather than a list of order ids stored in customers?
A list inside a column breaks first normal form and can't be indexed or joined properly.
Why did the design need an order_items table?
M:N always becomes two 1:N relationships through a junction table.
Where would a quantity attribute (how many of a dish in an order) belong?
It describes the pair (this order, this dish), so it lives in the junction table.