Home / Examples / ER Diagram

Mermaid ER Diagram Examples

Entity relationship diagrams document database tables and how they relate. Keeping them in Mermaid means the schema diagram can live in the same pull request as the migration that changes it.

An ER diagram starts with erDiagram. Each relationship line names two entities, a cardinality marker on each side, and a label.

E-commerce schema with keys

Attributes go inside braces as type name, with optional PK, FK, or UK markers. Quote a label when it contains spaces.

erDiagram
  CUSTOMER ||--o{ ORDER : places
  ORDER ||--|{ ORDER_ITEM : contains
  PRODUCT ||--o{ ORDER_ITEM : "appears in"
  CUSTOMER {
    int id PK
    string email UK
    string name
  }
  ORDER {
    int id PK
    int customer_id FK
    date created_at
    string status
  }
  ORDER_ITEM {
    int order_id FK
    int product_id FK
    int quantity
  }
  PRODUCT {
    int id PK
    string sku UK
    decimal price
  }

Multi-tenant SaaS model

A membership table connects users and organizations, so one user can belong to many organizations and each organization can have many users.

erDiagram
  ORGANIZATION ||--|{ MEMBERSHIP : has
  USER ||--o{ MEMBERSHIP : holds
  ORGANIZATION ||--o{ PROJECT : owns
  PROJECT ||--o{ API_KEY : issues
  MEMBERSHIP {
    int user_id FK
    int organization_id FK
    string role
  }

Blog with a many-to-many relationship

}o--o{ draws a many-to-many link directly. In the real database this usually becomes a join table such as POST_TAG.

erDiagram
  AUTHOR ||--o{ POST : writes
  POST ||--o{ COMMENT : receives
  POST }o--o{ TAG : "tagged with"
  POST {
    int id PK
    string slug UK
    datetime published_at
  }

ER Diagram syntax cheat sheet

SyntaxMeaning
|o / o|Zero or one
||Exactly one
}o / o{Zero or more
}| / |{One or more
--Identifying relationship (solid line)
..Non-identifying relationship (dashed line)
ENTITY { type name PK }Attributes with key markers PK, FK, UK

Common er diagram mistakes

  • The label after the colon is required. Use : "" if you really want no label.
  • Labels with spaces need double quotes: : "appears in".
  • Entity names cannot contain spaces. Use ORDER_ITEM rather than ORDER ITEM.

ER Diagram FAQ

How do I read ||--o{?
Read it left to right: exactly one entity on the left relates to zero or more entities on the right. For example, one customer places zero or more orders.
Can I add comments to attributes?
Yes. Add a quoted string after the attribute, for example string email UK "login identifier".

Keep going

More Mermaid examples

Last updated 2026-11-28.