Creating an ER Diagram From an Existing Database
For developers and DBAs handed a database they did not build, who need a diagram of its tables and of how they relate.
On this page
DbSchema connects to an existing database, reads its tables and foreign keys, and draws them as an ER diagram. DbSchema keeps the tables in a model, its own copy of the schema, and only reads the database. The free Community Edition draws the diagram for every database that DbSchema connects to.
- In DbSchema, choose File → New Model, keep Connect to Database selected, and click your database. Press the button named after it, such as Connect to PostgreSQL.
- In the connection dialog, choose Standard as the Method, and fill in Server Host, Port, Database User, Password and Database. If the database holds real data, also tick Read Only Connection on the Access and Model tab. Press Test Connection, then Connect.
- In Reverse Engineer the Database, tick the schemas to import and press Load.
DbSchema draws the imported tables on a diagram, with a line for each foreign key. On the first connection to a kind of database, DbSchema also downloads its JDBC driver. Read Only Connection matters because a change that you confirm on the diagram runs in the database at once while DbSchema is connected. On a read-only connection, DbSchema refuses such a change. The connection documentation covers SSL, SSH tunnels and the other connection methods.
For one engine step by step, see how to generate a PostgreSQL ER diagram, MySQL ER diagrams or ER diagrams for SQLite. The symbols on the lines are explained in Entity Relationship Diagram (ERD).
What the diagram shows
Every database describes its own tables in system tables, called the catalog. DbSchema draws what the catalog holds: tables, columns, data types, primary and unique keys, indexes, comments, and the foreign keys that were declared as constraints. A relationship that only the application code knows about gets no line.
In the three tables below, orders declares a foreign key to customers. payments.order_id holds order numbers, but payments declares no foreign key:
CREATE TABLE customers (
customer_id int PRIMARY KEY,
name varchar(50)
);
CREATE TABLE orders (
order_id int PRIMARY KEY,
customer_id int,
amount int,
FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
);
CREATE TABLE payments (
payment_id int PRIMARY KEY,
order_id int,
amount int
);
INSERT INTO customers VALUES (1, 'Anna'), (2, 'Ben');
INSERT INTO orders VALUES (10, 1, 100), (11, 2, 250);
INSERT INTO payments VALUES (100, 10, 100), (101, 11, 250), (102, 12, 80);
In PostgreSQL and MySQL, the information_schema views list the declared foreign keys:
SELECT table_name
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
AND table_name IN ('customers', 'orders', 'payments');
Only orders has one:
| table_name |
|---|
| orders |
DbSchema reads the same catalog, so the diagram has a line from orders to customers, and payments stands alone. SQLite has no information_schema, and PRAGMA foreign_key_list gives the same answer there.
Why a relationship has no line
When a line you expect is missing, look for one of these causes:
- Nobody declared the constraint, as with
paymentsabove. The application code keeps the relationship. - The table uses MySQL's MyISAM storage engine. MySQL 9.1 checks foreign keys only on InnoDB and NDB Cluster tables. On a MyISAM table, MySQL 9.1 accepted a
FOREIGN KEYclause without a warning and recorded no constraint. - The login lacks privileges. A MySQL login sees only the tables that the login holds a privilege on. Oracle's ALL_CONSTRAINTS view describes only the tables that the current user can access.
For the last cause, connect with a login that may read every schema you import. The first two causes are shown with examples in DBeaver ER Diagram Not Showing Relationships?
Add the missing relationships as virtual foreign keys
A virtual foreign key is a relationship that exists only in the DbSchema model. DbSchema draws it as a line on the diagram.
- On the diagram, drag
payments.order_idontoorders.order_id. The New Foreign Key dialog opens. - Tick Virtual.
- Press OK.
DbSchema draws the virtual foreign key in a different color from the declared foreign keys.
Without Virtual ticked, DbSchema creates the foreign key in the database on a live connection. The virtual foreign key stays in the model while DbSchema is open. To keep it, save the model to a file, which needs DbSchema Pro.
For a schema with many tables, Model → Find Virtual Foreign Keys suggests virtual foreign keys between columns with matching names. Reverse Engineering a SQLite Database shows other ways to find relationships that nobody declared.
Check each relationship against the rows
A virtual foreign key records what you believe about the data. Before you trust it, look for payments whose order does not exist. Run this query in the DbSchema SQL Editor, or in any client:
SELECT p.payment_id, p.order_id
FROM payments p
LEFT JOIN orders o ON o.order_id = p.order_id
WHERE o.order_id IS NULL;
One payment points at an order that does not exist:
| payment_id | order_id |
|---|---|
| 102 | 12 |
The orders table has only orders 10 and 11. If a few rows come back, the relationship holds, and those rows hold bad data. If most rows come back, the relationship is probably wrong, so delete the virtual foreign key.
Run the same check for a declared foreign key in SQLite. SQLite enforces foreign keys only on a connection that turns enforcement on, and SQLite 3.50 accepted an order for a customer who did not exist.
Split a large schema into several diagrams
A schema of a few hundred tables is unreadable on one diagram. In DbSchema, one model holds as many diagrams as you need, and a table can show on several of them.
- Choose Diagram → New Diagram.
- Name the diagram, such as Billing, and add the tables of that part of the application.
- If the tables overlap, choose Diagram → Auto Arrange Tables, then Arrange Only. Edit → Undo cannot bring back the positions that the tables had before.
The diagrams change only the model, never the database.
To diagram your own database, download DbSchema and connect it to the database over a read-only connection. Then add a virtual foreign key for each relationship that the database leaves out. Reverse engineering, the diagrams, virtual foreign keys and the SQL Editor are in the free Community Edition. Saving the model to a file needs DbSchema Pro, which the 15-day trial includes.
Turn the database you inherited into an ER diagram
DbSchema connects to the database, reverse-engineers the schema into an interactive diagram and lets you add the relationships nobody declared, free Community Edition included.