Mermaid ER Diagrams for Database Design

Design Entity-Relationship diagrams with Mermaid. Learn entity definitions, attribute types, relationship cardinality notation, and best practices for DB modeling.

Diagram Types

Detailed Explanation

ER Diagrams in Mermaid

Entity-Relationship (ER) diagrams visualize database schemas by showing tables (entities), their columns (attributes), and how they relate to each other. Mermaid's erDiagram syntax provides a concise way to define these schemas as code.

Defining Entities

Entities are defined with their name followed by attributes inside curly braces. Each attribute has a type and name:

erDiagram
    CUSTOMER {
        int id PK
        string name
        string email UK
        date created_at
    }
  • PK marks the primary key
  • UK marks a unique key
  • FK marks a foreign key

Relationship Cardinality

Mermaid uses a notation system for cardinality:

Notation Meaning
||--|| Exactly one to exactly one
||--o{ One to zero or more
||--|{ One to one or more
o|--o{ Zero or one to zero or more

The symbols mean: | = exactly one, o = zero, { = many.

A Complete Database Schema

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ ORDER_ITEM : contains
    PRODUCT ||--o{ ORDER_ITEM : "is ordered in"
    CUSTOMER {
        int id PK
        string name
        string email UK
        string phone
    }
    ORDER {
        int id PK
        int customer_id FK
        date order_date
        string status
        decimal total
    }
    ORDER_ITEM {
        int id PK
        int order_id FK
        int product_id FK
        int quantity
        decimal unit_price
    }
    PRODUCT {
        int id PK
        string name
        string sku UK
        decimal price
        int stock_count
    }

Best Practices

  1. Use PK/FK/UK annotations — they communicate constraints at a glance.
  2. Name relationships with verbsplaces, contains, belongs to make diagrams self-documenting.
  3. Keep entities focused — if an entity has more than 10-12 attributes, consider splitting it.
  4. Group related entities — place closely related tables near each other in the source code so Mermaid renders them adjacently.

ER diagrams in Mermaid do not support composite keys or check constraints natively, but you can add these details in annotations or comments.

Use Case

A database administrator designing the schema for a SaaS billing system. The ER diagram models customers, subscriptions, invoices, payments, and line items with proper cardinality — serving as the source of truth before writing migration scripts.

Try It — Mermaid Diagram Editor

Open full tool