ERDs with Mermaid

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.

erd-students-etc

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:

  1. Entity Declaration:
    • Use the ENTITY_NAME { ... } syntax to define an entity.
    • Within the curly braces, list the attributes of the entity, one per line.
  2. 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 one
      • 0..1: Zero or one
      • 0..*: Zero or many
      • 1..*: One or many

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: