Mermaid guide

Mermaid ER Diagram Syntax: Entities, Attributes, Keys, and Crow's-Foot Cardinality

The ER diagram documents a database schema: entities with their columns, and relationships that state how many rows on each side participate. Mermaid uses crow's-foot notation, the same symbols most schema tools draw, so the diagram reads naturally to anyone who has looked at a data model.

Each entity is an uppercase name followed by an attribute block. Relationship lines are written with a compact token that encodes the cardinality on both ends plus whether the relationship is identifying (solid) or non-identifying (dashed).

  • database schema docs
  • data modelling
  • migration planning
  • review of ORM mappings

Example

Mermaid Entity Relationship example

owns

USERS

text

id

PK

text

email

text

plan

int

created_at

DIAGRAMS

text

id

PK

text

code

text

r2_key

int

view_count

int

last_viewed_at

text

user_id

FK

erDiagram
    USERS {
        text id PK
        text email
        text plan
        int created_at
    }
    DIAGRAMS {
        text id PK
        text code
        text r2_key
        int view_count
        int last_viewed_at
        text user_id FK
    }
    USERS ||--o{ DIAGRAMS : "owns"

Syntax

Entities and attributes

erDiagram
    USER {
        uuid id PK
        text email UK
        timestamp created_at
    }
    POST {
        uuid id PK
        uuid user_id FK
        text title
        text body "markdown"
    }
Entities and attributes — Mermaid er

USER

uuid

id

PK

text

email

UK

timestamp

created_at

POST

uuid

id

PK

uuid

user_id

FK

text

title

text

body

markdown

Attribute lines are type name [PK|FK|UK] ["comment"]. Types are free text; Mermaid does not validate them.

Try this snippet →

Relationships

erDiagram
    USER ||--o{ POST : writes
    POST ||--|{ COMMENT : has
    USER }o--o{ ROLE : assigned
    CART ||..|| ORDER : becomes
Relationships — Mermaid er

writes

has

assigned

becomes

USER

POST

COMMENT

ROLE

CART

ORDER

Left token, line, right token. || exactly one, |o zero or one, }o zero or more, }| one or more. -- identifying, .. non-identifying.

Try this snippet →

Aliases and styling

erDiagram
    "Order Line" {
        int qty
    }
    ORDER ||--|{ "Order Line" : contains
Aliases and styling — Mermaid er

contains

Order Line

int

qty

ORDER

Quote an entity name to use spaces. Relationship labels are free text and appear on the line.

Try this snippet →

Common mistakes

These are the parse errors that come up most, including in code written by ChatGPT and Claude. Paste any failing diagram into the editor and it marks the offending line.

  • Cardinality token reversed: POST o{--|| USER.

    Fix: The symbol closest to each entity describes that entity's side: USER ||--o{ POST means one user, zero-or-more posts.

  • Attribute type and name swapped (id uuid PK).

    Fix: Type comes first: uuid id PK. Swapping them renders but labels the column "uuid".

  • Lowercase entity names mixed with uppercase ones.

    Fix: Mermaid accepts both, but the convention is uppercase. Pick one style; AI tools follow whatever your example uses.

Real-world examples

Complete, copy-ready entity relationships for common situations. Each one opens in the editor with a click.

Browse all examples →

FAQ

Can I generate an ER diagram from an existing schema?

Not inside Mermaid, but it is a good prompt: paste your CREATE TABLE statements into ChatGPT or Claude and ask for a Mermaid erDiagram with PK/FK markers. Then paste the result into MermanDraw to check it renders.

Does the ER diagram support composite keys?

Mark each column with PK. There is no syntax for a combined constraint, so note it in a comment string on the attribute.