Skip to content
erdschema-designbeginners

How to Visualize Your MySQL Schema with an ERD Diagram

7 min readMasterSQL

An Entity-Relationship Diagram (ERD) visualizes your database schema: tables, columns, data types, and how they connect through foreign keys. Crow's foot notation is the standard way to draw these diagrams, using specific symbols to show cardinality and optionality of relationships.

Every database starts as a mental model. You picture tables in your head, imagine how they connect, and then write CREATE TABLE statements. The problem is that mental models break down fast. Once you have more than five tables, you forget which column references which. You lose track of the relationship between orders and customers. You write a JOIN that returns too many rows because you missed a one-to-many relationship.

An ERD fixes this. It turns your mental model into a picture you can reference, share, and validate. Before writing a single CREATE TABLE statement, sketch an ERD. This forces you to think about what tables you need, how they connect, and what each relationship means.

What is Crow's Foot Notation?

Crow's foot notation is the standard diagramming language for relational databases. Introduced by Gordon Everest in 1976, it is also known as Information Engineering (IE) notation. The name comes from the three-pronged fork shape that represents "many" relationships.

The notation uses three primitive symbols, always combined in pairs at each end of a relationship line:

  • Vertical bar (|) means "one" - maximum cardinality is one, not many
  • Circle (O) means "optional" - zero instances are allowed
  • Crow's foot (fork shape) means "many" - unlimited maximum cardinality

These symbols are always used in pairs. The symbol closest to the entity box is the maximum cardinality (one or many). The symbol closest to the line is the minimum cardinality (mandatory or optional). You read them from the entity outward.

Crow's Foot Symbol Reference

SymbolNameReads AsSQL Enforcement
||One mandatoryExactly one - one and only one instanceNOT NULL FK. Child must reference one parent.
O|One optionalZero or one - at most one instanceNullable FK. Child may or may not reference a parent.
|<Many mandatoryOne or many - at least one instanceNOT NULL FK on child. See note below.
O<Many optionalZero or many - zero or more instancesNullable FK. Parent can exist with zero children.

A note on many mandatory. The |< symbol means "one or many" and describes two constraints: the child must have a parent (enforced by NOT NULL FK), and the parent must have at least one child. SQL foreign keys only enforce the first half. There is no declarative way to prevent a parent from existing with zero children. Enforcing that requires triggers or application logic. MySQL does not support deferred constraints for this purpose. The ERD shows the full business rule; the SQL implementation captures part of it.

In a standard one-to-many relationship, the parent table (the "one" side) gets the double bar symbol, and the child table (the "many" side) gets the crow's foot. The child table is always the one that holds the foreign key. The optionality (bar vs circle) depends on whether the foreign key column allows NULL values.

Reading a Real ERD

Here is a concrete example. Suppose you are building a school management system with three tables: classes, students, and enrollments.

Three tables, three relationships. Here is how to read each one:

Relationship 1: classes to students

A line connects the classes table to the students table. Near classes, you see two vertical bars (||) - this means each student belongs to exactly one class. Near students, you see the crow's foot symbol - this means one class has many students. The foreign key students.class_id references classes.id. Read it as: "One class has many students."

Relationship 2: students to enrollments

A line connects students to enrollments. Near students, two vertical bars (||) - each enrollment belongs to exactly one student. Near enrollments, the crow's foot - one student has many enrollments. The foreign key enrollments.student_id references students.id. Read it as: "One student has many enrollments."

Relationship 3: classes to enrollments

A line connects classes to enrollments. Near classes, two vertical bars (||) - each enrollment belongs to exactly one class. Near enrollments, the crow's foot - one class has many enrollments. The foreign key enrollments.class_id references classes.id.

Enrollments is a junction table. It resolves the many-to-many relationship between students and classes. A student can enroll in many classes, and a class can have many students. The enrollments table holds foreign keys to both, making the many-to-many relationship queryable with standard JOINs.

