- Databases
- SQL
- ER diagrams
Get an ER diagram out of your SQL schema, then make it readable
Dump the schema, generate the diagram, then cut it down: drop audit columns, collapse join tables and split by domain so people can read it.
By Abdessamad Bettal4 min read
Nobody draws an entity-relationship diagram for a forty-table schema by hand twice. The first time, it takes an afternoon. By the time anyone needs it again, three migrations have landed and the drawing is wrong.
The better workflow is to generate the diagram from the schema itself, then spend your time on the part a generator cannot do: deciding what to leave out so people can actually read it.
Step 1: get the schema as plain SQL
You want CREATE TABLE statements with the foreign keys included. Every major database can produce them without touching data.
PostgreSQL:
pg_dump --schema-only --no-owner --no-privileges mydb > schema.sql
MySQL or MariaDB:
mysqldump --no-data --skip-comments mydb > schema.sql
SQLite:
sqlite3 app.db .schema > schema.sql
If your project uses an ORM, its migration folder or a "dump schema" command gives you the same thing. Rails keeps it in db/structure.sql when configured to, and most migration tools can print the DDL they would run.
The dump will contain things that are not tables: indexes, sequences, grants, triggers, functions. You do not need to strip them out before pasting into the SQL to ER diagram generator; it reads the table definitions and foreign keys and skips the rest.
Step 2: generate, then look for the shape of the domain
With a real schema pasted in, the first diagram is usually too much. That is fine. Before editing anything, look at it for a minute and find the clusters: groups of tables that point at each other a lot and at the rest of the schema rarely.
In a typical e-commerce schema you will see them quickly:
- Customers and identity:
users,addresses,sessions,password_resets - Catalog:
products,variants,categories,product_categories - Ordering:
orders,order_items,payments,refunds - Fulfilment:
shipments,shipment_items,warehouses
Those clusters are your real diagram boundaries. One diagram per cluster, plus one small overview showing how the clusters connect, is far more useful than one diagram with everything.
Step 3: cut columns that do not explain anything
An ER diagram exists to show structure: what the entities are and how they relate. Most columns do not help with that. These are almost always safe to remove from the drawing:
created_at,updated_at,deleted_atand similar audit columnscreated_byandupdated_by, unless auditing is the point of the diagram- Internal flags like
is_migratedorlegacy_id - Long lists of descriptive fields: keep
nameandemail, dropphone_2andfax
Keep primary keys, foreign keys, and the handful of columns that tell you what the entity is. A products table reduced to id, sku, name and price_cents says everything a reader needs at the structural level. The full column list is one \d products away when someone needs it.
If you prefer to cut before generating, delete the lines from the SQL. It is often faster than editing shapes afterwards.
Step 4: collapse pure join tables
A table like this exists only to implement a many-to-many relationship:
CREATE TABLE product_categories (
product_id INTEGER NOT NULL REFERENCES products(id),
category_id INTEGER NOT NULL REFERENCES categories(id),
PRIMARY KEY (product_id, category_id)
);
On a conceptual diagram you can replace it with a single many-to-many line between products and categories. On a physical diagram meant for engineers writing queries, keep it, because they will need to join through it.
Decide which kind of diagram you are making and apply the rule consistently. Mixing the two, with some join tables collapsed and others not, is confusing.
The exception is a join table that carries its own data. order_items connects orders and products, but it also has quantity and unit_price_cents. That makes it a real entity, and it stays.
Reading the relationship lines
Generated diagrams draw each foreign key as an arrow from the referencing table to the referenced one. When you redraw or annotate, it helps to think in terms of cardinality, which is what crow's foot notation shows:
| Marker | Meaning |
|---|---|
| A single bar | Exactly one |
| A circle and a bar | Zero or one |
| A crow's foot and a bar | One or more |
| A crow's foot and a circle | Zero or more |
The foreign key tells you the "many" side: orders.user_id means one user has many orders. Whether the foreign key column is nullable tells you whether the "one" side is optional. A nullable orders.coupon_id means an order has zero or one coupon.
Step 5: keep the generated version reproducible
Save the SQL you generated from, trimmed to the tables and columns you kept, next to the diagram. When the schema changes, you regenerate from an updated dump, apply the same cuts and compare. That is a five-minute job, not an afternoon.
If you want a worked example to compare your own result against, the e-commerce ER diagram template covers customers, orders, line items and products with the noise already removed. And if your data lives in documents rather than tables, the JSON to diagram tool does the equivalent job for nested JSON, showing objects and arrays as connected cards.