Checking your session…
Module 09: Data Modeling and ER Diagrams

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:

TEXT
ENTITY
  ↓
ATTRIBUTES
  ↓
RELATIONSHIP

ER Diagram → Visual blueprint of a database.

ER Diagram Basics: Entity, Attribute & Relationship Classic Chen Notation: Entity = Rectangle | Attribute = Oval | Relationship = Diamond emp_id (PK) emp_name salary EMPLOYEE [ Entity ] WORKS IN [ Relationship ] DEPARTMENT [ Entity ] dept_id (PK) dept_name Entity (Thing) Attribute (Property) Relationship (Connection)
Figure 1: An ER diagram represents entities (rectangles), their attributes (ovals), and the relationships between entities (diamonds).

Entities

An entity represents a real-world object or concept that we want to store data about.

Examples:

TEXT
Student
Employee
Customer
Product
Department
Order

For example, in a college database:

TEXT
Student
Course
Department

can all be entities.


Attributes

An attribute describes a property of an entity.

For example, an Employee may have:

TEXT
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:

TEXT
Employee
   ↓
works in
   ↓
Department

This tells us that an employee belongs to a department.

Common relationship examples:

TEXT
Customer → places → Order
Student  → enrolls in → Course
Employee → works in → Department

Relationship = Connection between entities

One-to-Many Relationship (1 : N) One department employs multiple employees; each employee belongs to one department DEPARTMENT PK dept_id dept_name 1 N MANY Emp 1: Ravi Kumar (dept_id = 10) Emp 2: Anjali Mehta (dept_id = 10) Emp 3: Tom Watson (dept_id = 10) 1 Department : Many Employees (Implemented via dept_id Foreign Key)
Figure 2: A one-to-many relationship allows one department to be associated with many employees.

ER Diagram and SQL Tables

An ER diagram is often created before implementing the database tables.

For example:

TEXT
ER DESIGN
   ↓
TABLE DESIGN
   ↓
SQL IMPLEMENTATION

The relationship:

TEXT
Department
     ↓
   Employee

can later be implemented using a foreign key.

SQL
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.

From ER Diagram to SQL Relational Tables Entities transform into Tables; Relationships transform into Foreign Keys ER MODEL DESIGN DEPARTMENT (1) works in EMPLOYEE (∞) SQL DDL DEPARTMENTS Table PK dept_id INT dept_name VARCHAR(50) EMPLOYEES Table PK emp_id INT emp_name VARCHAR(100) FK dept_id INT ──► REFERENCES departments(dept_id)
Figure 3: An ER relationship can be implemented in SQL tables using keys such as a foreign key.

Simple ER Example

Consider an online shopping system.

Entities:

TEXT
Customer
Product
Order

Relationships:

TEXT
Customer → places → Order
Order → contains → Product

A simplified ER design might look like:

TEXT
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

TEXT
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.

TEXT
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

  1. Identify the entities in a college database.
  2. Give three attributes for a Student entity.
  3. Give an example of a relationship between Student and Course.
  4. Draw an ER diagram for Department and Employee.
  5. Identify the one-to-many relationship in your diagram.
  6. Explain how that relationship could be implemented using a foreign key:
SQL
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

TEXT
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