CMD Guide
HomeDatabases

ER Models

Step 6 in the Databases path · 7 concepts · 0 problems

0 / 7 complete

📘 Learn ER Models from zero

Start from zero. Before you ever write a SQL table, you need a blueprint of what your data describes and how the pieces connect. An Entity-Relationship (ER) model is that blueprint: a conceptual, technology-neutral description of the real-world "things" your system tracks and the associations between them.

Analogy: think of an architect's floor plan before a house is built. The plan names the rooms (entities), describes each room's features like square footage and window count (attributes), and shows which rooms connect through doorways (relationships). You design the plan on paper first; only later do you pour concrete (create tables).

The three core building blocks:

Worked example. Requirement: "Each employee works for exactly one department; a department employs many employees." Scan it: nouns Employee, Department become two rectangles. The verb works for becomes a diamond WORKS_FOR. "Exactly one" on the department side and "many" on the employee side make this a 1:N relationship (one department, many employees). Give Employee attributes SSN (key), name, salary; give Department dept_no (key), dept_name. That picture is the ER diagram, and it maps mechanically to two tables where the "N"-side table, Employee, carries a dept_no foreign key.

Key insight: an ER model turns an ambiguous English requirement into a precise, drawable structure — entity types, attributes, relationship types, and cardinality/participation constraints — that then translates almost mechanically into relational tables.

✨ Added by the guide to build intuition — not from the source course.

🎯 Guided practice

Problem 1 (Easy) — Identify and classify. Requirement: "A library has Books and Members. A Member can borrow many Books, and a Book can be borrowed by many Members over time. Each borrowing has a due date." Draw the ER model.

  1. Find entities (nouns): Book and Member are the two thing-categories → two rectangles.
  2. Find the relationship (verb): borrows → a diamond BORROWS connecting them.
  3. Determine cardinality: "a Member can borrow many Books" and "a Book can be borrowed by many Members" → both sides are "many" → this is an M:N relationship.
  4. Place the relationship attribute: due_date describes a specific borrowing, not the book and not the member, so it attaches to the BORROWS diamond — a relationship attribute, not an entity attribute.
  5. Map to tables: an M:N relationship always becomes a junction table. Note the "over time" wording: the same member can borrow the same book on different occasions, so (book_id, member_id) alone is not unique — the key must include the borrowing occasion. Result: Book(book_id, ...), Member(member_id, ...), and Borrows(book_id, member_id, borrow_date, due_date) with primary key (book_id, member_id, borrow_date) (or a surrogate loan_id), where book_id and member_id are foreign keys. Core pattern learned: M:N ⇒ separate junction table; relationship attributes live there; if the pairing repeats over time, the key must include a distinguishing attribute (or surrogate).

Problem 2 (Medium) — Weak entity + participation. Requirement: "Every Employee may have Dependents (e.g. children). A dependent is identified only by their first name, but only within the scope of that employee; a dependent cannot exist in the system without an employee." Model this.

  1. Spot the weakness signal: a Dependent has no key of its own — two different employees could each have a child named "Alex." So Dependent is a weak entity type (drawn as a double rectangle).
  2. Identify the owner: Dependent depends on Employee via an identifying relationship HAS_DEPENDENT (double diamond). Employee is the identifying / owner entity.
  3. Find the partial key: first_name alone isn't unique globally, but it is unique within one employee. It's the partial key (dashed underline). The full key of Dependent = owner's key + partial key = (SSN, first_name).
  4. Set participation: "cannot exist without an employee" ⇒ Dependent has total participation in HAS_DEPENDENT (double line to the diamond) — in fact every weak entity has total participation in its identifying relationship by definition. Employee's participation is partial (the word "may").
  5. Map to tables: Dependent(emp_ssn, first_name, ...) with composite primary key (emp_ssn, first_name), where emp_ssn is a foreign key to Employee(SSN). As a primary-key column emp_ssn is already NOT NULL; add ON DELETE CASCADE on that foreign key so deleting an employee removes their dependents, enforcing the existence dependency. Core pattern learned: no own key + existence-dependent ⇒ weak entity; its primary key is always owner key + partial key, and the identifying FK is mapped with ON DELETE CASCADE.

✨ Added by the guide — work these before the full problem set.

Lessons in this topic

🧠 Review & recall

Active recall is what moves a topic into long-term memory. Flip each card before revealing, then test yourself — your results are saved on this device.

Flashcard
In an ER diagram, what shape represents each of the four core building blocks: entity, attribute, relationship, and weak entity?
tap to reveal →
Entities (and entity sets) are rectangles, attributes are ellipses/ovals, relationships are diamonds, and a weak entity is a double rectangle. Lines connect entities to their attributes or to relationships.
💡 REctangle = REal thing, oval = attribute, Diamond = Does (relationship), Double box = weak.
Flashcard
Name the four attribute types in the ER model and how each is drawn.
tap to reveal →
Key attribute (uniquely identifies each entity, underlined, e.g. StudentID); composite attribute (splits into parts, e.g. Address into Country/State/City/Street); multivalued attribute (multiple values, double ellipse, e.g. phone numbers); derived attribute (computed from others, dashed/double ellipse, e.g. Age from Date of Birth).
💡 KCMD: Key, Composite, Multivalued, Derived.
Flashcard
What is the 'degree' of a relationship, and what are the three degrees with an example of each?
tap to reveal →
Degree is the number of entity types involved. Unary/recursive (degree 1, e.g. an Employee supervises another Employee), binary (degree 2, e.g. Student enrolls in Course — the most common), and ternary (degree 3, e.g. Employee works in a Department at a Location).
💡 Count the boxes touching the diamond: 1 = unary, 2 = binary, 3 = ternary.
Flashcard
Distinguish cardinality from participation in an ER model, including how participation is drawn.
tap to reveal →
Cardinality is the maximum number of instances of one entity that can be linked to one instance of another (1:1, 1:N, N:1, M:N). Participation says whether all (total, double line) or only some (partial, single line) instances of an entity must take part in the relationship.
💡 Cardinality = how MANY; participation = must ALL play? Double line = total, must play.
Flashcard
What makes an entity a 'weak entity', and how is it uniquely identified?
tap to reveal →
A weak entity cannot be uniquely identified by its own attributes and has no primary key of its own; it has existence dependency on a strong/owner entity. It is identified by combining the owner's primary key with its own partial key (discriminator). E.g. Course Section is identified by Course ID + Section Number.
💡 Weak = no key alone; full key = owner's key + partial key (discriminator).
Flashcard
What is a relationship attribute, and when should a property attach to the relationship rather than an entity?
tap to reveal →
A relationship attribute stores a property of the association itself, not of either entity. Attach it to the diamond when the value depends on the specific pairing — e.g. Enrollment Date and Grade on Student-enrolls-in-Course, or Position on Employee-works-in-Department.
💡 If it describes the link, not either box, it lives on the diamond.
Q1. An employee can supervise other employees, creating a supervisor-subordinate link within the same Employee entity. What kind of relationship is this?
Q2. A Department can offer many Courses, but each Course belongs to exactly one Department. What cardinality is the Department-Course relationship?
Q3. In the academic example, where should the attributes 'Enrollment Date' and 'Grade' be placed for the Student–Course 'enrolls in' relationship?
Q4. Course Section is modeled as a weak entity owned by Course, with Section Number as its partial key. What uniquely identifies a Course Section?
Q5. In the Employee Management System lesson, each Employee must be associated with a unique Payroll record and each payroll belongs to one employee. How is the 'receives' relationship classified and marked?