Mermaid ER diagram syntax: entities, attributes and cardinality with examples
Mermaid erDiagram syntax with rendered examples: entity blocks, typed attributes with keys, and crow's-foot cardinality from one-to-one to many-to-many.
An entity-relationship diagram is the schema picture: the things a database stores, the columns on each, and how rows point at each other. Mermaid's erDiagram draws it with crow's-foot cardinality markers, the convention data modellers already read.
Everything on this page is rendered by makesPDF's own Mermaid renderer, so a ```mermaid block produces exactly this in the PDF.
Entities and relationships
An entity is a bare name. A relationship is ENTITY_A <cardinality>--<cardinality> ENTITY_B : label, and it declares both entities if they do not exist yet:
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ LINE_ITEM : containsRead a marker from the entity it sits against: || is exactly one, o{ is zero or more, |{ is one or more, o| is zero or one. The line is solid for an identifying relationship and dotted — .. — for a non-identifying one.
Attributes
A { … } block after the entity lists its attributes, one per line as type name. Order is the type first: string name, int id:
erDiagram
USER {
int id PK
string name
string email
date created_at
}
ORDER {
int id PK
int user_id FK
string status
decimal total
}
USER ||--o{ ORDER : placesThe trailing keyword marks the attribute's key role — PK primary key, FK foreign key, UK unique — and is drawn beside the type.
Cardinality in practice
The four shapes cover the relationships that matter, and they combine in both directions:
erDiagram
AUTHOR ||--o{ BOOK : writes
BOOK ||--o{ REVIEW : receives
USER ||--o| AVATAR : has
TEAM }o--o{ USER : "member of"}o--o{ is the many-to-many form — the marker on each end is read the same way, just mirrored. A quoted label is required when it contains spaces: : "member of".
A fuller schema
Attribute blocks and relationships can be interleaved in any order, and an entity can be described in one block while its relationships are declared elsewhere:
erDiagram
INVOICE {
string id PK
string customer_id FK
date issued
decimal amount
}
PAYMENT {
string id PK
string invoice_id FK
string method
}
INVOICE ||--o{ PAYMENT : "settled by"
INVOICE }o--|| CUSTOMER : "billed to"Not supported in makesPDF yet: attribute comments — the quoted free text some Mermaid examples put after a key — are parsed but not drawn. Entity-level styling is not applied, so every entity uses the theme's fill.