ER Diagram Generator from SQL · Tutorial
How to document a MySQL schema with a diagram
6 min readbeginnerUpdated
MySQL dumps quote everything with backticks and often add foreign keys at the end with ALTER TABLE. You will diagram a small blog schema written that way and spot an optional relationship.
What you need
- The ER Diagram Generator
- MySQL CREATE TABLE statements, for example from
SHOW CREATE TABLE
Steps
Paste the MySQL DDL
Load the example. Backticks,
ENGINE=InnoDBandUNIQUE KEYlines are read like any other SQL.Check the ALTER TABLE keys
The foreign keys are added at the end with
ALTER TABLE … ADD CONSTRAINT. They still appear as lines: users to posts, posts to comments and users to comments.Spot the optional link
comments.user_idcan be NULL (guest comments, orON DELETE SET NULL), so the users end of that line shows a circle: zero or one user.Add it to the docs
Open Mermaid code, copy it, and paste it into your docs inside a mermaid code block.
```mermaid erDiagram users ||--o{ posts : "author_id" ... ```
Try it yourself
Opens the ER Diagram Generator from SQL with this tutorial's sample loaded.
Common mistakes
- Using SHOW TABLES output
- That only lists names. Use
SHOW CREATE TABLE nameormysqldump --no-datato get the full definitions. - Tables using MyISAM
- MyISAM tables accept foreign key syntax but don’t store the constraints, so dumps of them have no foreign keys to draw. Convert them to InnoDB to get real relationships.
Next steps
Go back to the ER Diagram Generator from SQL, or keep going with one of these: