Example ER diagram code
This is the Mermaid source loaded in the editor above. Copy it into a Markdown file inside a ```mermaid block and GitHub, GitLab, Notion or Obsidian will render it.
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ORDER_ITEM : contains
PRODUCT ||--o{ ORDER_ITEM : "appears in"
CUSTOMER {
int id PK
string email UK
string name
}
ORDER {
int id PK
int customer_id FK
date created_at
string status
}
ORDER_ITEM {
int order_id FK
int product_id FK
int quantity
}
PRODUCT {
int id PK
string sku UK
decimal price
}ER diagram syntax cheat sheet
| Syntax | What it does |
|---|---|
| erDiagram | Start an entity-relationship diagram. |
| A ||--o{ B : label | Relationship with crow's foot ends and a label (quote labels with spaces). |
| || / |o | Exactly one / zero or one (left side; mirrored as || and o| on the right). |
| }| / }o | One or more / zero or more (left side; |{ and o{ on the right). |
| -- vs .. | Solid line = identifying relationship, dotted = non-identifying. |
| ENTITY { type name PK } | Attributes with type and name. Keys: PK, FK, UK. |
How to make a Mermaid ER diagram
- 1Start the code with erDiagram.
- 2Add relationships such as CUSTOMER ||--o{ ORDER : places.
- 3List each entity's attributes with type and name in braces.
- 4Mark keys with PK, FK or UK.
- 5Export as SVG or PNG, or copy a share link.
Crow's foot cardinality
Each side of a relationship has two characters: |o means zero or one, || exactly one, }o zero or more and }| one or more. They are mirrored on the right side, so CUSTOMER ||--o{ ORDER reads "one customer has zero or more orders".
A relationship needs a label after the colon. Use a verb such as places or contains so the diagram reads as a sentence.
Attributes and keys
Add columns in braces after the entity name, one per line as type then name, such as int id PK and string email UK.
PK marks a primary key, FK a foreign key and UK a unique key. You can add a quoted comment after the key to note constraints.
Identifying vs non-identifying
Two dashes (--) draw a solid line for an identifying relationship, where the child cannot exist without the parent. Two dots (..) draw a dashed line for a non-identifying one.
Model one bounded area at a time; a diagram of 10 to 15 entities stays readable.