CREATE TABLE Statements

Here is the SQL that produces the schema shown in the ERD:

CREATE TABLE classes (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  teacher VARCHAR(100) NOT NULL
);

CREATE TABLE students (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  class_id INT NOT NULL,
  FOREIGN KEY (class_id) REFERENCES classes(id)
);

CREATE TABLE enrollments (
  id INT PRIMARY KEY AUTO_INCREMENT,
  student_id INT NOT NULL,
  class_id INT NOT NULL,
  grade VARCHAR(2),
  FOREIGN KEY (student_id) REFERENCES students(id),
  FOREIGN KEY (class_id) REFERENCES classes(id)
);

The FOREIGN KEY constraints are what the ERD visualizes. Each FOREIGN KEY in the SQL becomes a relationship line in the diagram. Without FK constraints, most ERD tools cannot show connections between tables because there is no metadata to derive relationships from.

Bad vs Good Schema Design

Here is a common mistake. Suppose you want to store a student's classes. The naive approach puts a comma-separated list in a single column:

-- Bad: storing multiple values in one column
CREATE TABLE students_bad (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  classes VARCHAR(255)  -- "Math,Science,English"
);

An ERD immediately exposes this problem. There is no relationship line between students and classes because there is no foreign key. The classes column is just a string. You cannot JOIN on it, enforce referential integrity, or query it efficiently. The correct approach uses a junction table (enrollments) with proper foreign keys, as shown in the ERD above.

Another design mistake: making the foreign key nullable when the relationship is mandatory. If every student must belong to a class, class_id should be NOT NULL. The ERD would show a circle (optional) near students instead of a bar (mandatory), signaling that the constraint is weaker than intended. This visual feedback catches design issues before you write a single query.

Primary Keys, Foreign Keys, and the Diagram

Every ERD shows two types of keys:

  • Primary Key (PK) - a unique identifier for each row. Usually the id column. Shown with a "PK" badge in the diagram. BIGINT is the recommended default for new tables; signed INT maxes out at about 2.1 billion values, which runs out faster than expected with audit logs and event tracking.
  • Foreign Key (FK) - a column that references another table's primary key. This is how tables connect. Shown with an "FK" badge in the diagram. In MySQL 8.4, foreign keys require a unique key or primary key on the referenced parent column - a stricter enforcement of the SQL standard compared to MySQL 8.0, which allowed non-unique indexes.

When you see a foreign key in one table pointing to a primary key in another, that is a relationship. The ERD draws a line between them with Crow's foot symbols showing the cardinality.

Common ERD Patterns

One-to-Many

The most common pattern. One parent table has many child rows. Example: one customer has many orders. The foreign key goes on the "many" side (orders.customer_id). The ERD shows double bars near the parent and a crow's foot near the child.

Many-to-Many

Requires a junction table. Example: students and classes. A student can enroll in many classes, and a class can have many students. The junction table (enrollments) holds foreign keys to both. The ERD shows two one-to-many relationships meeting at the junction table.

One-to-One

Rare in practice. Example: a user profile linked to a user account. The foreign key has a UNIQUE constraint, which changes the cardinality from many to one. The ERD shows double bars on both ends instead of a crow's foot on one side.

Using an Interactive ERD

Modern SQL playgrounds include built-in ERD diagrams. As you create tables and define foreign keys, the diagram updates automatically. This gives you real-time feedback on your schema design.

The interactive ERD in MasterSQL uses Crow's foot notation. When you run CREATE TABLE with FOREIGN KEY constraints, the diagram shows:

  • Table nodes with column names and data types
  • Primary key badges (PK) on id columns
  • Foreign key badges (FK) on reference columns
  • Relationship lines with Crow's foot symbols
  • Optional markers (circle) for nullable foreign keys

This feedback catches design issues immediately. If you forgot a foreign key, the relationship line does not appear. Circular foreign key references show as visible cycles in the diagram.

