4.3 Entity-Relationship Diagrams (ERD)

  1. Databases and relationships.

  2. erDiagram keyword.

  3. 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 User places an Order).

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 USER entity with id, name, and email.

  • There’s an ORDER entity with id, orderDate, and total.

  • The USER places ORDER relationship:

    • ||--o{ means one-to-many β†’ one user can place many orders.


βœ… Reading ERD Symbols

Symbol
Meaning

`

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

Now 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:

  • EntityName is the name of your table or data type.

  • Inside { } you list each attribute.

  • DataType can be int, 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 CUSTOMER can place many ORDERs.


βœ… Full Example

Here’s a small ER diagram using the erDiagram keyword:

What this shows:

  • A CUSTOMER places many ORDERs.

  • Each ORDER contains many PRODUCTs.

  • The diagram maps out your whole data flow in a simple, readable way.


βœ… Why Use erDiagram

The 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 erDiagram to 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:

  • id uniquely identifies the product.

  • name gives it a title.

  • price stores 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 CUSTOMER can place many ORDERs.

  • Each ORDER can contain many PRODUCTs.

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