Entity Relationship Model: Entities, Attributes, Relationships and ER Diagrams
Building a relational database without an ER model is like constructing a building without architectural drawings — you might start fine, but problems compound as complexity grows. This guide to the er model in dbms, developed originally by Peter Chen in 1976, gives you the architect's blueprint: how to identify entities, their attributes, and the relationships between them before you write a single CREATE TABLE statement.
What is the Entity Relationship Model?
The Entity Relationship (ER) model is a conceptual framework for designing a database before writing any SQL. It captures three things: entities, the real-world objects you need to track, like Student or Course; attributes, the properties of those entities, like name or price; and relationships, how entities connect to each other, like a Student enrolling in a Course. An er diagram tutorial visualizes all of this, and the er model to relational schema translation is what turns that diagram into actual SQL tables.
What You'll Learn
- Define entity, attribute, and relationship in the context of the ER model.
- Distinguish attribute types: composite, multivalued, derived, and key attributes.
- Identify one-to-one, one-to-many, and many-to-many relationship types with examples.
- Convert an ER diagram into a normalized set of MySQL CREATE TABLE statements.
Key Terms to Know
- Entity: A real-world object, person, or event tracked in the database, such as Student or Order.
- Attribute: A property of an entity, such as student_id, name, or price.
- Key attribute: The attribute that uniquely identifies each entity instance, mapped to a primary key.
- Composite attribute: An attribute that splits into sub-attributes, like address into street, city, and pincode.
- Cardinality: How many entity instances can participate in a relationship — one-to-one, one-to-many, or many-to-many.
Entities, Attributes, Relationships: The Building Blocks of ER Modeling
Building an ER model starts with identifying entities, the objects your system actually needs to track. For an e-learning platform, that's Users, Courses, Lessons, and Enrollments. Each entity gets attributes: a User has user_id (the key attribute), full_name, email, and date_of_birth. Then come relationships: a User enrolls in a Course, a Course contains Lessons.
Not every attribute is simple. A composite attribute like address splits into street, city, and pincode. A multivalued attribute, like multiple phone numbers for one student, can't fit in a single column and needs its own table. A derived attribute, like age, shouldn't be stored at all, since it can always be calculated from date_of_birth.
Cardinality in ER Model: One-to-One, One-to-Many, and Many-to-Many
Cardinality in er model terms describes how many instances of one entity relate to instances of another. One Course containing many Lessons, where each Lesson belongs to exactly one Course, is the simpler side of a one to many many to many relationship spectrum: a 1:N relationship, implemented with a straightforward foreign key on the 'many' side.
A User enrolling in many Courses, where one Course also has many enrolled Users, is genuinely many-to-many (M:N). This is the relationship type that can't be represented with a single foreign key in either table.
Junction Table SQL: Implementing Many-to-Many Relationships
A junction table sql design is exactly what resolves a many-to-many relationship. The Enrollments table sits between Users and Courses, holding user_id and course_id as foreign keys, combined into a composite primary key that also blocks the same user from enrolling in the same course twice.
This pattern, entity becomes a table, attribute becomes a column, key attribute becomes a primary key, and M:N relationship becomes a junction table, is the entire er model to relational schema translation in a nutshell.
Visual Summary
Picture three connected boxes. USER (user_id, full_name, email) connects to ENROLLMENT (user_id FK, course_id FK, enrolled_at, progress_pct) which connects to COURSE (course_id, title, price). Separately, COURSE connects directly to LESSON (lesson_id, course_id FK, title, duration). USER to ENROLLMENT is 1:N, COURSE to ENROLLMENT is 1:N, COURSE to LESSON is 1:N, and USER to COURSE through ENROLLMENT is M:N overall.
ER Model to SQL Mapping at a Glance
| ER Concept | What it represents | SQL implementation | Example |
|---|---|---|---|
| Entity | Real-world trackable object | Table | User, Course, Order |
| Attribute | Property of entity | Column | user_name, price, order_date |
| Key attribute | Unique identifier | PRIMARY KEY column | user_id, course_id |
| Derived attribute | Calculated from stored data | Not stored; computed in query | age from date_of_birth |
| 1:1 relationship | One instance matches one | FK in either table | User has one Profile |
| 1:N relationship | One instance to many | FK in the 'many' table | Course has many Lessons |
| M:N relationship | Many to many | Junction table with two FKs | User enrolls in Courses |
SQL Example
-- Converting the E-Learning ER model to MySQL tables
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
full_name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE NOT NULL,
dob DATE -- age is a derived attribute, computed in queries, not stored
);
CREATE TABLE courses (
course_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(200) NOT NULL,
price DECIMAL(10,2) NOT NULL DEFAULT 0.00
);
CREATE TABLE lessons (
lesson_id INT PRIMARY KEY AUTO_INCREMENT,
course_id INT NOT NULL,
title VARCHAR(200) NOT NULL,
FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE
);
-- Junction table resolving the User <-> Course many-to-many relationship
CREATE TABLE enrollments (
user_id INT NOT NULL,
course_id INT NOT NULL,
enrolled_at DATETIME DEFAULT CURRENT_TIMESTAMP,
progress_pct TINYINT DEFAULT 0 CHECK (progress_pct BETWEEN 0 AND 100),
PRIMARY KEY (user_id, course_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE
);
-- Find all courses a specific user is enrolled in
SELECT c.title, e.progress_pct, e.enrolled_at
FROM enrollments e
JOIN courses c ON c.course_id = e.course_id
WHERE e.user_id = 1
ORDER BY e.enrolled_at DESC;
This SQL directly translates the ER diagram into working tables. The users and courses tables represent entity sets with their attributes, lessons has a 1:N relationship with courses through a foreign key, and enrollments resolves the M:N relationship between users and courses with a composite primary key that blocks duplicate enrollment. ON DELETE CASCADE ensures that deleting a course automatically cleans up its lessons and enrollments too.
Real-World Examples
- Hospital management systems identify entities like Patient, Doctor, and Appointment, where Patient and Doctor have a many-to-many relationship resolved through the Appointment junction table.
- Banking systems identify Customer, Account, and Transaction as entities, where one Customer can have many Accounts and one Account records many Transactions, both clean 1:N relationships.
- HR systems model Employee and Project as entities with a many-to-many relationship, resolved through a Project_Assignments junction table, since one employee works on many projects and one project has many employees.
- Zomato-style apps identify entities like User, Restaurant, Menu_Item, and Order, where each entity's attributes and relationships form a complete schema supporting hundreds of SQL queries.
Best Practices and Pro Tips
- Sketch the ER diagram on paper or a whiteboard before opening MySQL Workbench. The moment you start writing CREATE TABLE statements first, it becomes much harder to spot a missing junction table or a relationship that should be 1:N instead of M:N.
- When in doubt about whether something is an entity or just an attribute, ask if it needs its own attributes or relationships. If 'category' only ever needs a name, it might be an attribute; if it needs a description and its own relationships, it's probably its own entity.
- Resist the urge to store derived attributes like age or total_order_amount directly in a table. It's tempting for query performance, but the moment the source data changes and you forget to update the derived column, your data quietly becomes wrong.
Common Mistakes to Avoid
- Jumping straight to writing SQL tables without planning an ER model, leading to schemas that need expensive changes later.
- Trying to implement an M:N relationship with a single foreign key or comma-separated values in one column — always use a junction table.
- Storing derived attributes like age or total_price and forgetting to keep them updated, instead of calculating them from source data in queries.
Interview Questions
Q1. What is an entity in the ER model?
An entity is a real-world object, person, concept, or event relevant to the system being modeled, like Student, Product, or Order. In SQL, an entity is implemented as a table.
Q2. How do you represent a many-to-many relationship in a relational database?
A many-to-many relationship can't be directly stored in relational tables. It's resolved using a junction table with foreign keys referencing the primary keys of both related tables, like an Enrollments table with user_id and course_id.
Q3. What is cardinality in the ER model?
Cardinality defines how many instances of one entity can be associated with instances of another. One-to-one means exactly one match on each side, one-to-many means one A can relate to many B instances, and many-to-many means many A instances can relate to many B instances.
Practice MCQs
1. In the ER model, an entity is converted to what in SQL?
- A column
- A table
- A foreign key
- A transaction
Answer: B. A table
Explanation: Each entity in the ER model is implemented as a table in the relational schema.
2. A student can enroll in many courses and a course can have many students. What type of relationship is this?
- One-to-one
- One-to-many
- Many-to-many
- Hierarchical
Answer: C. Many-to-many
Explanation: When entities on both sides can be associated with multiple instances on the other side, it is a many-to-many relationship.
3. Which attribute type should NOT be stored directly in the table but calculated in queries instead?
- Key attribute
- Composite attribute
- Derived attribute
- Multivalued attribute
Answer: C. Derived attribute
Explanation: Derived attributes like age can be calculated from stored values like date_of_birth. Storing them creates redundancy and update inconsistency risks.
Quick Revision Points
- Entity maps to table; attribute maps to column; relationship maps to foreign key or junction table.
- Key attribute maps to a PRIMARY KEY column; composite attribute maps to multiple columns.
- Multivalued attribute needs its own table; derived attribute isn't stored at all.
- M:N relationships require a junction table with a composite primary key.
Conclusion
- ER modeling is the blueprint that prevents costly database design mistakes — time spent here saves hours of ALTER TABLE work later.
- Understanding cardinality is essential for correctly placing foreign keys and creating junction tables.
- The ER-to-SQL mapping is a concrete skill tested directly in practicals and interviews.
The Entity Relationship model is the conceptual design tool behind every well-built relational database. Entities become tables, attributes become columns, key attributes become primary keys, and relationships become foreign keys for one to many many to many cases that actually resolve to junction tables. Whether you're following a formal er diagram tutorial or sketching on a whiteboard, mastering this database design er model approach produces schemas that are logical, maintainable, and ready to support complex SQL queries from day one.