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
| Symbol | Meaning | Description |
|---|---|---|
| ` | -- | |
| `}o-- | ` | |
| ` | --o{` | |
}o--o{ | Many to Many | Many 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
- Architecture Diagrams (C4 Model)
Create system architecture diagrams using C4 model with Mermaid - Chart.js Bar Chart
Create bar charts for data visualization - Graphviz DOT Diagrams
Create complex graph layouts with Graphviz DOT language - KaTeX Math Examples and Tips
Comprehensive guide to KaTeX math notation with examples and tips - Mermaid Arbitrary Graphs
Create arbitrary graphs and networks with Mermaid - Mermaid Block Diagrams
Create block diagrams for system architecture with Mermaid - Mermaid Charts (Pie, Bar, Line)
Create pie charts, bar charts, and line charts with Mermaid - Mermaid Class Diagrams (UML)
Create UML class diagrams with Mermaid - Mermaid Flowchart
Create flowcharts and decision trees with Mermaid - Mermaid Gantt Chart
Create Gantt charts for project timelines and scheduling - Mermaid Git Diagrams
Create Git branch and commit diagrams with Mermaid - Mermaid Kanban Boards
Create Kanban boards for project management with Mermaid - Mermaid Mindmaps
Create mindmaps for brainstorming and organizing ideas with Mermaid - Mermaid Packet Diagrams
Create packet diagrams for network protocols with Mermaid - Mermaid Quadrant Charts
Create quadrant charts for prioritization with Mermaid - Mermaid Radar Charts
Create radar charts for multi-dimensional comparisons with Mermaid - Mermaid Requirement Diagrams
Create requirement diagrams for system requirements with Mermaid - Mermaid Sankey Diagrams
Create Sankey diagrams for flow visualization with Mermaid - Mermaid Sequence Diagram
Create sequence diagrams for interactions and API flows - Mermaid State Diagrams
Create state diagrams and state machines with Mermaid - Mermaid Timeline Diagrams
Create timeline diagrams for chronological events with Mermaid - Mermaid Treemap Diagrams
Create treemap diagrams for hierarchical data visualization with Mermaid - Mermaid User Journey Diagrams
Create user journey maps with Mermaid - Mermaid ZenUML Diagrams
Create ZenUML sequence diagrams with Mermaid - P5.js Interactive Visualizations
Create interactive visualizations and animations with P5.js