17  Mermaid ER Diagrams

We use mermaid Entity Relationship Diagram for diagramming since markdown syntax of mermaid is more easily understood and markdown as plain text is version controllable with source control tools like git. Also mermaid diagrams are automatically rendered by web versions of source control tools such as github, gitlab and azure devops.

17.1 Entity

Entities are most basic part in the diagrams. They correspond to database tables normally. We can also give their attributes or columns in the diagram too. See below example.

erDiagram
    Student {
        int student_id PK
        string name
    }

erDiagram
    Student {
        int student_id PK
        string name
    }

17.2 Entity Relationships

Entities should have relationships. That is how they interact with other entities. The syntax for it is below:

<first-entity> [<relationship> <second-entity> : <relationship-label>]

Relationship label should show how it works in the requirements or domain. Please try to choose it accordingly.

An example a student enrolls in many courses. We could write it like below.

erDiagram
    Student ||--o{ Course : enrolls

erDiagram
    Student ||--o{ Course : enrolls

In this syntax, following table shows how we can model cardinality of the entities. That is 0,1 or many information between the entities.

Value (left) Value (right) Meaning
|o o| Zero or one
|| || Exactly one
}o o{ Zero or more (no upper limit)
}| |{ One or more (no upper limit)

We can read this information following way then

  • Student has zero to one advisor
  • Student has exactly one advisor
  • Student enrolls in 0-to-many courses
  • Student enrolls in 1-to-many courses

17.3 Full example 1

erDiagram
    STUDENT ||--o{ COURSE : enrolls
    COURSE ||--|{ LESSON : contains
    TEACHER ||--o{ COURSE : teaches
    TEACHER ||--o{ LESSON : conducts
    STUDENT ||--o{ LESSON : attends


    STUDENT {
        int id PK
        string name
        date created_at
        date updated_at
    }
    COURSE {
        int id PK
        string title
        string description
        date created_at
        date updated_at
    }
    LESSON {
        int id PK
        int course_id FK
        string title
        date scheduled_date
        date created_at
        date updated_at
    }
    TEACHER {
        int id PK
        string name
        string email
        date created_at
        date updated_at
    }

erDiagram
    STUDENT ||--o{ COURSE : enrolls
    COURSE ||--|{ LESSON : contains
    TEACHER ||--o{ COURSE : teaches
    TEACHER ||--o{ LESSON : conducts
    STUDENT ||--o{ LESSON : attends


    STUDENT {
        int id PK
        string name
        date created_at
        date updated_at
    }
    COURSE {
        int id PK
        string title
        string description
        date created_at
        date updated_at
    }
    LESSON {
        int id PK
        int course_id FK
        string title
        date scheduled_date
        date created_at
        date updated_at
    }
    TEACHER {
        int id PK
        string name
        string email
        date created_at
        date updated_at
    }

https://mermaid.live/

17.4 Use LLMs to generate ER diagrams

We can use LLMs like ChatGPT, Gemini or Perplexity to create our draft diagrams since mermaid is normal markdown code.

Below AI prompt creates a draft mermaid diagram to start from.

Please create me a simple mermaid entity relationship diagram which shows students, courses, lessons and teachers. All of the entities should have common columns in it.

Try it in AI tools to see the result.