Skip to content
MermaidViewer

Convert a SQL Schema into a Mermaid ER Diagram

Turn CREATE TABLE statements into a Mermaid ER diagram by hand or with AI. Map columns, primary and foreign keys and cardinality correctly, with full examples.

6 min readUpdated

Documenting an existing database is one of the best uses of Mermaid ER diagrams. Here's how to translate SQL into erDiagram syntax — and how to let AI do it in seconds.

Starting point: SQL

sql
CREATE TABLE authors (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE books (
  id SERIAL PRIMARY KEY,
  author_id INT NOT NULL REFERENCES authors(id),
  isbn TEXT UNIQUE,
  title TEXT NOT NULL
);

The Mermaid version

mermaid
erDiagram
    AUTHOR ||--o{ BOOK : writes
    AUTHOR {
      int id PK
      text name
    }
    BOOK {
      int id PK
      int author_id FK
      text isbn UK
      text title
    }

Mapping rules

SQLMermaid
PRIMARY KEYPK
REFERENCES / FOREIGN KEYFK + a relationship line
UNIQUEUK
NOT NULL foreign key|| on the parent side (exactly one)
Nullable foreign key|o on the parent side (zero or one)
Join table with two FKsTwo one-to-many relationships

Let AI convert it

In the editor, open AI → Generate, paste your SQL and ask: "Convert this schema into a Mermaid ER diagram with keys and relationships." Review the result and use Edit to tweak names or remove noise like audit columns.