ERDs with Mermaid
- id: 1730206218
- Date: Feb. 15, 2025, 5:18 p.m.
- Author:
Overview
Goals: Describe ERDs. Skillfully create ERDs with Mermaid.
id:
See also this explanation from mermaid.js.org
ERDs with Mermaid
Mermaid is a powerful tool for creating various types of diagrams, including Entity-Relationship Diagrams (ERDs).
An entity-relationship diagram (ERD) is a visual representation of entities, their attributes, and the relationships between them in a database. ERDs are extremely useful for designing databases.
Example of an ERD
Here is an example.
Here is the corresponding mermaid code.
erDiagram
STUDENT {
int student_id PK
string name
date birth_date
}
COURSE {
int course_id PK
string course_name
}
ENROLLMENT {
int id PK
int student_id FK
int course_id FK
date enrollment_date
}
STUDENT ||--o{ ENROLLMENT : "enrolls in"
COURSE ||--o{ ENROLLMENT : "has enrollment"
Key Components:
- Entity Declaration:
- Use the
ENTITY_NAME { ... }
syntax to define an entity. - Within the curly braces, list the attributes of the entity, one per line.
- Use the
- Relationship Declaration:
- Connect entities using a line and specify the cardinality and
relationship label:
ENTITY_1 "cardinality1" -- "cardinality2" ENTITY_2 : relationship_label
- Cardinality:
1
: One and only one0..1
: Zero or one0..*
: Zero or many1..*
: One or many
- Connect entities using a line and specify the cardinality and
relationship label:
Example:
erDiagram
CUSTOMER {
id
name
address
}
ORDER {
id
date
total
}
CUSTOMER "1" -- "0..*" ORDER : places
This example defines two entities: CUSTOMER
and
ORDER
. A customer can place zero or many orders, while an
order can only be placed by one customer.
Additional Tips:
- Formatting: Use Markdown formatting within the entity and attribute descriptions.
- Styling: While Mermaid doesn’t directly support custom styling for ERDs, you can explore CSS customization for more advanced styling.
- Complex Relationships: For more complex relationships, consider using techniques like associative entities or higher-order relationships.
- Clarity and Conciseness: Keep your ERDs clear and concise by focusing on the essential information.