Relational Model and ER Design

Model data with entities, relationships and cardinalities, turn ER diagrams into tables, and understand keys and integrity constraints.

Beginner · about 5 hours · difficulty 2/5

DBMS is one of the three core CS subjects in placements (with OS and networks), and schema design questions show up in LLD and backend rounds too. Start with how to model data before writing any SQL.

The relational model

  • Data lives in relations (tables) of tuples (rows) with typed attributes (columns).
  • Keys: a super key uniquely identifies rows; a candidate key is a minimal super key; the chosen one is the primary key; others are alternate keys. A foreign key references another table's key.
  • Integrity constraints: entity integrity (primary key not null), referential integrity (foreign keys point to real rows), plus UNIQUE, NOT NULL, CHECK.
  • DBMS vs file systems: concurrency, recovery, integrity and a query language. Know the three-schema architecture and data independence.

ER modeling

  • Entities (Student, Course), attributes (simple, composite, multivalued, derived), and relationships (Enrolls).
  • Cardinality: one-to-one, one-to-many, many-to-many. Participation: total vs partial.
  • Weak entities depend on an owner for identity (an OrderItem needs its Order).
  • Specialisation and generalisation (optional).

ER to tables

  • One-to-many: put the foreign key on the "many" side.
  • Many-to-many: create a junction table with both foreign keys, such as enrollments(student_id, course_id, grade).
  • Multivalued attributes become their own table.

Pitfalls

Storing comma-separated lists in a column, using names as primary keys, and forgetting that a many-to-many relationship often has its own attributes.

Good enough to move on: you can design the schema for a college portal or a food delivery app with correct keys and relationships in 15 minutes.

Key concepts

  • Tables, rows and attributes
  • Super, candidate, primary and foreign keys
  • Integrity constraints
  • Entities, attributes and relationships
  • Cardinality and participation
  • ER diagram to tables
  • Weak entities
  • Three-schema architecture

Mini project

Draw an ER diagram for a college placement portal (students, companies, drives, applications, interview rounds), convert it to tables with primary and foreign keys, and create it in PostgreSQL or MySQL with sample data.

Check yourself

How do you represent a many-to-many relationship between students and courses that also stores each student's grade, and what is the primary key of that table?

Free resources