MyMisPal
Phase 3 · Database design

ERD Studio

Draw your own entity-relationship diagram: drag tables, connect them with crow's foot relationships, and the studio adds the foreign keys and writes MySQL-ready SQL for you. Then run it in the SQL playground with one click.

employs · 1:N departmentsPKdept_idINTnameVARCHAR(100) employeesPKemp_idINTfull_nameVARCHAR(100)FKdept_idINT
How to read a relationship Read it both ways: each department has zero or many employees, and each employee belongs to exactly one department. The foreign key dept_id always sits on the many side.

Building blocks

Entity

A thing we store data about: a student, a product, an order. It becomes a table.

Attribute

A fact about the entity: name, price, date. It becomes a column.

PK

An attribute whose value never repeats and identifies each row, such as student_id.

FK

A column pointing to another table's primary key; it is how a relationship is stored.

Relationship

The link between two entities, with its cardinality: one-to-one, one-to-many, many-to-many.

Crow's foot notation

Each end of a relationship carries two symbols: the one at the entity says “at most how many”, the one further out says “at least how many”. The symbol next to an entity counts that entity's rows.

Exactly one

There must be one, and never more than one.

Zero or one

Optional: there may be none, and at most one.

One or many

At least one, possibly many.

Zero or many

Optional, and possibly many.

From a system description to a diagram: 5 steps

  1. 1
    Pick out the nouns

    Read the system description and underline the important nouns (student, course, instructor). Most become entities.

  2. 2
    List the attributes

    For each entity, list the facts you need. Keep each attribute atomic: one value per cell.

  3. 3
    Choose the primary key

    Every entity needs a primary key. A numeric id such as student_id is usually simplest.

  4. 4
    Draw relationships and cardinality

    Verbs point to relationships (enrols, teaches). Ask both ways: how many students per course, how many courses per student?

  5. 5
    Resolve many-to-many and normalise

    Turn every many-to-many into a bridge table, and make sure no fact is stored in more than one place.

Common mistakes

  • An entity with no primary key.
  • Storing several values in one cell, like “phones: 050, 055”.
  • Putting the foreign key on the “one” side instead of the “many” side.
  • Leaving a many-to-many without a bridge table when you implement it.
  • Storing a derived value such as “age” instead of the birth date.

Auto-checked challenges

Read the story, draw the diagram in the studio and press Check. The studio reviews entities, keys and relationships and tells you what is missing.

Entry

Clinic appointments

A clinic has doctors and patients. Each appointment is for one patient with one doctor. A patient can book many appointments and a doctor has many appointments. Store each appointment's date and time in a column named appointment_time.

Intermediate

Course enrolment and grades

A student enrols in many courses, and a course has many students. We also need each student's grade in every course they took. Think: where does the grade column belong? (Hint: the grade describes the enrolment itself, not the student or the course.)

Advanced

Departments and their managers

Every employee belongs to one department, and a department has many employees. On top of that, each department is managed by exactly one employee, and an employee manages at most one department. So there are two different relationships between the same two tables!

Templates

Complete examples to open in the studio, edit, and see their SQL.

University registration

Departments, instructors, students, courses and an enrolment bridge holding the grade.

Online store

Customers, orders, products and categories, plus a one-to-one between order and payment.

Hospital

Departments, doctors, patients, appointments and prescriptions.

Library

Books and authors in a many-to-many relationship, and members who borrow books.

The SQL lessons company

The same database from the SQL lessons, including the manager self-relationship.

Spotted a mistake on this page, or have an idea to improve the site?