What an ER diagram shows
An ER diagram has three ingredients:
- Entities: the things you store, such as Customer, Order or Product. Each becomes a table.
- Attributes: the facts about each thing, such as a name or a price. Each becomes a column.
- Relationships: how things connect, such as "a customer places orders". These become foreign keys.
Entity and attribute symbols
| Symbol | Means |
|---|---|
| A box with a title | An entity (a table) |
| A line inside the box | An attribute (a column) |
| PK | Primary key: the column that identifies each row |
| FK | Foreign key: a column that points to another table |
| UK | Unique: no two rows share this value |
Crow's foot cardinality symbols
The ends of each relationship line say how many of one thing can link to the other. Read the symbol nearest the box you are reading toward.
| Symbol at the line end | Means |
|---|---|
| Two short bars | Exactly one |
| A circle and a bar | Zero or one |
| A crow's foot and a bar | One or many |
| A crow's foot and a circle | Zero or many |
In diagram text the same four ends are written as || (exactly one), |o (zero or one), }| (one or many) and }o (zero or many).
The circle means "optional" (zero is allowed). The bar means "at least one". The crow's foot means "many".
Example: a small shop
How to read it:
- A customer places zero or many orders, and each order belongs to exactly one customer.
- Each order contains one or many order lines.
- Each order line refers to exactly one product, and a product can appear in zero or many order lines. ORDER_LINE is the table that joins orders and products, which is how databases store a many to many relationship.
Example: a school
A teacher teaches zero or many courses. A student enrols in zero or many courses, and a course has zero or many students. When you build the real database, that many to many line becomes a joining table such as ENROLMENT.
How to design an ER diagram in five steps
- List the nouns in the problem: customers, orders, products. Those are your entities.
- Give each one a primary key and the attributes you need.
- Draw the relationships and name them with a verb: places, contains, teaches.
- Set the cardinality at both ends: ask "can there be none? can there be many?" in each direction.
- Replace many to many lines with a joining table that has a foreign key to each side.
Writing an ER diagram as text
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER {
int id PK
int customer_id FK
}
||--o{ reads from left to right: exactly one CUSTOMER to zero or many ORDERs. Use -- for a solid line (the child depends on the parent) and .. for a dashed one.
Drawing ER diagrams for free
In Chalkline, open Diagram as text → Database, type the entities and relationships, and press Apply. Crow's foot ends and keys are drawn for you, in a hand drawn or a clean style. If you already have SQL, see SQL to ER diagram, which builds the diagram from your CREATE TABLE statements and turns your edits back into SQL. For object models rather than tables, see UML class diagrams.