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
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"
}
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
A ||--|| B: Exactly one A connects to exactly one B.A ||--o{ B: One A connects to zero or many B rows (Most common Parent-Child setup).A }|--|{ B: Many-to-Many connection (Typically requires a join table in actual implementation).
Keys
You can flag special keys by appending these shortcuts after the attribute name:
- PK : Primary Key
- FK : Foreign Key
- UK : Unique Key
Attributes can identify keys.
erDiagram
USER ||--o{ ORDER : places
USER {
int userId PK
string name
}
ORDER {
int orderId PK
int amount
}
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
}
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
}
Layout and Advanced Tips
- Handling Hyphens: If your table or entity name contains a hyphen, wrap it in double quotes when assigning relationships (e.g., "LINE-ITEM" ||--|| ORDER). Inside the curly braces block definition, you can use the name directly without quotes.
- Relationship Labels: Always append a trailing description string (e.g., : "places") at the end of relationship lines to clearly define the action between entities.
- No Code Injection: Attributes do not support complex constraint language (like NOT NULL or DEFAULT). Keep those details tucked inside the "comment string" at the end of the field row.