Basic entity relationship diagram example
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ORDER_ITEM : contains
PRODUCT ||--o{ ORDER_ITEM : "ordered in"
CUSTOMER {
uuid id PK
string email UK
string name
datetime created_at
}
ORDER {
uuid id PK
uuid customer_id FK
string status
decimal total
}
ORDER_ITEM {
uuid order_id FK
uuid product_id FK
int quantity
}
PRODUCT {
uuid id PK
string sku UK
decimal price
}Entity relationship (ER) diagrams describe the tables in a database, their columns and how they relate. Mermaid uses crow's foot notation, the standard way data teams show one-to-one, one-to-many and many-to-many relationships.
Keeping your schema as a Mermaid erDiagram next to migrations makes reviews easier: the diagram changes in the same pull request as the SQL, and anyone can read it without a database GUI.
When to use a entity relationship diagram
- Designing a new database schema before writing migrations
- Documenting an existing data model for new team members
- Reviewing schema changes in pull requests
- Explaining data relationships to analysts and product managers
Entity Relationship Diagram syntax reference
| Syntax | What it does |
|---|---|
erDiagram | Start an ER diagram. |
A ||--o{ B : label | Relationship with cardinality on both ends and a label. |
|o / o| | Zero or one. |
|| | Exactly one. |
}o / o{ | Zero or more. |
}| / |{ | One or more. |
-- vs .. | Identifying (solid) vs non-identifying (dashed) relationship. |
ENTITY { type name PK } | Attributes with type, name and optional PK, FK or UK keys. |
type name "comment" | Attribute comment shown in the diagram. |
ENTITY["Display name"] | Entity alias with a friendlier label. |
Entity Relationship Diagram examples
Many-to-many with a join table
Students and courses are linked through an ENROLLMENT table.
erDiagram
STUDENT ||--o{ ENROLLMENT : has
COURSE ||--o{ ENROLLMENT : includes
STUDENT {
int id PK
string name
}
COURSE {
int id PK
string title
}
ENROLLMENT {
int student_id FK
int course_id FK
date enrolled_on
}Blog schema
Users write posts; posts have comments and tags.
erDiagram
USER ||--o{ POST : writes
POST ||--o{ COMMENT : has
USER ||--o{ COMMENT : writes
POST }o--o{ TAG : "tagged with"Tables, keys, and how many rows may exist are the questions. A mermaid er diagram answers them with crow's foot lines and a short attribute list, which is enough for a design doc or a README next to a migration. The syntax table shows the symbols. The part people get wrong is reading the foot, and putting the foreign key on the right entity.
How to read the foot
CUSTOMER ||--o{ ORDER is one customer and zero or more orders. The double bar is "exactly one". The circle-and-crow is "zero or more". The many side is the child, and that's where the foreign key lives. If you reverse the symbols, you have said an order has many customers, which is a different product.
The verb after the colon is the relationship name: : places. It's required. A line with the symbols and no colon fails to parse, so the preview never draws the rest of the schema.
Attributes sit in a block under the entity. string email PK marks the primary key. string customerId FK marks the foreign key. You don't have to list every column. List the keys, then the columns a reader needs to understand the link. A twenty-column dump is a table definition, and it makes the relationships too small to see.
Entity names in this style are single tokens, usually uppercase. The id is the name. There's no separate label, so rename carefully and keep the same token in the relationship line.
Worked example
A customer places orders. The customer row holds the email. The order row holds its own id and a customer id that points back.
erDiagram
CUSTOMER ||--o{ ORDER : places
CUSTOMER {
string email PK
string name
}
ORDER {
int id PK
string customerId FK
}The bar is on CUSTOMER, so each order belongs to one customer. The crow is on ORDER, so the customer may have no orders yet. That matches a newly registered account, which is why the circle (optional) is more honest than a mandatory many. customerId is the FK on ORDER. Putting it on CUSTOMER would say the person row stores one order id, and then the crow's foot would be lying.
email as a primary key is a modeling choice you might reject. The diagram's job is to make that choice visible. If you prefer a surrogate key, change the PK line and keep the relationship. The foot doesn't care which column is the key. It cares which entity is the one and which is the many.
Add a third entity only when it has its own row. A status enum is usually a column on ORDER, not an entity, unless you store statuses in a lookup table with attributes of their own.
When this is the wrong diagram
A class diagram is the better picture of application types, methods, and inheritance. An ER diagram has no methods, and it'll look silly if you force them in. A flowchart is the better picture of the checkout steps that create these rows. Use the ER diagram when someone asks "what is stored, and how many."
Common mistakes
- Dropping the colon, as in
CUSTOMER ||--o{ ORDER places. The parser stops there. The label form isCUSTOMER ||--o{ ORDER : places. - Drawing the crow on the parent.
||is the one side ando{is the many side. The foreign key belongs on the many side. - Marking
PKon a column that's only a foreign key, or marking both and forgetting which entity owns the row. One owner, one PK. - Listing every column until the relationship line is a footnote. Keep keys and the few attributes that explain the link.
Where to go next
The AI ER diagram generator will sketch entities from a description of the product. Fix the feet in the editor before you share it, and the cheat sheet lists the cardinality pairs. The ecommerce ER template extends this customer-and-order idea with products and payments.
Building the file from scratch is covered in the ERD diagram creator post. Subtypes, which this syntax will not draw as Chen circles, are in the EER diagram generator post. The four crow's foot marks are in Mermaid ER diagram cardinality.
Tips and best practices
- Use singular, uppercase entity names (ORDER, not orders) to make them stand out from attributes.
- Mark PK, FK and UK on attributes — reviewers mostly care about keys.
- Quote relationship labels that contain spaces: "ordered in".
- Model many-to-many relationships explicitly with a join entity when it carries data (dates, roles).
Generate a entity relationship diagram with AI
Not sure where to start? Describe what you need and the AI entity relationship diagram generator writes the Mermaid code for you. Try prompts like:
- “SaaS multi-tenant schema with organizations, users and roles”
- “Hotel booking database”
- “Inventory and warehouse management”
Entity Relationship Diagram templates
Entity Relationship Diagram FAQ
How do I show one-to-many in a Mermaid ER diagram?
Use ||--o{ for one-to-zero-or-many or ||--|{ for one-to-one-or-many. The left side describes how many of the first entity, the right side how many of the second.
Can I mark primary and foreign keys?
Yes. After the attribute name add PK, FK or UK (unique key). You can combine them, for example PK, FK.
Can I generate an ER diagram from SQL?
Paste your CREATE TABLE statements into the MermaidViewer AI generator and ask for an erDiagram; it converts tables, columns and foreign keys into Mermaid syntax you can then edit.