Going Stellar

Home/Methods

Mermaid · Diagrams

Mermaid ER diagram: syntax, cardinality and keys, with examples

A Mermaid ER diagram is one line per relationship: two entities, the crow's foot markers between them and a label. Attributes, keys and comments go in a block under each entity. Every example below renders as written in Mermaid 12.3 and 11.17, and opens in our free editor.

Updated

The smallest ER diagram

Start with erDiagram, then write one relationship per line: the first entity, the relationship, the second entity, a colon and a label.

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains
    CUSTOMER }|..|{ DELIVERY-ADDRESS : uses

Open this example in the editor (ER: customer, order, line item).

Read the first line as “a customer places zero or more orders, and an order belongs to exactly one customer”. The label describes the relationship from the first entity's side. Entity names are usually singular and in capitals, but Mermaid requires neither; hyphens and underscores are allowed.

Cardinality: the crow's foot markers

Each end of the line has a marker of two characters. The outer character is the maximum (one or many), the inner one the minimum (zero or one). The left end describes how many of the first entity, the right end how many of the second.

LeftRightMeaning
|oo|zero or one
||||exactly one
}oo{zero or more
}||{one or more
erDiagram
    TEAM ||--|{ MEMBER : has
    MEMBER |o--o| LAPTOP : "is issued"
    TEAM ||--o{ PROJECT : owns
    PROJECT }o--o{ MEMBER : "is staffed by"

Open this example in the editor (ER: the four cardinalities).

A team has one or more members; a member has zero or one laptop, and a laptop is issued to zero or one member. A project and its members are many-to-many. Mermaid also accepts words: CAR 1 to zero or more NAMED-DRIVER : allows draws the same as ||--o{.

Identifying and non-identifying relationships

Two hyphens, --, draw a solid line: an identifying relationship, where the child cannot exist without the parent. Two dots, .., draw a dashed line: both entities exist on their own.

erDiagram
    CAR ||--o{ NAMED-DRIVER : allows
    PERSON ||--o{ NAMED-DRIVER : is
    PERSON }o..o{ CAR : "has driven"

Open this example in the editor (ER: identifying and non-identifying).

A named driver only exists for a given car and person, so both of its relationships are solid. A person and a car exist independently, so the many-to-many line between them is dashed. In words, to is identifying and optionally to is not.

Attributes, keys and comments

Under an entity, a block in braces lists its attributes, one type name pair per line. After the name come the keys, PK, FK or UK, and a comment in double quotes. Several keys on one attribute are separated by a comma.

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ ORDER_ITEM : contains
    PRODUCT ||--o{ ORDER_ITEM : "is ordered as"
    CUSTOMER {
        int id PK
        string email UK
        string name
    }
    ORDER {
        int id PK
        int customer_id FK
        date placed_at
        string status "new, paid or shipped"
    }
    ORDER_ITEM {
        int order_id PK, FK
        int product_id PK, FK
        int quantity
        decimal unit_price
    }
    PRODUCT {
        int id PK
        string sku UK
        string title
    }

Open this example in the editor (ER: shop schema with keys).

Types are free text: Mermaid does not check them against any database, so decimal, varchar(255) and string[] are all fine. A type must start with a letter. Whether to list foreign keys is your choice; the relationship lines already show them.

Aliases, direction and optional types

An alias in square brackets after the entity name is what the box shows, so the name you type can stay short. direction sets the layout: TB (the default), BT, LR or RL. Since Mermaid 11.16, a type ending in ? marks an optional or nullable attribute.

erDiagram
    direction LR
    dept["Department"] {
        int id PK
        string name
    }
    emp["Employee"] {
        int id PK
        string name
        string? phone
        int dept_id FK
    }
    dept ||--o{ emp : employs

Open this example in the editor (ER: aliases, direction, optional types).

Subgraphs for large schemas

Since Mermaid 11.17, subgraph … end groups entities into labelled areas, for example one per domain. A title with spaces goes in double quotes. Relationships can cross from one group to another.

erDiagram
    ACCOUNT ||--o{ INVOICE : receives
    ACCOUNT ||--o{ WORKSPACE : owns
    subgraph Billing
        INVOICE ||--|{ INVOICE_LINE : lists
    end
    subgraph "Product usage"
        WORKSPACE ||--o{ MEMBER : has
    end

Open this example in the editor (ER: subgraphs by domain).

Mistakes, with and without an error

A relationship without a label. Once a line has a relationship, the colon and the label are required. This fails with Expecting 'COLON':

erDiagram
    CUSTOMER ||--o{ ORDER

If you do not want a label, give an empty one:

erDiagram
    CUSTOMER ||--o{ ORDER : ""

Open this example in the editor (ER: relationship with an empty label).

Two keys without a comma. int id PK FK fails with a parse error on that line; write PK, FK.

erDiagram
    CUSTOMER {
        int id PK FK
    }

A name with a space, without quotes. This one draws without any error, which makes it easy to miss: the chart gets three boxes, LINE, ITEM and PRODUCT, instead of two.

erDiagram
    LINE ITEM }o--|| PRODUCT : "refers to"

Put the name in double quotes, or declare an alias on its own line and use the short id in relationships. An alias written inside a relationship line fails with a parse error.

erDiagram
    "LINE ITEM" }o--|| PRODUCT : "refers to"
    item["Order line"]
    item }o--|| ORDER : "belongs to"

Open this example in the editor (ER: entity names with spaces).

The same code, two looks

Mermaid 12 draws ER diagrams with a new default theme and look, and lays them out with ELK instead of Dagre. Mermaid 11 uses the classic look. The boxes, lines and labels are the same; the colours and the placement differ. Our editor uses 12.3.0; a site, wiki or app on Mermaid 11 shows the older look. To get the previous look in 12, set theme: default, look: classic and layout: dagre in the diagram's front matter.

ER diagrams in Confluence

Confluence Cloud does not render Mermaid on its own. Mermaid Studio for Confluence, our app, does: type /Mermaid on a page, write or paste the code with a live preview, or start from one of eight templates (flowchart, sequence, Gantt, class, ER, state, pie, mind map). The diagram follows Confluence's light or dark mode, or keeps one of five themes. Copying the SVG is free; Pro adds downloads as PNG or SVG and an AI that writes the Mermaid from a plain-English description. It runs Mermaid 11.17.2, and every example on this page renders there as written.

Limits

Every example was rendered on 29 November 2026 with Mermaid 12.3.0, the version in our editor, and with 11.17.2, the version in Mermaid Studio, in light and dark themes. An ER diagram in Mermaid is a drawing: it does not connect to a database, check types or generate tables, and Mermaid draws the cardinality you type even when it contradicts your schema.

Sources

  1. Mermaid documentation: Entity Relationship Diagrams
  2. Mermaid documentation: Diagram Syntax
  3. Atlassian Marketplace: Mermaid Studio for Confluence (version 3.10.0)

Questions and answers

How do I write a one-to-many relationship in a Mermaid ER diagram?

CUSTOMER ||--o{ ORDER : places: exactly one customer, zero or more orders. Use ||--|{ when there must be at least one.

How do I mark a primary key and a foreign key in Mermaid?

After the attribute name: int id PK, int customer_id FK. For both on one attribute, separate them with a comma: PK, FK.

What is the difference between -- and .. in a Mermaid ER diagram?

-- draws a solid line for an identifying relationship; .. draws a dashed line for a non-identifying one.

Can a Mermaid ER diagram be generated from my database?

Not by Mermaid itself: it only draws the text you give it. Some database tools can export their schema as Mermaid text, which you then paste into an editor.

How to cite this

Going Stellar (2026). Mermaid ER diagram: syntax, cardinality and keys, with examples. GOING STELLAR S.R.L..
https://goingstellar.ro/methods/mermaid-er-diagram-syntax-examples/

Read as Markdown

All methods

Contact

Tell us what is not working

Write in English or Romanian. A person reads it — usually the same person who would do the work — and answers within one business day.