Back to all posts

How to Create ER Diagrams for SQLite with a Free Tool

For developers holding a SQLite file who want to see its tables and the foreign keys between them as one picture.

On this page

Run .schema in the sqlite3 shell on a file with thirty tables, and it prints thirty CREATE TABLE statements, with the foreign keys inside their text. An ER diagram shows the same schema as a picture: every table a box, and every foreign key a line between two columns. DbSchema draws that diagram from the file, in the free Community Edition:

  1. Open DbSchema. On the Welcome Screen, in Start New, choose Connect to Database.
  2. Click SQLite, choose New Connection on the right, and press Connect.
  3. In the connection dialog, choose your .db file, press Test Connection, then Connect.
  4. In Reverse Engineer the Database, press Load.
  5. If the tables overlap, choose Diagram → Auto Arrange Tables and confirm.
The DbSchema Connect to SQLite dialog with the file music.db in the File field, and the Test Connection and Connect buttons

DbSchema downloads the SQLite JDBC driver the first time you connect. Connecting and importing read the file without changing it. DbSchema keeps the tables, columns, indexes and foreign keys that it read in a model of its own, apart from the SQLite file.

Try DbSchema on your own database

Free Community Edition plus a 15-day Architect trial. No signup. Windows, macOS and Linux.

Why a table has no line

Each line comes from a REFERENCES clause in the schema. Take three tables of a music library, where tracks.album_id was written without its clause:

CREATE TABLE artists (
  artist_id INTEGER PRIMARY KEY,
  name      TEXT NOT NULL
);

CREATE TABLE albums (
  album_id  INTEGER PRIMARY KEY,
  title     TEXT NOT NULL,
  artist_id INTEGER REFERENCES artists (artist_id)
);

CREATE TABLE tracks (
  track_id INTEGER PRIMARY KEY,
  name     TEXT NOT NULL,
  album_id INTEGER
);

DbSchema reads the foreign keys of each table with pragma_foreign_key_list. Join pragma_foreign_key_list to sqlite_master, the table that lists the schema's tables, and you get every line that the diagram will draw:

SELECT m.name AS child_table, f."from" AS child_column,
       f."table" AS parent_table, f."to" AS parent_column
FROM sqlite_master AS m
JOIN pragma_foreign_key_list(m.name) AS f
WHERE m.type = 'table'
ORDER BY m.name;

On SQLite 3.50.4 it returns one row:

child_tablechild_columnparent_tableparent_column
albumsartist_idartistsartist_id

albums points at artists, so the diagram joins those two boxes. tracks has no row, so its box stands alone, although its album_id holds album numbers.

DbSchema diagram of the SQLite tables tracks, albums and artists with their data types: a foreign key line from albums to artists, and no line to tracks

The diagram draws the foreign keys that the schema declares. SQLite checks them only when PRAGMA foreign_keys is on. The SQLite foreign key documentation says that this setting is off by default and has to be set on each connection. On a new connection, SQLite accepts this album, although the artists table holds no artist 7:

INSERT INTO albums (album_id, title, artist_id) VALUES (1, 'Blue Train', 7);

Add the missing line as a virtual foreign key

SQLite cannot add a foreign key to an existing table. SQLite's ALTER TABLE only renames a table, and adds, renames or drops a column. So this statement fails:

ALTER TABLE tracks ADD FOREIGN KEY (album_id) REFERENCES albums (album_id);

SQLite 3.50.4 refuses it with near "FOREIGN": syntax error. To add the clause, you create the table again with it and copy the rows over.

To see the relationship without touching the file, add a virtual foreign key in DbSchema:

  1. Drag tracks.album_id onto albums.album_id on the diagram.
  2. In the foreign key dialog that opens, tick Virtual.
  3. Press OK.

DbSchema draws the virtual foreign key in another color and never creates it in the database. The virtual foreign key lives in the model. To keep it for the next session, save the model to a file, which needs DbSchema Pro. The 15-day trial in every new installation includes Pro.

The same DbSchema diagram with a virtual foreign key line from tracks to albums, in another color than the line from albums to artists

Show the data types and share the diagram

Choose View → Diagram Display → Column Data Type to write each column's type next to its name, as the Diagram page shows. In SQLite, the declared type is the only record of what a column was meant to hold. Outside a STRICT table, SQLite lets most columns store a value of any type, as its type affinity rules explain.

To give the diagram to someone who doesn't run DbSchema, choose File → Export As... → HTML5/PDF Documentation, pick HTML5, PDF or Markdown, and press Generate. The HTML5 documentation is one file that opens in any browser, with the diagram, a search box and the columns of every table.

A table in DbSchema's HTML5 documentation, from another schema: its comment, its columns with data types and constraints, its indexes and its foreign keys

In the Community Edition, DbSchema writes documentation for a diagram of up to 12 tables, and prints "DbSchema Trial" across the diagram image. DbSchema Pro writes it for a diagram of any size.

Download DbSchema, connect it to your SQLite file, and choose Diagram → Auto Arrange Tables. The Community Edition connects, reverse-engineers and draws the diagram. Saving the model with its virtual foreign keys, and documenting a diagram of more than 12 tables, need DbSchema Pro. Every new installation includes a 15-day trial that covers Pro.

DbSchema Database Designer