Mermaid ER Diagram Syntax
An ER diagram opens with erDiagram, then lists relationships between entities and, optionally, each entity's attribute block.
| Construct | Meaning | Example |
|---|---|---|
A ||--o{ B : verb | One-to-many: exactly one A relates to zero or more B. | erDiagram
CUSTOMER ||--o{ ORDER : places |
A ||--|{ B : verb | One-to-many where B requires at least one: exactly one A relates to one or more B. | erDiagram
ORDER ||--|{ ORDER_ITEM : contains |
A }o--o{ B : verb | Many-to-many: zero or more A relate to zero or more B. | erDiagram
POST }o--o{ TAG : "tagged with" |
ENTITY { type name } | An attribute block listing an entity's columns and their types. | erDiagram
CUSTOMER {
int id
string name
} |
int id PK | Appending PK after an attribute marks it as the primary key. | erDiagram
CUSTOMER {
int id PK
} |
int customer_id FK | Appending FK after an attribute marks it as a foreign key referencing another entity. | erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER {
int customer_id FK
} |
Everything together
Mermaid source
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ORDER_ITEM : contains
CUSTOMER {
int id PK
string name
}
ORDER {
int id PK
int customer_id FK
date placed_at
}
ORDER_ITEM {
int order_id FK
int quantity
}