Practical Tips

  • Start with entities. List the main things your application tracks (users, orders, products). Each becomes a table.
  • Add relationships. How do these entities connect? Each connection becomes a foreign key.
  • Check cardinality. For each relationship, ask: "Can one X have many Y?" If yes, put the foreign key on the Y side.
  • Validate with queries. After designing, write a few JOINs. If they return unexpected results, your ERD might be wrong.
  • Keep it updated. When you ALTER TABLE, check the ERD. The diagram should match your actual schema.

ERDs in Production

ERDs are not just for learning. In production, they document the schema for the team. New members can understand the structure by looking at the diagram instead of reading hundreds of CREATE TABLE statements. Database reviews start with the ERD to catch design issues before deployment.

Tools like MySQL Workbench, dbdiagram.io, and built-in playground ERDs all use Crow's foot notation. Learning this standard means you can read any database diagram you encounter. In MySQL 8.4, foreign keys now require a unique key or primary key on the parent table's referenced columns, which means your ERD should show that parent-side columns serving as FK targets have a unique key or primary key constraint.

Key Takeaways

  • Crow's foot notation uses three symbols (bar, circle, fork) combined in pairs at each end of a relationship line. The symbol closest to the entity is the maximum cardinality; the symbol closest to the line is the minimum cardinality.
  • The crow's foot (fork) always appears on the child table - the table that holds the foreign key. The double bar appears on the parent table being referenced.
  • Many-to-many relationships require a junction table with foreign keys to both parent tables. The ERD shows two one-to-many relationships meeting at the junction.
  • SQL foreign keys cannot enforce minimum cardinality on the parent side. A "one or many" relationship means the child must have a parent (NOT NULL FK), but the parent can still exist with zero children. Enforcing that requires triggers or application logic.
  • In MySQL 8.4, foreign keys require a unique key or primary key on the parent columns they reference. Your ERD should reflect this requirement.

FAQ

What is the difference between ERD and schema diagram?

An ERD is a conceptual or logical model that shows entities and relationships. A schema diagram is a physical representation of the actual database tables, columns, and constraints. In practice, most database tools use the terms interchangeably, and the diagrams look nearly identical. The main difference is that an ERD may omit implementation details like data types and indexes, while a schema diagram includes them.

How do I know if a foreign key is mandatory or optional?

Check whether the foreign key column allows NULL values. If the column is NOT NULL, the relationship is mandatory - every row in the child table must reference a parent. If the column allows NULL, the relationship is optional - a child row can exist without a parent. In the ERD, NOT NULL shows as a vertical bar, and NULL shows as a circle.

Can I create an ERD from existing MySQL tables?

Yes. MySQL Workbench can reverse-engineer an existing database and generate an EER diagram automatically. It reads foreign key constraints and displays them as Crow's foot relationships. Tools like dbdiagram.io also accept CREATE TABLE statements and render diagrams from them. MasterSQL's playground ERD updates in real time as you run SQL.

What does it mean when there is no relationship line between two tables?

It means there is no foreign key constraint connecting them. The tables may still be related through queries (using WHERE clauses instead of JOINs), but the database does not enforce the relationship. Without a foreign key, the ERD cannot show a relationship line, and referential integrity is not guaranteed.

Why are some relationships drawn with dashed lines?

In some ERD notations, a dashed line indicates a non-identifying relationship, where the child table has its own independent primary key and the foreign key is just a regular column. A solid line indicates an identifying relationship, where the child's primary key includes the parent's foreign key (composite PK). MySQL Workbench uses this convention in its EER diagrams.

Should I always use BIGINT for primary keys?

BIGINT is the recommended default for new tables. Signed INT maxes out at about 2.1 billion values, which runs out faster than expected with audit logs, session tokens, and event tracking tables. BIGINT uses 8 bytes per value instead of 4 bytes for INT, which is negligible for most applications. Use INT only when the table will never exceed a few million rows.

M

Written by

MasterSQL

Related Tutorials