Mermaid ER relationship cheat sheet: cardinality, lines, and keys
Every Mermaid erDiagram relationship marker, its word form, solid and dashed lines, attribute keys and comments, display names, direction, and styling, with examples you can paste.
A Mermaid ER relationship packs four decisions into one line: how many records on the left, solid or dashed, how many on the right, and a label. This page is the reference for each part, plus the attribute, naming, and styling syntax around them. For a worked example of why each choice matters, start with Mermaid ER diagrams explained with an order database.
The shape of a relationship
FIRST_ENTITY <left marker><line><right marker> SECOND_ENTITY : label
Each marker describes the entity it's next to. In CUSTOMER ||--o{ ORDER : places, the || beside CUSTOMER says every order has exactly one customer, and the o{ beside ORDER says a customer has zero or more orders. Read every line twice, once from each end.
The label is required. Use quotes when it has spaces, and "" when you want no label at all.
Cardinality markers
| Meaning | Left side | Right side | Drawn as |
|---|---|---|---|
| Zero or one | |o |
o| |
a ring and a bar |
| Exactly one | || |
|| |
two bars |
| Zero or more | }o |
o{ |
a ring and a crow's foot |
| One or more | }| |
|{ |
a bar and a crow's foot |
The ring means "may be none" (optional). The bar means "at least one" (mandatory). The crow's foot means "many". The left and right forms are mirror images, so the open side of { or } always faces the entity it describes.
erDiagram
COUNTRY ||--o{ CITY : contains
CITY |o--o| MAYOR : "is led by"
CITY ||--|{ DISTRICT : "is divided into"Each city is in exactly one country and a country has zero or more cities. A city has zero or one mayor, and each mayor leads zero or one city. A city has at least one district.
Word forms
Mermaid also accepts words instead of symbols, which some reviewers find easier to read:
| Meaning | Words Mermaid accepts |
|---|---|
| Zero or one | zero or one, one or zero |
| Exactly one | only one, one, 1 |
| Zero or more | zero or more, zero or many, many(0), 0+ |
| One or more | one or more, one or many, many(1), 1+ |
With words, the line is to (solid) or optionally to (dashed):
erDiagram
CAR only one to zero or more TRIP : makes
DRIVER zero or one optionally to zero or more TRIP : drivesPick one style per diagram. Mixing symbols and words makes a diagram harder to scan, and Flowgraph's Manual mode always writes symbols when it changes a line.
Solid and dashed lines
| Line | Symbol | Word | Use it when |
|---|---|---|---|
| Identifying | -- |
to |
the child's key includes the parent's key, so it can't exist or be identified on its own |
| Non-identifying | .. |
optionally to |
the child has its own key and only refers to the parent |
An order line keyed by (order_id, line_number) is identifying: a line number means nothing without its order. An order that refers to a customer by customer_id but has its own id is non-identifying, even when customer_id is required. "Required" is what the cardinality says; "identifying" is about keys.
erDiagram
PURCHASE_ORDER ||--|{ ORDER_LINE : contains
CUSTOMER ||..o{ PURCHASE_ORDER : places
ORDER_LINE {
int order_id PK, FK
int line_number PK
int quantity
}
PURCHASE_ORDER {
int id PK
int customer_id FK
}Many diagrams draw every relationship solid. That's harmless for a quick sketch, but if your team reads solid as "part of the key", be consistent: Flowgraph's SQL import draws a solid line only when the foreign key is part of the primary key.
Attributes, keys, and comments
ENTITY {
type name
type name PK
type name PK, FK
type name UK "a comment in quotes"
}
- The type and name are each one word. Brackets and parentheses are allowed, so
varchar(255),decimal(10,2)andstring[]work; spaces don't. - Keys come after the name:
PK(primary),FK(foreign),UK(unique), separated by commas when there are several. - A comment goes last, in double quotes.
- An entity can have no attribute block at all. It's drawn as a plain box, which suits overview diagrams.
Names and display names
Entity names are usually written in capitals with underscores, such as ORDER_ITEM, because a name can't contain spaces. To show a friendlier name, add a display name in square brackets and keep the short name for the relationships:
erDiagram
ORDER_ITEM["Order item"] {
int quantity
}
PRODUCT["Product"]
PRODUCT ||--o{ ORDER_ITEM : "appears in"The display name goes where the entity is declared, on its block or a line of its own; a relationship line only takes the short name.
Direction and styling
direction sets the flow: TB (top to bottom, the default), BT, LR, or RL. Wide schemas with long chains often read better left to right.
style colors one entity; classDef defines a reusable style that class (or :::name) applies to several:
erDiagram
direction LR
CUSTOMER ||--o{ PURCHASE_ORDER : places
PURCHASE_ORDER ||--|{ ORDER_ITEM : contains
classDef core fill:#e6f2ee,stroke:#0f5c4a
class CUSTOMER,PURCHASE_ORDER core
style ORDER_ITEM stroke-dasharray: 4 3Keep styling for meaning, such as "these tables belong to the billing service", rather than decoration. In Flowgraph Studio the Layout tab can also set the direction without editing the code, and the themes color every entity at once.
A checklist before you share
- Every relationship has a label a newcomer would understand.
- Each end's cardinality has been read aloud as a sentence and matches the business rule, not just the column's nullability.
- Solid and dashed lines mean the same thing everywhere in the diagram.
- Keys are marked on the attributes that carry them, including both halves of a composite key.
- The diagram fits on a screen; if it doesn't, split it (keeping a large database readable shows how).
The official Mermaid ER syntax reference has the full grammar. In Flowgraph Studio, Manual mode sets the markers and line style from menus, so you can check this page against the drawing rather than memorise it.
More guides
- Edit Mermaid diagrams visually in VS CodeDrag, connect and restyle a Mermaid diagram on Flowgraph's canvas inside VS Code, and every change is written back into the .mmd file or Markdown block as ordinary code.
- Keep a diagram's style in its code with %% flowgraph-themeOne comment line keeps a Mermaid diagram's Flowgraph theme in the file, so it looks the same in your repository, in VS Code, from the CLI and through the render API.
- Preview Flowgraph diagrams in VS Code, with icons, layout and your themeInstall the Flowgraph Studio extension to preview Mermaid files and Markdown blocks in VS Code as Flowgraph draws them, see mistakes in plain words, and export SVG or PNG.