ER Diagram
An ER (Entity-Relationship) diagram is a visual blueprint of a database's structure - it shows the entities (things you want to store data about), their attributes, and how the entities relate to one another, before any table is actually created.
Core Components
- Entity - a real-world object or concept, like Customer or Order, usually drawn as a rectangle
- Attribute - a property of an entity, like name or email, drawn as an oval
- Relationship - how two entities are connected, like "Customer places Order", drawn as a diamond
- Primary Key - the attribute that uniquely identifies each entity instance, usually underlined
Types of Relationships
- One-to-One - one record in Table A relates to exactly one record in Table B
- One-to-Many - one record in Table A relates to many records in Table B (e.g. one Customer, many Orders)
- Many-to-Many - many records in Table A relate to many records in Table B (e.g. Students and Courses)
Why ER Diagrams Matter
Designing an ER diagram before writing any SQL helps catch structural problems early - like missing relationships or duplicated data - and makes it much easier to translate the design into actual CREATE TABLE statements.
Drawing an ER Diagram Step by Step
- List the nouns in the requirement. These are candidate entities.
- Remove duplicates and attributes. "Customer name" is an attribute of Customer, not an entity of its own.
- Pick a primary key for every entity.
- Find the verbs that connect entities: places, contains, belongs to. These become relationships.
- Set cardinality for each relationship by asking "how many on each side?"
- Decide participation: must every order have a customer (mandatory), or can it exist without one (optional)?
- Review with someone who knows the business before turning it into tables.
Worked Example: An Online Store
Requirement: "Customers place orders. Each order contains one or more products, and a product can appear in many orders. We need to know the quantity of each product in an order."
- Entities: Customer (customer_id, name, email), Order (order_id, order_date), Product (product_id, name, price).
- Customer to Order: one-to-many. One customer places many orders; each order belongs to one customer.
- Order to Product: many-to-many, with an attribute quantity on the relationship itself.
A many-to-many relationship cannot be stored directly in relational tables, so it becomes a junction table holding both keys plus the relationship attribute:
CREATE TABLE customer (
customer_id INT PRIMARY KEY,
name VARCHAR(80) NOT NULL,
email VARCHAR(120) UNIQUE
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL REFERENCES customer(customer_id),
order_date DATE NOT NULL
);
CREATE TABLE product (
product_id INT PRIMARY KEY,
name VARCHAR(80) NOT NULL,
price DECIMAL(10,2) NOT NULL
);
CREATE TABLE order_item (
order_id INT REFERENCES orders(order_id),
product_id INT REFERENCES product(product_id),
quantity INT NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);
The one-to-many link simply places the customer's key inside the orders table as a foreign key.
Attribute Types and Weak Entities
- Composite attribute: can be split, such as address into street, city and pincode.
- Multivalued attribute: can hold several values, such as phone numbers; drawn as a double oval and usually stored in its own table.
- Derived attribute: calculated from others, such as age from date of birth; drawn as a dashed oval and often not stored at all.
- Weak entity: cannot be identified on its own, such as an order line that depends on its order; drawn as a double rectangle.
Besides the classic Chen notation of ovals and diamonds, many tools use crow's foot notation, where lines end in symbols showing "one" or "many". Both describe the same ideas.
Common ER Diagram Mistakes
- Treating an attribute as an entity, or the reverse.
- Leaving a many-to-many relationship unresolved when moving to tables.
- Storing derived values like order total without a reason, which can drift out of sync.
- Choosing changeable values such as email as the primary key.
- Skipping cardinality, which leaves developers guessing about the rules.