Advertisement
❮ Previous: Mermaid Class Diagrams Next: Mermaid Gantt Charts ❯

Mermaid ER Diagrams

ER (Entity-Relationship) diagrams model database structures.

Mermaid Entity-Relationship Diagrams (erDiagram) are specialized layouts designed explicitly for schema modeling and database design. They focus on Entities (database tables), Attributes (columns with data types), and Relationships (foreign key connections with structural cardinality).

Mermaid uses Crow's Foot-style relationship notation.


Basic ER Syntax

erDiagram
    CUSTOMER {
        string name
        int age
    }

    ORDER {
        int orderNumber
        date orderDate
    }

    CUSTOMER ||--o{ ORDER : places

Try this code ❯


1. Declaring Entities and Attributes

An entity block represents a database table. Inside the curly braces {}, you define the fields using the format: DataType AttributeName Keys "Comments".

erDiagram
    CUSTOMER {
        int customer_id PK
        string first_name
        string email UK "Primary contact"
    }

Try this code ❯


Cardinality

Relationships show how rows in one table connect to rows in another. The syntax uses a combination of bars (|) and circles (o) or crow's feet ({ or }) to define constraints on both sides of the line:

Cardinality Symbol Meaning
|| Exactly One
|o Zero or One
|{ One or Many
o{ Zero or Many

Examples of Complete Connectors


Keys

You can flag special keys by appending these shortcuts after the attribute name:

Attributes can identify keys.

erDiagram
    USER ||--o{ ORDER : places

    USER {
        int userId PK
        string name
    }

    ORDER {
        int orderId PK
        int amount
    }

Try this code ❯

PK marks a primary key.

The source also describes FK for foreign keys and ? for nullable attributes.


Blog Database Example

erDiagram
    USER ||--o{ POST : writes

    USER {
        int id PK
        string username
    }

    POST {
        int id PK
        string title
    }

Try this code ❯


Comprehensive E-Commerce Example

Here is a multi-table database schema displaying structural constraints, key assignments, data types, and text-labeled relationship directions:

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE-ITEM : contains
    PRODUCT ||--o{ LINE-ITEM : ordered-in

    CUSTOMER {
        int id PK
        string name
        string email UK
        string status
    }

    ORDER {
        int id PK
        int customer_id FK
        date order_date
        string status
    }

    LINE-ITEM {
        int id PK
        int order_id FK
        int product_id FK
        int quantity
        price unit_price
    }

    PRODUCT {
        int id PK
        string sku UK
        string title
        int stock_level
    }

Try this code ❯


Layout and Advanced Tips

❮ Previous: Mermaid Class Diagrams Next: Mermaid Gantt Charts ❯
Advertisement