notesonly.in

One notebook for every subject — open it anywhere.

Log in

ER Model and Relational Model: Complete Beginner's Guide

The ER (Entity-Relationship) model is a visual way to design a database before building it: you draw the things you store (entities), their details (attributes), and how they connect (relationships). The relational model is what you implement — data in tables with rows and columns, linked by keys. The ER diagram is the blueprint; the relational schema is the building.

This matters because designing on paper first prevents expensive mistakes. Realising too late that one customer can have many orders — or that a student takes many courses — leaves you with duplicated, contradictory data. The ER diagram forces you to settle these questions before a single table exists.

Key Concepts

  • Entity: a real-world object you store data about, like Student. Drawn as a rectangle.
  • Attribute: a property of an entity, like Name. The key attribute (underlined) uniquely identifies each entity, e.g. StudentID.
  • Relationship: an association between entities, like Enrolls. Drawn as a diamond.
  • Cardinality: how many of each side participate — one-to-one (1:1), one-to-many (1:N), or many-to-many (M:N).
  • Primary key: the column(s) uniquely identifying each table row — never empty, never duplicated.
  • Foreign key: a column referring to another table's primary key — the link between tables.

How It Works

Fixed mapping rules convert an ER diagram into tables. Each entity becomes a table, its key attribute the primary key. A 1:N relationship becomes a foreign key on the "many" side — Orders gets a CustomerID column. An M:N relationship becomes its own table (a junction table) holding both foreign keys as a composite primary key — Enrollments(StudentID, CourseID). A 1:1 relationship merges into one table or links via a foreign key with a uniqueness rule.

Worked Example

Course-registration design. ER diagram: Student(StudentID, Name) and Course(CourseID, Title), with M:N relationship Enrolls storing Grade.

  1. Entities → tables: Students(StudentID primary key, Name); Courses(CourseID primary key, Title). 2 tables.
  2. M:N → junction table: Enrollments(StudentID foreign key, CourseID foreign key, Grade), keyed by (StudentID, CourseID). Total: 3 tables.
  3. Data: 3 students × 2 courses = 6 possible enrollments; 4 actually exist, so Enrollments holds 4 rows.
  4. Key count: 2 primary keys in entity tables + 1 composite key and 2 foreign keys in the junction table = 5 key constraints.
  5. Query "courses taken by S1": match S1's 2 Enrollments rows, follow 2 foreign keys into Courses — exactly 2 titles, no duplicates, no missing links.

Common Misconceptions

  • "The ER diagram is the database." It is only the design. The database is the tables, keys, and constraints derived from it.
  • "Every relationship needs its own table." Only M:N does. A 1:N link is one foreign-key column on the many side; a separate table just adds pointless joins.
  • "Primary key and foreign key are the same." A primary key identifies rows inside its own table; a foreign key points at another table's primary key. Different roles, even when one column plays both.

Real-World Applications

  • App backends: every web app's database starts as an ER-style design of users, orders, and posts.
  • Banking: accounts, customers, and transactions with strict key relationships.
  • E-commerce: products, categories, carts, and orders with 1:N and M:N links.
  • Hospital records: patients, doctors, appointments — where cardinalities are safety-critical.

Think of the ER model as the architect's drawing and the relational model as the construction: entities become tables, 1:N links become foreign keys, M:N links become junction tables. Learn the mapping rules once, and any real-world description becomes a clean database design on paper.