CREATE TABLE, you need to know which tables there are and how they connect. Get it wrong, and you find out three months later when a book with two authors won’t fit.
An entity relationship diagram (ERD) is a drawing of that answer. Boxes are the things you store, lines are how they relate, and small symbols at the ends of each line say how many. It is a map of the data’s structure. It is not a flowchart: a flowchart shows steps happening over time, and an ERD shows what exists and how it is connected.
This post teaches you to draw one yourself. You will learn to read the symbols, follow a five-step recipe on a small library, fix the four most common mistakes, and then try one on your own. It helps to know what a table, a primary key and a foreign key are. If not, read The Relational Model first.
The four parts
Take this sentence: “Each member places zero or more loans. Each loan belongs to exactly one member.” Every ERD is built from four kinds of part, and this sentence has all of them.- Entity: a kind of thing you store many of. The nouns member and loan become boxes, named in the singular:
MemberandLoan. - Attribute: a fact about one entity, such as a member’s name. It is written as a line of text inside the box. (This sentence has none yet.)
- Relationship: how two entities are linked. The verb places becomes a line between the boxes, labelled with the verb.
- Cardinality: how many on each side. Zero or more and exactly one become symbols at the two ends of the line.
Member table later on. Name boxes in the singular, because each box describes one row.
Reading crow’s-foot notation
There are several ways to draw cardinality. This post uses crow’s foot, the style most developer tools use: dbdiagram.io, Mermaid, DBeaver, MySQL Workbench and Lucidchart all draw it. Each end of a line carries two symbols:- Inner symbol, next to the box: the maximum. A bar
|means one. A crow’s foot<(three prongs) means many. - Outer symbol, further along the line: the minimum. A bar
|means one, so the link is required. A circleomeans zero, so it is optional.
1 (exactly one), 0..1 (zero or one), 1..* (one or more), 0..* (zero or more). The older Chen notation, from Peter Chen’s 1976 paper that introduced ER modelling, draws relationships as diamonds and attributes as ovals. It is good for teaching concepts, but it takes a lot of space, so developer tools rarely use it. The ideas are the same in all three styles. Only the symbols change.
The recipe: from a brief to a diagram
Here is a short brief for a library system. You will rarely get a cleaner one. Follow five passes over it. Each pass looks for one kind of word and adds one kind of part to the drawing. A few rules of thumb for each step:- Nouns. Mark them all, even the obvious ones. It is faster to cross a noun off than to notice one you missed.
- Entities. Keep a noun if there are many of it and it has facts of its own. Drop the system itself (“the library”, “the app”). Watch for verbs that carry facts: “borrows” has dates, so it becomes the
Loanentity. Orders, bookings, payments and enrolments are often found this way. - Attributes. A noun that only describes something else is an attribute. Give every entity a primary key that never changes. A plain ID such as
memberIDis the safe default. - Relationships. One line per pair of entities that the brief links directly. Label it with the verb, so the line reads as a sentence: Member makes Loan.
- Cardinality. For each line, ask the question twice: “One member has how many loans?” and “One loan has how many members?” Draw each answer next to the box being counted. If the brief doesn’t say, ask whoever wrote it. That question is often the most useful thing the diagram does.
Many-to-many needs a bridge
Look at the Book–Author line. It has a crow’s foot on both ends: a book has one or more authors, and an author writes one or more books. Lines like this can’t be stored as drawn. Try with real data: Good Omens was written by Terry Pratchett and Neil Gaiman. ABook row has one authorID cell, so a second author needs a second row, and that row repeats the book’s primary key. The fix is a third table, often called a bridge, junction or associative table. It has one row per book–author pair, and both of its columns are foreign keys.
In the diagram, the bridge turns one many-to-many line into two one-to-many lines, with the crow’s feet pointing at the bridge. The bridge can also hold facts about the pair itself. For example, BookAuthor.position would store whether Gaiman is listed first or second.
You have already met one of these. Loan sits between Member and Book, and over time a member borrows many books and a book is borrowed by many members. Loan is a bridge with its own facts (the dates), which is why it showed up as an entity in step 2.
Where the foreign key goes
Once every line is one-to-many or one-to-one, the diagram tells you exactly where each foreign key goes. The minimum symbols tell you whether it can be empty. The rule in one line: the foreign key lives where the crow’s foot is. Each loan has one member, so the loan can store it. A member has many loans, so the member can’t. The minimums map to SQL too, but only partly:
That last row surprises people. A foreign key can say “this loan points at a real member”. It can’t say “this book must have at least one row in another table”. Draw the rule anyway, because the diagram is where you agree on it. Then decide who enforces it.
Four common mistakes
These four show up in nearly every first ERD. Each card shows the mistake, and one click shows the fix. The fourth mistake is the most common one. It happens when you draw the symbol next to the box you start from, instead of the box you are counting. When in doubt, read the line out loud from both ends.Your turn
Here is a new brief. Answer the four questions, then check your answers. If you get stuck, scroll back to the recipe.From diagram to code
Most teams don’t draw ERDs by hand any more. They write them as text, so the diagram lives next to the code and shows up in code review. Here is the finished library, with the bridge table and every foreign key, in three common formats. Mermaid. GitHub, GitLab, Notion and this site render it straight from Markdown: DBML. This is the format of dbdiagram.io, which draws the diagram and exports SQL:How much detail?
The same model can be drawn at three levels of detail. Pick the level that suits the reader.- Conceptual: entity names and lines, maybe cardinality. Use it to agree on scope with people who don’t write code. In this post: recipe steps 2 and 4.
- Logical: adds attributes, primary keys, cardinality and bridge tables. Use it to agree on the design. In this post: recipe step 5, plus the bridge.
- Physical: adds column types,
NOT NULL, foreign keys and indexes. Use it to build. In this post: the SQL above.
When an ERD is the wrong tool
- Process questions. “What happens after a book is returned?” is a flow, so draw a flowchart or sequence diagram. An ERD has no time and no order.
- Data with no fixed shape. Free-form documents, logs or user-defined fields don’t fit fixed boxes well. A rough sketch still helps, but don’t force it.
- Rules beyond cardinality. “A member can have at most five loans at once” is a real rule, but crow’s foot can’t draw it. Write it as a note next to the line.
Check yourself
Glossary
For AI agents
Adapted from Lucidchart, “What is an Entity Relationship Diagram (ERD)?”. Changes: an ERD is described here as a structure diagram, not a type of flowchart; product-specific steps and the history before Chen’s 1976 paper are left out; the rule that “one or more” can’t be enforced by a plain foreign key is added. The library and clinic data are illustrative; figures are simplified teaching models.