Convert a SQL schema into an ER diagram.
Foreign keys already encode the relationships and their cardinality. The diagram renders what the schema is enforcing anyway.
Read from constraints, not guessed
A foreign key gives the relationship and its direction; a unique constraint on that key gives one-to-one rather than one-to-many; NOT NULL gives mandatory rather than optional. All of that is stated in the DDL, so the resulting cardinality is read rather than inferred, which is the difference between a diagram you trust and one you check.
Join tables become relationships
A table whose primary key is composed entirely of foreign keys is a join table. Drawing it as an entity produces a picture of your storage rather than your model, so it is recognised and drawn as a single many-to-many relationship instead. That one transformation is most of what makes a generated ER diagram match the model in your head.
Migrations work too
Most application repositories hold migrations rather than a checked-in schema, so the current shape is reconstructed by applying them in order. That works and takes longer on a long history, which is a decent argument for keeping a generated schema dump in the repository regardless.
Fidelity
| Comes through | Does not | |
|---|---|---|
| Tables | Every table as an entity | |
| Columns and types | Yes | Vendor-specific types are approximated |
| Foreign keys | Relationships with direction | |
| Cardinality | Read from unique and null constraints | |
| Join tables | Collapsed into many-to-many | |
| Implicit relationships | A column with no constraint is invisible | |
| Check constraints | As notes | Not as diagram semantics |
| Triggers and procedures | Not represented |
The prompt
Questions
Related
- Convert Code to a DiagramThe general case: point it at code and get a diagram of what is there rather than what you remember building.
- Convert a Diagram into CodeThe direction almost nothing else does: the diagram stops being documentation and becomes the instruction.
- Convert Terraform to an Architecture DiagramTerraform records dependencies explicitly through references, so the edges are read rather than inferred. It is one of the most reliable sources there is.
- Convert Mermaid to ExcalidrawUseful when a Mermaid diagram has outgrown what Mermaid's layout can render legibly.