How to Generate an ER Diagram from SQL DDL: Step-by-Step Tutorial
This tutorial walks through generating an ER diagram from SQL CREATE TABLE statements using the DDL to ER Diagram tool. You will paste DDL, generate the diagram, read the relationship lines, rearrange tables, and export to SVG, PNG, and Mermaid.
๐ง Open the tool to follow along:
Open DDL to ER DiagramTable of Contents
1 Prepare Your DDL
You need one or more CREATE TABLE statements. These can come from your database client's "Script Table As" feature, a migration file, or a schema dump. Here is a simple example with two related tables:
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Email VARCHAR(255)
);
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT NOT NULL,
OrderDate DATE,
Total DECIMAL(10,2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Tip: Include all related tables in a single paste. If you only paste the Orders table without Customers, the tool will flag a warning that the referenced table is missing.
2 Paste and Generate
Open the tool and paste your DDL into the left-hand SQL editor. You can also click ๐ Load .sql to import a file directly from your filesystem. Click โถ Generate Diagram.
The status strip below the editor will show: โ Parsed 2 tables, 1 relationship. If there are issues, they will appear as warnings or errors โ check the Warnings tab on the right panel for details.
3 Read the Diagram
The diagram panel shows each table as a dark card with its columns listed inside. Key columns are color-coded:
- ๐ Blue text โ Primary key columns
- ๐ Purple text โ Foreign key columns
- Grey text โ Regular columns
Relationship lines connect tables through their foreign keys. The crow's foot symbol (a three-pronged fork) on one end indicates the "many" side of the relationship โ the table that contains the foreign key. The single-line end indicates the "one" side โ the table being referenced.
Hover over any relationship line to see a tooltip showing the exact column mapping, such as Orders.CustomerID โ Customers.CustomerID.
4 Rearrange and Focus
The diagram is interactive. You can:
- Drag tables โ click and drag any table card to reposition it. The relationship lines update automatically.
- Zoom โ use the scroll wheel, or click the ๏ผ / โ buttons in the control bar above the diagram.
- Fit to screen โ click โ Fit to auto-zoom the diagram so all tables are visible.
- Toggle data types โ click the Types button to show or hide the data type annotations on each column. Hiding types gives a cleaner view focused on names and keys.
- Keys-only mode โ click Keys Only to hide all non-key columns. This is useful for large schemas where you only care about the primary key / foreign key structure.
5 Review the Mermaid Output
Click the Mermaid tab to see the generated Mermaid ER syntax. This is a text representation of the same diagram that can be rendered in GitHub Markdown, Notion, Obsidian, and other platforms that support Mermaid.
To use it in a GitHub README, wrap the text in a code fence:
```mermaid
erDiagram
Customers {
INT CustomerID PK
NVARCHAR(100) Name
VARCHAR(255) Email
}
Orders {
INT OrderID PK
INT CustomerID FK
DATE OrderDate
DECIMAL(10,2) Total
}
Customers ||--|{ Orders : "CustomerID"
```
Click ๐ Copy Mermaid to copy the syntax to your clipboard.
6 Export the Diagram
The toolbar above the diagram provides two image export options:
- โฌ SVG โ downloads a scalable vector file. Best for documentation, wikis, and presentations. The SVG stays crisp at any zoom level and can be further edited in tools like Figma or Inkscape.
- โฌ PNG โ downloads a raster image at 2ร resolution. Best for embedding in Slack, email, Confluence, or README files where a simple
<img>tag is easier than inline SVG.
7 Check the Warnings Tab
The Warnings tab shows any issues the parser encountered:
- "FK references missing table" โ a FOREIGN KEY points to a table that was not found in the input. Add the missing table's CREATE TABLE statement.
- "appears to be a many-to-many join table" โ the parser detected a table with a composite PK where both columns are FKs to different tables. This is informational, not an error.
- "No CREATE TABLE statements found" โ the input does not contain any parseable DDL. Check that you pasted SQL CREATE TABLE statements and not just SELECT queries or other SQL.
Common Pitfalls
- Missing referenced tables. The most common warning. If you export only a subset of tables from your database, FK references to tables outside that subset will show as missing. Solution: include all tables in the schema, or accept the warnings for known external references.
- ALTER TABLE foreign keys. Foreign keys added via
ALTER TABLE ADD CONSTRAINTafter theCREATE TABLEblock are not currently detected. Move these constraints inside theCREATE TABLEbody for the parser to pick them up. - Overly dense diagrams. For schemas with 30+ tables, the automatic grid layout may place tables far apart from their related tables. Use drag-and-drop to group related tables together, or use Keys Only mode to reduce visual noise.
- Schema prefixes in FKs. If your FK uses
REFERENCES dbo.Customers(CustomerID)but the table was created as justCREATE TABLE Customers, the schema prefix is stripped during parsing and the match will still work.
๐ง Try it yourself โ paste any DDL and get an ER diagram in seconds.
Open DDL to ER Diagram