Class diagrams: multiplicity
简介 / Introduction
The little numbers you write at the end of an association line in a class diagram — 1, 0..1, * — are the multiplicity. They answer a very practical question: standing on side A, how many instances can hang off side B? Customer "1" --> "0..*" Order means “one customer may place zero to many orders, and each order belongs to exactly one customer”.
Multiplicity is the easiest thing to leave out. Skip it and the model still looks plausible, but the moment it reaches database foreign keys, API contracts, or generated code the constraint is gone — because “no number” is usually read as “one-to-one”. In this tutorial we model the same order domain (Customer / Order / OrderItem / Payment) twice, once in PlantUML and once in Mermaid, then compare the places where the two engines are easy to confuse.
PlantUML 版
1 | @startuml |
Key points:
- The multiplicity written on the left of the relation name describes the class next to it. Labels go after the colon:
relationName : label. "1"means exactly one,"0..1"means zero or one (a nullable foreign key),"0..*"means zero to many,"1..*"means one to many.*--is composition: order items live and die with their order and cannot exist on their own.- For finer control you can use interval numbers such as
"2..5", or the attribute formcustomer : Customer[1].
Mermaid 版
1 | classDiagram |
Mermaid’s classDiagram also accepts multiplicity labels, and the syntax is basically the same: A "n" <relation> "m" B : label. A few notes:
- Mermaid declares members as
+Type name, the reverse of PlantUML’s+name : Type. - Composition is
*--, aggregation iso--, inheritance is<|--, realization is<|... - Multiplicity labels in Mermaid must be quoted —
Customer 1 --> 0..* Orderis a parse error. - Direction semantics match PlantUML: a label describes the count seen from the class it sits next to.
对比 / Side-by-side
| Aspect | PlantUML | Mermaid |
|---|---|---|
| Multiplicity syntax | Customer "1" --> "0..*" Order : places |
Customer "1" --> "0..*" Order : places (nearly identical) |
| Label position | After the colon | After the colon |
| Composition / aggregation | *-- / o-- |
*-- / o-- |
| Member declaration order | +name : String |
+String name |
| Attribute-style multiplicity | customer : Customer[1] |
Not supported |
| Arbitrary interval numbers | 2..5 |
Supported |
| Native class diagram | Yes | Yes (classDiagram, v11) |
小结 / Wrap-up
When writing a multiplicity, always ask yourself: standing here, how many are there? Set the lower and upper bound for the main relation first, then sweep the model for every nullable foreign key and make sure it is marked 0..1 rather than 1. The two engines agree almost perfectly on syntax; the real differences are that PlantUML additionally supports attribute-style [1] annotations and finer interval ranges, which is handy in design documents, while Mermaid’s 0..* / 1..* range notation already covers the vast majority of business models. Once the model is done, make sure the critical multiplicities are mirrored as NOT NULL constraints in the database — the difference between 0..1 and 1 in the diagram ends up living or dying on that single column.
- Title: Class diagrams: multiplicity
- Author: puml.online
- Created at : 2026-11-06 09:00:00
- Updated at : 2026-11-26 01:07:01
- Link: https://puml.online/blog/class-diagram-multiplicity-en/
- License: This work is licensed under CC BY-NC-SA 4.0.