Mermaid entity relationship diagrams

Draw a database's tables and how they connect, in plain text. This guide covers the ER diagram syntax, with examples you can load into the converter and download as a PDF.

Markdown
Preview

Your first ER diagram

An entity relationship diagram shows the things a system stores, called entities, and how they relate to one another. In practice the entities are usually database tables: customers, orders, products. ER diagrams are the usual way to plan a database before building it, and to explain an existing one to someone new.

Start the block with erDiagram. Each line is a relationship: an entity, a connector, a second entity, a colon and a label describing the relationship.

```mermaid
erDiagram
  AUTHOR ||--o{ BOOK : writes
  PUBLISHER ||--|{ BOOK : publishes
```

Entity names are often written in capitals, which makes them stand out from the labels. That's only a convention, and lowercase names work just as well. Labels of more than one word are safest in double quotes, as in : "appears in".

Reading the line ends

Each end of a connector says how many of that entity can take part, in what's called crow's foot notation. The outer symbol is the maximum: a straight bar means one, and the three-pronged “crow's foot” means many. The inner symbol is the minimum: a bar means at least one, and a circle means zero, so there may be none.

Left endRight endMeans
|oo|Zero or one
||||Exactly one
}oo{Zero or more
}||{One or more

Each end describes the entity it's next to. So AUTHOR ||--o{ BOOK : writes reads as “each author writes zero or more books, and each book is written by exactly one author.” Reading both directions out loud like that is the best way to check a relationship says what you mean.

```mermaid
erDiagram
  PERSON ||--o| PASSPORT : holds
  COUNTRY ||--|{ CITY : contains
  STUDENT }o--o{ COURSE : "enrols in"
```

The last line is a many-to-many relationship: a student takes many courses, and a course has many students. A database stores that through a third table, and you can draw it that way too, as in the complete example below.

Solid and dashed lines

Two dashes, --, draw a solid line, and two dots, .., a dashed one. A solid line is an identifying relationship: the child can't be identified without its parent, as an order line only makes sense as part of its order. A dashed line is non-identifying: the two are related, but each stands on its own, as a product exists whether or not anyone has ordered it.

```mermaid
erDiagram
  ORDER ||--|{ ORDER_LINE : contains
  PRODUCT ||..o{ ORDER_LINE : "is ordered as"
  CUSTOMER |o..o{ ORDER : places
```

If the distinction doesn't matter for your diagram, use solid lines throughout. Most readers look at the line ends first.

Attributes and keys

To list an entity's columns, write its name and the attributes in curly braces, one per line: the type first, then the name. After the name you can add a key, PK for a primary key, FK for a foreign key or UK for a unique key, and a comment in quotes.

```mermaid
erDiagram
  CUSTOMER ||--o{ INVOICE : receives
  CUSTOMER {
    int id PK
    string email UK "Used to sign in"
    string name
    date joined_on
  }
  INVOICE {
    int id PK
    int customer_id FK
    decimal total
    string status "draft, sent or paid"
  }
```

A column that is both a primary and a foreign key lists both, separated by a comma: int order_id PK, FK. The types are only labels, so use whatever your database calls them, such as varchar, uuid or timestamp.

A complete example

Here is a small school database that puts it all together. The many-to-many relationship between students and courses goes through an ENROLMENT table, which also holds the grade, since a grade belongs to one student in one course.

```mermaid
erDiagram
  STUDENT ||--o{ ENROLMENT : has
  COURSE ||--o{ ENROLMENT : has
  TEACHER ||--o{ COURSE : teaches
  STUDENT {
    int id PK
    string name
    int year
  }
  COURSE {
    string code PK
    string title
    int teacher_id FK
  }
  ENROLMENT {
    int student_id PK, FK
    string course_code PK, FK
    string grade
  }
  TEACHER {
    int id PK
    string name
  }
```

Common mistakes

When an ER diagram has a mistake, the preview shows its code with the error above it, including the line Mermaid stopped at. These are the usual causes:

ER diagrams in a PDF

md2pdf.me draws each diagram as a vector graphic, so the boxes and text stay sharp however far you zoom into the PDF, and a diagram is never split across two pages. A diagram wider than the page's text is shrunk to fit it, and so is one taller than most of a page. Entities with long attribute lists take up a lot of room, so a large schema can end up with small text.

For a big database, draw one diagram per area, such as billing, accounts or content, and show full attribute lists only where they matter. An overview diagram with entity names alone shows the overall shape before the detail.

A diagram's colors come from the style you choose. Classic, Academic and Résumé draw it in shades of grey for printing, Modern and Report in their blue accent, Worksheet in its own blue and purple, and GitHub in the same colors as github.com. The themed styles use their own colors: oxblood for Gothic, rust and navy for Sandwake, forest green for Elvish, teal and magenta for Cyberpunk, black and oxblood on newsprint grey for Newspaper, and ink and vermilion for Fiendish.

Other diagram types

Back to the Mermaid cheat sheet