4.3 Entity-Relationship Diagrams (ERD)
Databases and relationships.
erDiagramkeyword.Entities, attributes, relationships.
1. Databases and Relationships
When designing a software system, itβs just as important to understand how data is stored and linked as it is to know how the processes flow or how the code is structured. This is where Entity-Relationship Diagrams (ERDs) come in β and Mermaid makes it easy to create them with simple text.
β
What is an Entity-Relationship Diagram (ERD)?
An Entity-Relationship Diagram shows:
Entities: things you store information about (like
User,Order,Product).Attributes: properties of those entities (like
name,price,email).Relationships: how entities are connected (for example, a
Userplaces anOrder).
ERDs help you visualize your database structure β theyβre like a blueprint for your tables and how they link together.
β
Why Use ERDs?
ERDs are useful because they:
Make your database easy to understand, even for non-technical stakeholders.
Help designers and developers see which tables link to which.
Clarify one-to-one, one-to-many, or many-to-many relationships.
Prevent mistakes like duplicate data or missing connections.
β
How ERDs Work
1οΈβ£ Entities Each entity is like a database table. For example:
2οΈβ£ Attributes
Attributes are the columns or fields in each table.
For example, the User entity has id, name, and email.
3οΈβ£ Relationships Relationships show how one entity is linked to another. Common types are:
One-to-One (
||--||): each record links to exactly one other record.One-to-Many (
||--o{): one record links to multiple records.Many-to-Many (
}o--o{): multiple records link to multiple records.
β
Example: Users and Orders
Hereβs a simple ERD example in Mermaid:
What this shows:
Thereβs a
USERentity withid,name, andemail.Thereβs an
ORDERentity withid,orderDate, andtotal.The
USERplacesORDERrelationship:||--o{means one-to-many β one user can place many orders.
β
Reading ERD Symbols
`
o{
Many
}o
Many
--
Connects two entities
So USER ||--o{ ORDER means:
A single USER can have many ORDERS.
β
Why It Matters
Mapping your database structure with an ERD helps you:
Spot missing relationships before you start building.
Explain your data model to your team.
Keep your database normalized and organized.
Mermaidβs text-based syntax makes it easy to update your ERD anytime your data model evolves β no separate diagramming tool needed!
In short:
π ERDs map out your data structure:
Entities hold data
Attributes define the details
Relationships show how everything connects. This keeps your database logical, scalable, and easy to maintain.
2. erDiagram Keyword
erDiagram KeywordNow that you understand the purpose of Entity-Relationship Diagrams (ERDs), letβs see exactly how to write one in Mermaid.
Mermaid makes it simple with the erDiagram keyword β this tells Mermaid: βRender this as a database entity-relationship diagram.β
β
How to Start an ERD
Every ERD in Mermaid begins with:
After that, you define:
Entities (like tables)
Attributes (fields inside the entity)
Relationships (the lines that connect entities)
β
Basic Entity Syntax
Each entity is written like this:
EntityNameis the name of your table or data type.Inside
{ }you list each attribute.DataTypecan beint,string,float,booleanβ anything you want.
Example:
This creates an entity named CUSTOMER with three attributes.
β
Defining Relationships
After you define your entities, you show how they connect with special symbols:
||--||β One-to-One||--o{β One-to-Many}o--o{β Many-to-Many
Each relationship line ends with a : Label to describe it.
Example:
This means:
One
CUSTOMERcan place manyORDERs.
β
Full Example
Hereβs a small ER diagram using the erDiagram keyword:
What this shows:
A
CUSTOMERplaces manyORDERs.Each
ORDERcontains manyPRODUCTs.The diagram maps out your whole data flow in a simple, readable way.
β
Why Use erDiagram
erDiagramThe erDiagram keyword:
Switches Mermaid to ERD mode.
Lets you describe your tables and fields as code.
Keeps your database design version-controlled with your docs or project.
Makes updating your data model as easy as editing text.
In short:
π Use
erDiagramto write Entity-Relationship Diagrams in clear text β so your data structure stays simple, visible, and easy to share.
3. Entities, Attributes, Relationships
When you build an Entity-Relationship Diagram (ERD) in Mermaid, youβre mapping out three main parts: entities, attributes, and relationships. Together, these describe the blueprint of your database β who stores what, and how theyβre connected.
β
1οΈβ£ Entities
Entities are the main objects or tables in your database.
Each entity is something you want to store data about β for example: Customer, Order, Product, Invoice.
In Mermaid, you declare each entity with a name and a block of { }.
Example:
This creates a CUSTOMER entity (or table) with three columns: id, name, and email.
β
2οΈβ£ Attributes
Attributes are the properties or fields that belong to an entity β they describe the details you want to store.
For example, a PRODUCT might have:
iduniquely identifies the product.namegives it a title.pricestores its cost.
Mermaid uses simple type labels like int, string, or float to make your ERD easier to read β but they wonβt enforce real database types.
β
3οΈβ£ Relationships
Relationships show how your entities link together. They explain which tables reference each other β which is the foundation of relational databases.
In Mermaid ERDs, you use special line symbols to define:
One-to-One:
||--||One-to-Many:
||--o{Many-to-Many:
}o--o{
And you label each relationship to show what it means.
Example:
This means:
A single
CUSTOMERcan place manyORDERs.Each
ORDERcan contain manyPRODUCTs.
This clear mapping helps everyone see:
Which tables are connected
How they depend on each other
How data flows between them
β
Putting It Together
Hereβs a complete example with all three parts:
β
Why This Matters
When you:
Design a new database, you use entities, attributes, and relationships to plan your tables.
Update a schema, you keep your ERD up to date so everyone understands changes.
Onboard teammates, you show them how data is organized without needing to open the database directly.
In short:
π Entities = the things you store data about π Attributes = the details of each thing π Relationships = the connections that link them together
Together, they make your database clear, consistent, and easy to maintain β and Mermaid makes describing them as simple as writing a few lines of text.
Last updated