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

Steps

  1. Paste the MySQL DDL

    Load the example. Backticks, ENGINE=InnoDB and UNIQUE KEY lines are read like any other SQL.

  2. 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.

  3. Spot the optional link

    comments.user_id can be NULL (guest comments, or ON DELETE SET NULL), so the users end of that line shows a circle: zero or one user.

  4. 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.

Try it now

Common mistakes

Using SHOW TABLES output
That only lists names. Use SHOW CREATE TABLE name or mysqldump --no-data to 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: