DiagramDB / Mermaid ER Diagram

Mermaid ER Diagram Generator

Paste a SQL or DBML schema and get Mermaid erDiagram code, ready for a README, a wiki page or a pull request description.

Mermaid
erDiagram
  users {
    SERIAL id PK
    VARCHAR(255) email UK
    VARCHAR(80) display_name
    TIMESTAMP created_at
  }
  posts {
    SERIAL id PK
    INTEGER author_id FK
    VARCHAR(200) title
    TEXT body
    TIMESTAMP published_at
  }
  comments {
    SERIAL id PK
    INTEGER post_id FK
    INTEGER user_id FK
    TEXT body
  }
  tags {
    SERIAL id PK
    VARCHAR(50) name UK
  }
  post_tags {
    INTEGER post_id PK, FK
    INTEGER tag_id PK, FK
  }
  users ||--o{ posts : "author_id"
  posts ||--o{ comments : "post_id"
  users |o--o{ comments : "user_id"
  posts ||--o{ post_tags : "post_id"
  tags ||--o{ post_tags : "tag_id"

Preview

The same schema drawn with crow’s-foot notation, as Mermaid will lay it out in spirit (Mermaid chooses its own positions).

Mermaid erDiagram syntax

An ER diagram starts with erDiagram. Each entity lists attributes as type name, optionally followed by PK, FK or UK and a quoted comment. Relationships are written ENTITY_A cardinality ENTITY_B : "label".

MermaidReads as (left to right)
||--||exactly one to exactly one
||--o{exactly one to zero or more
||--|{exactly one to one or more
|o--o{zero or one to zero or more
}o--o{zero or more to zero or more
||..o{same, but a dotted (non-identifying) line

The outer character of each end is the maximum (| one, {/} many), the inner is the minimum (| one, o zero). Two dashes draw a solid, identifying relationship; two dots draw a dashed, non-identifying one. These are the same crow’s-foot symbols explained on the crow’s foot notation page.

The generator writes the parent table on the left: users ||--o{ posts says one user has zero or more posts. A nullable foreign key makes the parent end |o (zero or one) instead of ||.

Questions

Where do Mermaid ER diagrams render?

GitHub and GitLab render ```mermaid code blocks in Markdown files, issues and pull requests. Notion, Obsidian, Confluence (via apps), Docusaurus, MkDocs Material and many other tools support Mermaid too.

How do I paste the output into GitHub?

Wrap it in a fenced code block with the language set to mermaid: three backticks followed by mermaid, the code, then three backticks. Use the “Copy for Markdown” button to get it already wrapped.

Why are some types changed in the Mermaid output?

Mermaid attribute types cannot contain spaces or commas, so a type like DECIMAL(10, 2) becomes DECIMAL(10_2) and “double precision” becomes double_precision. The diagram is for documentation; your real SQL is unchanged.

What do PK, FK and UK mean in Mermaid?

They are Mermaid’s attribute key markers: primary key, foreign key and unique key. The generator adds them from your PRIMARY KEY, REFERENCES/FOREIGN KEY and UNIQUE constraints.

Related tools