Reverse Engineering a SQLite Database
For developers who hold a SQLite file with no documentation and need its tables, columns and relationships.
On this page
You have a SQLite file from an app or a client, and nobody wrote down what is in it. SQLite keeps the CREATE statement of every table, index, view and trigger inside the file. Read those statements from sqlite_master, ask the PRAGMA functions for the columns and foreign keys, then look for the relationships that no statement declares. DbSchema reads these statements in one step and draws the tables as an ER diagram.
To run the queries below on your own machine, create this shop database in an empty file:
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date TEXT
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL,
product TEXT NOT NULL,
quantity INTEGER
);
CREATE INDEX idx_order_items_order ON order_items (order_id);
CREATE VIEW order_sizes AS
SELECT order_id, sum(quantity) AS items
FROM order_items
GROUP BY order_id;
INSERT INTO customers VALUES (1, 'Anna'), (2, 'Ben');
INSERT INTO orders VALUES (10, 1, '2026-03-01'), (11, 2, '2026-03-02'), (12, 3, '2026-03-03');
INSERT INTO order_items VALUES (10, 'pen', 2), (11, 'ink', 'two'), (13, 'pad', 1);
SQLite accepts every row, although three rows hold mistakes. The queries below find each one.
Copy the file before you open it
Work on a copy. If the folder holds a file with the same name plus -wal, such as shop.db-wal, copy that file with the database. The database then runs in WAL mode: SQLite writes committed changes to the -wal file first, and moves them into the database file later.
The SQLite WAL documentation says that a copy without its -wal file can lose committed transactions, or come out corrupted.
Read the CREATE statements from sqlite_master
Every SQLite database holds a table with one row for each table, index, view and trigger in the file. The SQLite documentation names this table sqlite_schema, and SQLite also accepts its older name, sqlite_master. Most scripts use sqlite_master, and so do the queries below:
SELECT type, name, tbl_name
FROM sqlite_master
ORDER BY type, name;
| type | name | tbl_name |
|---|---|---|
| index | idx_order_items_order | order_items |
| table | customers | customers |
| table | order_items | order_items |
| table | orders | orders |
| view | order_sizes | order_sizes |
The tbl_name column names the table that each object belongs to. The index idx_order_items_order belongs to order_items, and each table belongs to itself.
The sql column holds each object's CREATE statement:
SELECT sql FROM sqlite_master WHERE name = 'orders';
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date TEXT
)
SQLite keeps the statement as it was typed, with its comments and line breaks. In the sqlite3 shell, the .schema command prints this column for every object.
Columns and foreign keys with PRAGMA
SQLite has no information_schema views. It answers questions about the schema through PRAGMA statements, which return rows like a query. PRAGMA table_info returns one row for each column of a table.
On SQLite 3.16.0 and later, you can also call these PRAGMAs as functions named with the prefix pragma_, and join them like tables. This query lists the columns of every table:
SELECT m.name AS table_name, c.name AS column_name, c.type, c.pk
FROM sqlite_master AS m
JOIN pragma_table_info(m.name) AS c
WHERE m.type = 'table'
ORDER BY m.name, c.cid;
| table_name | column_name | type | pk |
|---|---|---|---|
| customers | customer_id | INTEGER | 1 |
| customers | name | TEXT | 0 |
| order_items | order_id | INTEGER | 0 |
| order_items | product | TEXT | 0 |
| order_items | quantity | INTEGER | 0 |
| orders | order_id | INTEGER | 1 |
| orders | customer_id | INTEGER | 0 |
| orders | order_date | TEXT | 0 |
The pk column is 1 for the primary key of customers and of orders. The order_items table has no primary key.
PRAGMA foreign_key_list returns the foreign keys that a table declares with REFERENCES:
PRAGMA foreign_key_list('orders');
| id | seq | table | from | to | on_update | on_delete | match |
|---|---|---|---|---|---|---|---|
| 0 | 0 | customers | customer_id | customer_id | NO ACTION | NO ACTION | NONE |
The column orders.customer_id refers to customers.customer_id. To list the foreign keys of every table at once, join pragma_foreign_key_list to sqlite_master. The query is in How to Create ER Diagrams for SQLite.
Find the relationships that nobody declared
The column order_items.order_id holds order numbers, but its CREATE statement has no REFERENCES clause. PRAGMA foreign_key_list('order_items') returns no row. SQLite enforces a foreign key only on a connection that runs PRAGMA foreign_keys = ON. The SQLite foreign key documentation says that this setting is off by default.
Start from the column names. This query lists the columns whose name ends in _id, and leaves out primary keys and columns with a declared foreign key. GLOB reads the underscore as a plain character, where LIKE would read it as a wildcard:
SELECT m.name AS table_name, c.name AS column_name
FROM sqlite_master AS m
JOIN pragma_table_info(m.name) AS c
LEFT JOIN pragma_foreign_key_list(m.name) AS f ON f."from" = c.name
WHERE m.type = 'table'
AND c.name GLOB '*_id'
AND c.pk = 0
AND f."from" IS NULL;
| table_name | column_name |
|---|---|
| order_items | order_id |
GLOB is case-sensitive, so a column named Order_ID needs the pattern '*_ID'.
A matching name is only a hint, so check the data next. This query returns the order_items rows whose order number has no order:
SELECT i.order_id, i.product
FROM order_items AS i
LEFT JOIN orders AS o ON o.order_id = i.order_id
WHERE o.order_id IS NULL;
| order_id | product |
|---|---|
| 13 | pad |
Two of the three items match an order, so order_items.order_id refers to orders. The pad row points at order 13, which doesn't exist.
A declared foreign key can be broken too, because SQLite didn't check it when the rows went in. PRAGMA foreign_key_check lists every row that breaks a declared foreign key:
PRAGMA foreign_key_check;
| table | rowid | parent | fkid |
|---|---|---|---|
| orders | 12 | customers | 0 |
Order 12 refers to customer 3, and the customers table has no customer 3. The rowid is the number that SQLite stores each row under. An INTEGER PRIMARY KEY column holds the rowid, so the rowid here is the order number. foreign_key_check works while foreign_keys is off, its default, so it checks the file as you received it.
The file holds one declared relationship, and one that only the column name and the data show:
Declared types and stored values
PRAGMA table_info returns the type that the CREATE statement declares. Outside a STRICT table, SQLite converts a value to the declared type where it can, and otherwise stores the value as it is. The type affinity rules say which conversions apply. The typeof function returns the type of each stored value:
SELECT product, quantity, typeof(quantity)
FROM order_items;
| product | quantity | typeof(quantity) |
|---|---|---|
| pen | 2 | integer |
| ink | two | text |
| pad | 1 | integer |
The quantity column is declared INTEGER, and the ink row holds the text two. SQLite could not turn two into a number, so it stored the text.
SQLite 3.37.0 added STRICT tables. A STRICT order_items table rejects the same row with the error "cannot store TEXT value in INTEGER column order_items.quantity". The strict column of PRAGMA table_list shows which tables are STRICT. Where a table is not STRICT, check the stored types before you trust the declared ones.
Reverse-engineer the file in DbSchema
DbSchema reads the same schema from the file and draws it as an ER diagram. Connect DbSchema to a copy of the file and import it, with the steps in How to Create ER Diagrams for SQLite. DbSchema downloads the SQLite JDBC driver the first time you connect.
Connecting and importing read the file and change nothing in it, as the connection documentation says. DbSchema keeps the tables, columns, indexes and foreign keys that it read in a model, its own copy of the schema, apart from the SQLite file.
On the diagram, order_items has no line to orders, because its CREATE statement declares no foreign key. Choose Model → Find Virtual Foreign Keys, and DbSchema suggests virtual foreign keys between columns with matching names, such as order_items.order_id and orders.order_id.
A virtual foreign key exists only in the DbSchema model, and DbSchema never creates it in the SQLite file. In DbSchema Pro, the Data Explorer follows virtual foreign keys like real ones: click an order, and the next table lists its items.
Download DbSchema, connect it to a copy of your SQLite file, and choose Model → Find Virtual Foreign Keys. The free Community Edition connects, reverse-engineers the file and draws the diagram. Saving the model with its virtual foreign keys needs DbSchema Pro, and every new installation includes a 15-day trial that covers Pro.
Draw your SQLite file as a diagram
DbSchema reverse-engineers a SQLite file into an interactive ER diagram and lets you draw the relationships SQLite never enforced as virtual foreign keys that live in the model, not in the database. Reverse engineering, ER diagrams and the SQL editor are in the free Community Edition.