Mermaid Entity Relationship Diagrams

Entity Relationship Diagrams (ERD) visualize database schemas, showing entities, their attributes, and relationships. Perfect for database design and documentation.

Use Case

Use ER diagrams when you need to:

  • Design database schemas
  • Document database structure
  • Show relationships between entities
  • Visualize data models
  • Communicate database architecture

Code

1```mermaid
2erDiagram
3    CUSTOMER ||--o{ ORDER : places
4    ORDER ||--|{ LINE-ITEM : contains
5    PRODUCT ||--o{ LINE-ITEM : "ordered in"
6```

Result:

Explanation

  • erDiagram - Start ER diagram
  • Entity names in UPPERCASE
  • Relationship syntax: Entity1 ||--o{ Entity2 : label
  • Cardinality symbols:
    • ||--|| : One to one
    • }o--|| : Many to one
    • }o--o{ : Many to many
    • ||--o{ : One to many
    • }o--|{ : One or more to many

Examples

Example 1: E-Commerce Database

 1```mermaid
 2erDiagram
 3    CUSTOMER {
 4        int customer_id PK
 5        string name
 6        string email
 7        string address
 8    }
 9    
10    ORDER {
11        int order_id PK
12        int customer_id FK
13        date order_date
14        decimal total
15    }
16    
17    PRODUCT {
18        int product_id PK
19        string name
20        decimal price
21        int stock
22    }
23    
24    ORDER_ITEM {
25        int order_id FK
26        int product_id FK
27        int quantity
28        decimal price
29    }
30    
31    CUSTOMER ||--o{ ORDER : places
32    ORDER ||--|{ ORDER_ITEM : contains
33    PRODUCT ||--o{ ORDER_ITEM : "ordered in"
34```

Result:

Example 2: University Database

 1```mermaid
 2erDiagram
 3    STUDENT {
 4        int student_id PK
 5        string name
 6        string email
 7    }
 8    
 9    COURSE {
10        int course_id PK
11        string title
12        int credits
13    }
14    
15    ENROLLMENT {
16        int student_id FK
17        int course_id FK
18        string grade
19    }
20    
21    PROFESSOR {
22        int professor_id PK
23        string name
24        string department
25    }
26    
27    STUDENT }o--o{ COURSE : enrolls
28    COURSE }o--|| PROFESSOR : "taught by"
29```

Result:

Relationship Cardinality

SymbolMeaningDescription
`--
`}o--`
`--o{`
}o--o{Many to ManyMany entities relate to many
`}o--{`One or More to Many

Notes

  • Entity names should be in UPPERCASE
  • Attributes are defined inside curly braces
  • PK = Primary Key, FK = Foreign Key
  • Relationship labels are optional but recommended
  • Use descriptive relationship names

Gotchas/Warnings

  • ⚠️ Entity Names: Must be uppercase and use hyphens for multi-word names
  • ⚠️ Attributes: Define inside entity blocks with type and constraints
  • ⚠️ Relationships: Cardinality symbols must match the relationship direction
  • ⚠️ Complexity: Large ER diagrams can become hard to read - break into modules

Related Snippets