How to generate an ER diagram from SQL
- 1Pick the dialect your SQL was written for: PostgreSQL, MySQL or SQL Server.
- 2Paste the CREATE TABLE statements or a schema-only dump into the editor.
- 3Review the diagram: foreign keys become lines between the related tables.
- 4Drag tables to adjust the layout, or re-run auto-layout.
- 5Export the diagram as PNG or SVG, or convert the SQL to DBML or a Mermaid erDiagram.
What the diagram reads from your SQL
Every CREATE TABLE becomes a box listing its columns and types. PRIMARY KEY, NOT NULL and UNIQUE constraints are shown as markers on the columns, and enum types become their own boxes.
Relationship lines come only from declared foreign keys: an inline REFERENCES clause, a FOREIGN KEY constraint in the table, or a later ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY, which is how most dump tools write them. A column named user_id without a foreign key constraint is just a column, so if lines are missing, check that the keys are actually declared.
Getting a schema-only dump
You only need the structure, not the data. PostgreSQL: pg_dump --schema-only --no-owner --no-privileges mydb > schema.sql (add -n myschema to limit it to one schema). MySQL and MariaDB: mysqldump --no-data --skip-triggers mydb > schema.sql. SQL Server: in SSMS, right-click the database, choose Tasks > Generate Scripts, pick the tables, and under Advanced set Types of data to script to Schema only.
Dumps can include functions, triggers, views, sequences and grants. If one of those stops the parser, delete it; the diagram needs only the table definitions and the statements that add primary and foreign keys.
Crow's foot notation explained
Each relationship line connects a foreign key column to the key it points at. The end of the line shows how many rows can sit on that side: a single bar means one, and the three-pronged crow's foot means many. A line from posts.user_id to users.id with the crow's foot at posts reads as "one user has many posts, each post belongs to one user".
In full crow's foot notation a circle next to the end means zero is allowed (optional) and a second bar means at least one is required (mandatory). In SQL terms, a nullable foreign key column is the optional case and a NOT NULL foreign key is the mandatory one, so check the NOT NULL markers on the column when you need to know whether a row can exist without its parent.
One-to-one relationships have a bar at both ends and usually come from a foreign key that is also UNIQUE or the primary key. Many-to-many relationships cannot be stored directly in a relational database; they need a junction table, such as post_tags with post_id and tag_id, which turns them into two one-to-many lines.