ER diagrams: attributes and keys

ER diagrams: attributes and keys

puml.online

简介 / 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@startuml
hide circle
entity "Customer" as C {
+customer_id : int <<PK>>
--
+email : varchar <<UK>>
+name : varchar
+loyalty_points : int
}
entity "Order" as O {
+order_id : int <<PK>>
--
+customer_id : int <<FK>>
+placed_at : datetime
+total : decimal
}
entity "OrderItem" as OI {
+order_id : int <<PK, FK>>
+product_id : int <<PK, FK>>
--
+quantity : int
+unit_price : decimal
}
C ||--o{ O : places
O ||--|{ OI : contains
@enduml

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 C gives the entity an alias, so the relationship line reads C ||--o{ O instead 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
erDiagram
CUSTOMER {
int customer_id PK
varchar email UK
varchar name "display name"
int loyalty_points
}
ORDER {
int order_id PK
int customer_id FK
datetime placed_at
decimal total
}
ORDER_ITEM {
int order_id PK, FK
int product_id PK, FK
int quantity
decimal unit_price
}
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ORDER_ITEM : contains

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, FK is 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.