ER diagrams: attributes and keys
简介 / Introduction
The real payload of an ER diagram lives inside the entities: the attributes, and the primary / foreign / unique key constraints among them. Those constraints decide how the tables are built and how the joins land. PlantUML declares entities with entity blocks and tags keys with stereotypes like <<PK>>; Mermaid uses PK / FK / UK keyword suffixes. This post walks the same customer-order-order-item model through both engines and explains the differences around composite keys and attribute comments.
PlantUML 版
PlantUML has no native erDiagram; entities are declared with entity, and attributes are written as +name : type <<stereotype>>. A standalone -- line separates the key area from the ordinary attributes. hide circle removes the small circle in front of each entity:
1 | @startuml |
Key points:
+is a visibility prefix:+means public,-means private.<<PK>>,<<FK>>and<<UK>>are stereotypes and are rendered as labels. Several fit on one line —<<PK, FK>>means the attribute is both a primary and a foreign key.- A lone
--draws a separator line, letting you split “keys” from “ordinary attributes”. This layout trick is unique to PlantUML. as Cgives the entity an alias, so the relationship line readsC ||--o{ Oinstead of the full name.
Mermaid 版
Mermaid has a native erDiagram. Inside an attribute block the type comes first, then the attribute name, then the key keyword. Comments go in double quotes:
1 | erDiagram |
Key points:
- The attribute blocks must come after the relationship lines (relationships first, then entity blocks under
erDiagram), otherwise the parser may reject the diagram. PK, FKis separated by a comma and a space — two keys on one line mean a composite primary key.- Type and attribute name are separated by a space, not a colon. The type is rendered as its own small label.
- Text in double quotes is an attribute comment:
name "display name"says the column displays a nickname rather than the raw column name.
对比 / Side-by-side
| Feature | PlantUML | Mermaid |
|---|---|---|
| Key marking | stereotype <<PK>> / <<FK>> / <<UK>> |
suffix keyword PK / FK / UK |
| Attribute format | +name : type <<PK>> |
type name PK |
| Composite key | several stereotypes on one line: <<PK, FK>> |
comma-separated: order_id PK, FK |
| Attribute comments | requires a note or skinparam | inline "comment" |
| Separating keys from plain columns | -- separator line |
not supported |
PlantUML reads more like a UML class diagram, with key information modelled as stereotypes you can extend freely. Mermaid reads more like a SQL DDL fragment, with types and constraints laid out on a single line — close to the table you would actually create.
小结 / Wrap-up
Attributes and keys are the easiest part of an ER diagram to get wrong. The two classic mistakes are marking only one key of a composite key, and leaving a foreign key with a type that does not match the primary key it references. With PlantUML, use the -- separator together with stereotypes so keys are visually distinct from ordinary columns. With Mermaid, treat the attribute block as a DDL snippet and add a comment to every key. Both engines support the PK / FK / UK constraints, so decide on the constraint semantics first and the syntax will follow.
- Title: ER diagrams: attributes and keys
- Author: puml.online
- Created at : 2026-11-12 09:00:00
- Updated at : 2026-11-26 01:07:01
- Link: https://puml.online/blog/er-diagram-attributes-keys-en/
- License: This work is licensed under CC BY-NC-SA 4.0.