Mermaid ER Diagram Maker
Write ER diagrams in Mermaid code and watch them render as you type. Start from the example below, then export PNG or SVG, share a link or embed the diagram.
Updated · Code editor with live preview
ER diagram example
Rendering preview…
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : contains
PRODUCT ||--o{ LINE_ITEM : "appears in"
CUSTOMER {
string id PK
string email
}
ORDER {
string id PK
string customer_id FK
date created_at
}What is an ER diagram?
An entity relationship (ER) diagram shows the entities in a data model, usually database tables, along with their attributes and the relationships between them, such as one-to-many. In Mermaid an ER diagram starts with the erDiagram keyword.
When to use an ER diagram
- Designing a database schema
- Documenting tables, primary keys and foreign keys
- Reviewing data model changes in pull requests
- Explaining how records relate to non-technical teammates
How to make an ER diagram in Mermaid
- Step 1
Open the editor
Open TryMermaid with the example on this page, or start a new diagram with erDiagram on the first line.
- Step 2
Add relationships
Write one relationship per line, such as CUSTOMER ||--o{ ORDER : places. The symbols on each side set the cardinality, and the text after the colon labels the relationship.
- Step 3
Add attributes and keys
Add a block after an entity name with one attribute per line, written as type name and optionally followed by PK, FK or UK.
- Step 4
Export or share
Check the live preview, then export the diagram as PNG or SVG or copy a share link.
ER diagram syntax cheat sheet
| Element | Syntax | What it does |
|---|---|---|
| Exactly one | || | One and only one |
| Zero or one | |o or o| | Optional single |
| One or more | }| or |{ | At least one |
| Zero or more | }o or o{ | Any number |
| Identifying | A ||--o{ B : label | Solid line |
| Non-identifying | A ||..o{ B : label | Dashed line |
| Attribute | string email UK | Type, name and optional key |
ER diagram FAQ
How do I show a one-to-many relationship in Mermaid?
Put ||--o{ between the two entities, for example CUSTOMER ||--o{ ORDER : places. The || side means exactly one customer and the o{ side means zero or more orders. Use ||--|{ if every customer must have at least one order.
Can I mark primary and foreign keys?
Yes. Inside an entity block, add PK, FK or UK after the attribute name, for example string customer_id FK. You can combine keys with a comma, such as PK, FK, and add a quoted comment at the end of the line.