9.2 ER Diagrams — Entities, Attributes, Relationships
Level: 9 | Version: v1.1 | Author: Meptrasoft
Overview
An ER Diagram (Entity-Relationship Diagram) is a visual way to design and understand a database.
It shows:
- Entities → What things are stored
- Attributes → Properties of those things
- Relationships → How those things are connected
For example:
ENTITY
↓
ATTRIBUTES
↓
RELATIONSHIP
ER Diagram → Visual blueprint of a database.
Entities
An entity represents a real-world object or concept that we want to store data about.
Examples:
Student
Employee
Customer
Product
Department
Order
For example, in a college database:
Student
Course
Department
can all be entities.
Attributes
An attribute describes a property of an entity.
For example, an Employee may have:
Employee
├── emp_id
├── emp_name
├── department
└── salary
Here, emp_id, emp_name, department, and salary are attributes.
Entity = Thing
Attribute = Property of the thing
Relationships
A relationship describes how two or more entities are connected.
For example:
Employee
↓
works in
↓
Department
This tells us that an employee belongs to a department.
Common relationship examples:
Customer → places → Order
Student → enrolls in → Course
Employee → works in → Department
Relationship = Connection between entities
ER Diagram and SQL Tables
An ER diagram is often created before implementing the database tables.
For example:
ER DESIGN
↓
TABLE DESIGN
↓
SQL IMPLEMENTATION
The relationship:
Department
↓
Employee
can later be implemented using a foreign key.
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
dept_id INT REFERENCES departments(dept_id)
);
Here, dept_id connects the employee to the department.
Simple ER Example
Consider an online shopping system.
Entities:
Customer
Product
Order
Relationships:
Customer → places → Order
Order → contains → Product
A simplified ER design might look like:
Customer
↓
places
↓
Order
↓
contains
↓
Product
This gives the database designer a clear picture before creating the actual tables.
Common Beginner Mistakes
| ✗ Mistake | ✓ Correct Understanding |
|---|---|
| Entity and attribute are the same | Entity is the object; attribute describes it |
| A relationship is a table | A relationship describes how entities are connected |
| ER diagrams are only used after tables are created | They are often used during database design before implementation |
| Every entity must have only three attributes | An entity can have as many appropriate attributes as the design requires |
| A foreign key is the same as a relationship | The foreign key is a database implementation mechanism for representing certain relationships |
Placement Quick Points
ENTITY
→ REAL-WORLD OBJECT
ATTRIBUTE
→ PROPERTY OF ENTITY
RELATIONSHIP
→ CONNECTION BETWEEN ENTITIES
- Entity represents a real-world object or concept.
- Attribute describes an entity.
- Relationship connects entities.
- ER diagrams help visualize database structure before implementation.
- Relationships can be implemented using keys such as foreign keys.
- One-to-many is a common relationship pattern.
Interview Questions
- What is an ER diagram?
An ER diagram is a visual representation of entities, attributes, and relationships used to design a database.
- What is an entity?
An entity is a real-world object or concept about which data is stored.
- What is an attribute?
An attribute is a property that describes an entity.
- What is a relationship?
A relationship describes how two or more entities are connected.
- Give an example of a one-to-many relationship.
One department can have many employees.
Department 1 ↓ Employees Many- How is an ER relationship implemented in SQL?
It can be represented using keys such as a primary key and foreign key.
- Why are ER diagrams useful?
They help developers and database designers understand the structure and relationships of the data before implementing the database.
Practice & Hands-On Exercises
- Identify the entities in a college database.
- Give three attributes for a
Studententity. - Give an example of a relationship between
StudentandCourse. - Draw an ER diagram for
DepartmentandEmployee. - Identify the one-to-many relationship in your diagram.
- Explain how that relationship could be implemented using a foreign key:
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL
);
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100) NOT NULL,
dept_id INT REFERENCES departments(dept_id)
);
💡 Tip: Test your queries and schema definitions using the in-browser interactive runner above.
Key Takeaway
ENTITY
→ WHAT
ATTRIBUTE
→ DETAILS
RELATIONSHIP
→ HOW THEY CONNECT
ER diagrams provide a visual blueprint of a database by showing entities, their attributes, and the relationships between them.
End of ER Diagrams — Entities, Attributes, Relationships