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
| SQL | Mermaid |
|---|---|
| PRIMARY KEY | PK |
| REFERENCES / FOREIGN KEY | FK + a relationship line |
| UNIQUE | UK |
| 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 FKs | Two 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.