Generate

SQL to ER Diagram — Draw a Schema from CREATE TABLE

Paste CREATE TABLE statements and get an entity-relationship diagram, plus the Mermaid source to reuse.

Runs entirely in your browser — nothing you paste is uploaded or stored.

Preview100%

Load the example, or paste CREATE TABLE statements to see the entity-relationship diagram here.

What is sql to er diagram?

An entity-relationship (ER) diagram shows a database's tables as boxes, their columns inside, and the foreign keys between them as lines. Most teams need one at some point — for onboarding, a design review or documentation — and few keep one up to date by hand, because the schema already says everything the diagram needs. This tool reads SQL DDL, the CREATE TABLE and ALTER TABLE statements that define a schema, works out the primary keys, foreign keys and unique constraints, and draws the diagram for you. It understands the common dialects — PostgreSQL, MySQL, SQLite and SQL Server — including quoted identifiers, schema prefixes, composite keys and foreign keys added later with ALTER TABLE, and it skips anything it does not recognise instead of failing, so you can paste a schema-only dump as it is. Each relationship shows its cardinality: a NOT NULL foreign key draws a mandatory parent, a nullable one an optional parent, and a foreign key that is also unique draws one-to-one. The diagram is produced as Mermaid erDiagram source, which you can copy into a README or wiki that renders Mermaid, as well as exporting the picture itself.

When to use it

  • Documenting an existing database for a README or onboarding guide by pasting the output of pg_dump --schema-only or mysqldump --no-data.
  • Reviewing a migration or a new feature's tables as a diagram before the change is merged.
  • Checking that foreign keys and nullability say what you intended, since the cardinality symbols make a missing NOT NULL or UNIQUE obvious.
  • Generating Mermaid ER source from SQL to commit alongside the code, so the diagram lives in version control and renders on GitHub or GitLab.
  • Exploring an unfamiliar schema quickly by switching to key columns only, which shows how the tables connect without the noise of every column.

How to use this tool

  1. Click Load example, drop a .sql file onto the input, or paste your CREATE TABLE statements into the left pane.
  2. The diagram draws as you type, and the pane header counts the tables and relationships found; anything that is not a table definition is ignored.
  3. Choose how much of each table to draw with the Columns list — all columns, only key columns, or just the table names for a large schema.
  4. Scroll to zoom and drag to pan the diagram, or use the fullscreen viewer for a big schema.
  5. Download the diagram as SVG, PNG or PDF, or copy the generated Mermaid source to paste into documentation.
  6. Copy a share link to send someone the exact schema and view settings.

Example

A small blog schema with one-to-many and many-to-many relationships, the built-in example.

Input

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL UNIQUE,
  name VARCHAR(100),
  created_at TIMESTAMP NOT NULL DEFAULT now()
);

CREATE TABLE posts (
  id SERIAL PRIMARY KEY,
  author_id INTEGER NOT NULL REFERENCES users(id),
  title VARCHAR(200) NOT NULL,
  body TEXT,
  published_at TIMESTAMP
);

CREATE TABLE comments (
  id SERIAL PRIMARY KEY,
  post_id INTEGER NOT NULL,
  user_id INTEGER,
  body TEXT NOT NULL,
  FOREIGN KEY (post_id) REFERENCES posts (id),
  FOREIGN KEY (user_id) REFERENCES users (id)
);

CREATE TABLE tags (
  id SERIAL PRIMARY KEY,
  label VARCHAR(50) NOT NULL UNIQUE
);

CREATE TABLE post_tags (
  post_id INTEGER NOT NULL REFERENCES posts(id),
  tag_id INTEGER NOT NULL REFERENCES tags(id),
  PRIMARY KEY (post_id, tag_id)
);

Output

Five tables — users, posts, comments, tags and post_tags — with five relationships. users to posts is mandatory (author_id is NOT NULL), users to comments is optional (user_id is nullable), and post_tags carries a composite primary key made of two foreign keys, which shows the many-to-many link between posts and tags.

The generated Mermaid source begins with erDiagram and can be pasted into any page that renders Mermaid.

How the diagram is drawn

The SQL is turned into Mermaid erDiagram source, and that source is rendered by the same Mermaid library used by the Mermaid Diagram Generator. That is why you can copy the generated source and keep editing it there — to rename a relationship, add a note or change the theme — and why the picture and the code always agree.

What is deliberately not guessed

The tool only draws relationships that the SQL states. It does not infer a foreign key from a column named user_id, and it does not read indexes, views or triggers. A schema that relies on application-level relationships will show tables without lines between them; adding REFERENCES clauses, or the matching ALTER TABLE statements, is the way to make them appear.

Frequently asked questions

Which SQL dialects does it understand?

PostgreSQL, MySQL and MariaDB, SQLite and SQL Server syntax are all handled, including double-quoted, backtick and square-bracket identifiers, schema-qualified names such as public.users or [dbo].[Users], CREATE TABLE IF NOT EXISTS, temporary tables, multi-word types like timestamp with time zone, array types, and trailing options such as ENGINE=InnoDB. It reads the structure rather than validating the SQL, so a dialect-specific clause it does not know about is skipped, not reported as an error.

Does it need a database connection?

No. It only reads the text you paste, so there is nothing to connect to and no credentials involved. Export the schema with your database's own tool — pg_dump --schema-only, mysqldump --no-data, or the .schema command in the SQLite shell — and paste the result.

How does it decide between one-to-one and one-to-many?

A foreign key whose columns are also the table's primary key, or are covered by a UNIQUE constraint, can match at most one row, so it is drawn as one-to-one. Any other foreign key is one-to-many. On the parent side, a NOT NULL foreign key means every child must have a parent, drawn as exactly one; a nullable foreign key means the parent is optional, drawn as zero or one.

Why is a table drawn empty or a warning shown?

A foreign key points at a table that was not in the pasted SQL. The diagram still draws the relationship, with the missing table shown as an empty box, and a warning names it. Paste the missing CREATE TABLE statement, or ignore the warning if you meant to show only part of the schema.

My foreign keys are in separate ALTER TABLE statements — will they show?

Yes. Dumps from PostgreSQL and SQL Server usually add foreign keys at the end with ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY. Those are read in a second pass, so they appear in the diagram whatever order the statements come in. Foreign keys that are only implied by a column name, such as user_id with no REFERENCES clause, are not guessed, because a wrong guess is worse than a missing line.

Why are decimal(10,2) and varchar(MAX) shown without their parameters?

The diagram is rendered with Mermaid, whose column types cannot contain commas or arbitrary text. A single numeric parameter such as varchar(255) is kept; anything else is shortened to the base type. The full definition is still in your SQL.

Is there a size limit?

The input is limited to 200,000 characters, which is hundreds of tables. Very large schemas draw more readably with the Columns list set to key columns only or table names only. Paste only the CREATE TABLE and ALTER TABLE statements, not a dump that includes row data.

Is my schema uploaded anywhere?

No. Parsing and drawing both happen in your browser, and a share link stores the SQL inside the URL rather than on a server. Even so, treat share links like the schema itself and avoid posting links to confidential designs in public places.

Find these tools useful? A coffee helps keep them free and ad-light.

Buy me a coffee