Find a course

Module 2: Modeling with ER DiagramsLesson 4 of 1214 min

Cardinality and ER Diagrams

Classify relationships as one-to-one, one-to-many or many-to-many, read crow's foot notation including optionality, and draw a diagram from requirements.

Step 1 of 10Learn

How many on each side?

For every relationship, a designer asks two questions, one from each side:

  • For one customer, how many orders can there be? Zero or many.
  • For one order, how many customers placed it? Exactly one.

The maximums give the relationship's cardinality:

  • One-to-many (1:N): one customer, many orders. The most common type by far.
  • Many-to-many (M:N): a book can have many authors, and an author can write many books.
  • One-to-one (1:1): each employee has at most one company laptop, and each laptop belongs to at most one employee.

The minimums give the participation (also called optionality or modality). If an order must have a customer, its participation is mandatory (minimum one). If a customer can exist without orders, as a new account does, participation on that side is optional (minimum zero).

These answers come from the business, not from the designer's guess. Can an order be split between two customers? Can a book exist with no author, like an anonymous classic? Asking "can there be zero?" and "can there be more than one?" in both directions is the quickest way to find requirements nobody thought to mention.

Step 2 of 10Practice · Sort into groups

Classify each relationship by its cardinality.

Tap an item, then tap its group (or drag it there).

Step 3 of 10Learn

Reading crow's foot notation

Several notations exist for ER diagrams. The most widely used in industry is crow's foot notation (also called IE notation). Entities are boxes, relationships are lines, and the line ends carry symbols:

  • A crow's foot (three prongs) means many.
  • A single bar means one.
  • A circle means zero (optional).

Each end combines a minimum (the symbol farther from the box) and a maximum (the symbol touching the box):

Symbol at the endMeaning
Two barsExactly one (one and only one)
Circle and barZero or one
Bar and crow's footOne or many
Circle and crow's footZero or many

You read a relationship from one entity, across the line, to the symbol at the far end. In the diagram, start at CUSTOMER, cross to the ORDER end, and read "a customer places zero or many orders." Then start at ORDER, cross to the CUSTOMER end, and read "an order is placed by exactly one customer."

The older Chen notation, often used in textbooks, draws relationships as diamonds and attributes as ovals. UML class diagrams write multiplicities as numbers like 0..* and 1. They all express the same ideas.

Crow's foot notation: a customer places zero or many orders, and each order is placed by exactly one customerCUSTOMERORDERplacesexactly onezero or many

Step 4 of 10Practice · Choose one

On a crow's foot diagram, the line between DEPARTMENT and EMPLOYEE has a circle and crow's foot at the EMPLOYEE end. What does that end tell you?

Step 5 of 10Learn

Optional or mandatory?

Participation decides whether NOT NULL appears on a foreign key later and whether certain rows can be inserted at all, so get it right in the diagram.

Consider an employee and a parking space. If every employee must have a space, then you can't hire anyone until a space is free, which is probably not what the business wants. If a space can exist without an employee, empty spaces are allowed. So the realistic model is "an employee has zero or one parking space, and a space is assigned to zero or one employee."

Some participation rules are surprisingly strict in real businesses. A bank may require every account to have at least one owner. A shop may require every order to contain at least one line. Relational databases enforce "exactly one" easily with a NOT NULL foreign key, but "at least one" on the many side usually needs application logic or a transaction that inserts the order and its first line together.

When unsure, ask about edge cases out loud: "What about a new customer who hasn't ordered yet? A book that's been announced but has no confirmed author? An order that was canceled?" Each answer pins down a circle or a bar.

Step 6 of 10Practice · Fill in the blanks

Fill in the blanks

In crow's foot notation, a crow's foot means , a single bar means , and a circle means .

Step 7 of 10Practice · Diagram

Crow’s foot ends: minimum × maximum

Each line end combines two symbols: the inner one is the minimum, the outer one the maximum.

A new customer may have no orders yet, or hundreds later. Which end belongs next to ORDER?

Tap the shape that answers the question, then press Check.

Crow’s foot ends: minimum × maximumTwo-by-two matrix: Exactly one: bar + bar, One or many: bar + crow’s foot, Zero or one: circle + bar, Zero or many: circle + crow’s foot.Exactly one: bar + barOne or many: bar + crow’sfootZero or one: circle + barZero or many: circle + crow’sfootMaximumOneManyMinimumZero (optional)One (mandatory)
Exactly one: bar + bar

Every row must link to exactly one: an order line belongs to one order.

One or many: bar + crow’s foot

At least one required, no upper limit.

Zero or one: circle + bar

Optional single link, such as an employee and a parking space.

Zero or many: circle + crow’s foot

The most common end: a customer may have no orders yet.

Step 8 of 10Learn

Resolving many-to-many relationships

Relational tables can't represent a many-to-many relationship directly. A book row can't hold a list of author ids, and an author row can't hold a list of book ids. The solution is to introduce an associative entity, which becomes a junction table (also called a bridge, link or join table).

Book–Author becomes three entities: BOOK, AUTHOR and BOOK_AUTHOR. Each BOOK_AUTHOR instance records one pairing: "author 7 wrote book 21." Now the many-to-many has become two one-to-many relationships: a book has many BOOK_AUTHOR rows, and an author has many BOOK_AUTHOR rows.

The junction often turns out to be a real business concept with attributes of its own. Order–Book becomes ORDER_LINE, carrying the quantity and the price paid at the time of the order. Student–Course becomes ENROLLMENT, carrying the grade and enrollment date. Author–Book can carry the author's position in the byline (first author, second author).

When you find an attribute that seems to belong to two entities at once, such as a grade that's neither just about the student nor just about the course, it's a strong hint that a many-to-many relationship is hiding there, and the attribute belongs on the junction.

A many-to-many relationship between BOOK and AUTHOR resolved by a junction table BOOK_AUTHOR that holds both foreign keysBOOKBOOK_AUTHORbook_id, author_idAUTHOR

Step 9 of 10Practice · What would you do?

What would you do?

A gym tracks members and classes. Each member can sign up for many classes, each class has many members, and the gym wants to record the date each member signed up for each class. Where should the sign-up date be stored?

Step 10 of 10Practice · Select all that apply

Which of these relationships need a junction table when implemented in a relational database? Select all that apply.

Select all that apply.