ERD Generator from SQL

Paste CREATE TABLE statements and get an interactive entity relationship diagram with keys, foreign key arrows and 1:1 or N:1 labels. Export as PNG or JPEG.

The interactive tool loads here when JavaScript runs.

About this tool

What is the ERD Generator?

The ERD Generator reads SQL DDL and draws an entity relationship diagram. Each CREATE TABLE becomes a box listing its columns and types, with primary key columns highlighted in yellow and foreign key columns in blue. Every foreign key, whether written inline (REFERENCES users(id)), as a table constraint (FOREIGN KEY ... REFERENCES ...), or in a later ALTER TABLE ... ADD FOREIGN KEY, becomes an arrow from the child table to the parent.

Arrows are labelled with the foreign key column and its cardinality: N:1 for an ordinary foreign key, and 1:1 when the foreign key column is also unique or is the whole primary key. Tables can be dragged to tidy the layout, and the diagram can be exported as PNG or JPEG.

The parser handles MySQL backticks, PostgreSQL and SQL Server style double quotes and square brackets, schema prefixes like public.users, IF NOT EXISTS and composite primary keys. It is not a full SQL parser: views, partitions and exotic syntax are skipped, and a foreign key to a table you did not paste is ignored. Nothing leaves your browser.

  • Reads inline, table-level and ALTER TABLE foreign keys
  • Composite primary keys, quoted identifiers, schema prefixes and IF NOT EXISTS
  • 1:1 and N:1 labels derived from unique and primary key constraints
  • Draggable, zoomable diagram
  • PNG and JPEG export
  • Runs in your browser
Read the guide: Database Schema Design and ER Diagrams: Keys, Relationships and Normal Forms

Updated

How to use the ERD Generator

  1. Paste one or more CREATE TABLE statements into SQL Schema Input, or click Load Sample for a users, posts, categories and post_categories schema.
  2. Click Generate ERD.
  3. Drag tables to rearrange them, and use the zoom controls in the corner to fit the diagram.
  4. Use Export to save the diagram as PNG or JPEG for a design doc or wiki page.

Worked examples

A junction table

Input

CREATE TABLE post_categories (
  post_id INT NOT NULL,
  category_id INT NOT NULL,
  PRIMARY KEY (post_id, category_id),
  FOREIGN KEY (post_id) REFERENCES posts(id),
  FOREIGN KEY (category_id) REFERENCES categories(id)
);

Output

post_categories box with both columns marked as key columns, and two arrows: post_id N:1 to posts, category_id N:1 to categories.

Neither foreign key is unique on its own (only the pair is), so both arrows are N:1. Together they express the many-to-many relationship between posts and categories.

A one-to-one profile table

Input

CREATE TABLE profiles (user_id INT PRIMARY KEY REFERENCES users(id), bio TEXT);

Output

Arrow from profiles to users labelled user_id 1:1.

user_id is the entire primary key, so each user can have at most one profile row.

Background and insight

Peter Chen introduced the entity-relationship model in a 1976 paper, drawing entities as rectangles and relationships as diamonds. Most diagrams you see today use a simpler style closer to the physical schema: one box per table, one line per foreign key, and cardinality marked at the ends of the line, often with crow's foot symbols where the crow's foot sits on the many side.

Cardinality in a real schema is decided by constraints, not by naming. posts.user_id REFERENCES users(id) is many-to-one because nothing stops two posts pointing at the same user. Add UNIQUE to user_id and it becomes one-to-one. Many-to-many does not exist as a single constraint at all: it shows up as a junction table with two N:1 arrows, such as post_categories pointing at both posts and categories.

Generating the diagram from DDL rather than drawing it by hand keeps it honest. The diagram reflects what the database will enforce, and you can regenerate it from a migration or from pg_dump --schema-only or mysqldump --no-data whenever the schema changes.

Common use cases

  • Onboarding onto a codebase

    Dump the schema and see how the main tables connect before reading the models.

  • Design documents

    Export a diagram of the tables a feature adds and drop it into the design doc or RFC.

  • Reviewing a migration

    Paste the new DDL and check that every foreign key points where the author intended.

  • Checking a generated schema

    Visualise the SQL from the Schema Designer to catch a missing relationship.

Tips and limitations

  • Get the DDL from a live database with pg_dump --schema-only, mysqldump --no-data, or sqlite3 db .schema.
  • If an arrow is missing, check that the referenced table is also in the input; foreign keys to tables you did not paste are skipped.
  • Large schemas are easier to read in groups. Paste one bounded area (billing, auth) at a time.
  • Export after arranging the boxes: the image captures the current layout.
  • Composite foreign keys are drawn as one arrow per column pair.

Sources and further reading

  1. The entity-relationship model: toward a unified view of data, ACM Transactions on Database Systems. Chen's 1976 paper that introduced ER diagrams
  2. A relational model of data for large shared data banks, Communications of the ACM. Codd, 1970
  3. Constraints, PostgreSQL Global Development Group. Foreign keys and UNIQUE, which set cardinality
  4. pg_dump, PostgreSQL Global Development Group. --schema-only output to generate a diagram from

Frequently asked questions

How do I generate an ER diagram from an existing database?

Export the schema as SQL and paste it in. Use pg_dump --schema-only for PostgreSQL, mysqldump --no-data for MySQL or MariaDB, and sqlite3 your.db .schema for SQLite. Then click Generate ERD.

How does the ERD generator decide between 1:1 and N:1?

From constraints. A foreign key column that is UNIQUE, or that forms the entire primary key, can only reference each parent row once, so it is labelled 1:1. Any other foreign key allows many child rows per parent and is labelled N:1.

Why don't I see many-to-many relationships?

Because SQL has no many-to-many constraint. It is modelled with a junction table holding two foreign keys, and the diagram shows that table with an N:1 arrow to each side. Reading the two arrows together gives you the many-to-many.

Which SQL dialects does the ERD parser understand?

The common CREATE TABLE shapes from MySQL, MariaDB, PostgreSQL, SQLite and SQL Server: backtick, double-quote and bracket quoting, schema-qualified names, IF NOT EXISTS, inline and table-level keys, and ALTER TABLE ADD FOREIGN KEY. Views, partitions and stored procedures are ignored.

Is my schema uploaded when I generate a diagram?

No. Parsing, layout and image export all happen in your browser tab, so table and column names stay on your machine.

Is there an in-depth guide to the ERD Generator?

Yes. “Database Schema Design and ER Diagrams: Keys, Relationships and Normal Forms” covers the background, how it works, common pitfalls and worked examples. Read the full guide.