A database describes itself. Page 1 is the root of a table b-tree, the schema table, whose rows list every table, index, view and trigger, each with the SQL that made it and, for tables and indexes, the page number of its b-tree’s root. A reader starts here to find everything else.
sqlite_schema
The schema table is stored as if it had been made by:
CREATE TABLE sqlite_schema(type text, name text, tbl_name text, rootpage integer, sql text);
| Column | Holds |
|---|---|
type | table (ordinary and virtual tables), index, view or trigger. |
name | The object’s name. |
tbl_name | The table an index or trigger belongs to; for a table or view, its own name. |
rootpage | The root page of a table’s or index’s b-tree; 0 or NULL for views, triggers and virtual tables. |
sql | The CREATE statement that made the object, normalised as below, or NULL for the indexes SQLite makes itself. SQLite rewrites it when ALTER TABLE changes the object. |
The text of sql is close to what was typed, but not the same. The words
CREATE, TABLE, VIEW, TRIGGER and INDEX are upper-cased, TEMP or TEMPORARY after the
first CREATE is removed, any database name before the object’s name is removed, spaces
before the statement are removed, and the spaces after the first two words are reduced
to one.
The table can also be named sqlite_master, its older name, which works
everywhere. In the TEMP database it is sqlite_temp_schema or
sqlite_temp_master. None of these names appears in the file. The schema
table itself has no row of its own.
The first row of the schema table in a database from the files examined for this
site is decoded under Records: type
table, name and table papers, root page 2, and its CREATE
TABLE statement. That schema lists the tables papers, authors, authorspapers, tags,
tagspapers, files and links, and four indexes named
sqlite_autoindex_….
Tables
An ordinary table is a table b-tree, keyed by the 64-bit signed rowid. Each row is a table leaf cell: the rowid and a record of the columns in the order the table declares them.
A column declared INTEGER PRIMARY KEY, with the type written exactly as
INTEGER, becomes another name for the rowid. Its value is the cell’s rowid, and the
record stores NULL in its place. A column of REAL affinity may hold a value that fits
an integer as an integer, to save space; SQLite reads it back as a float.
A virtual table has no b-tree of its own: its rows come from the module that implements it, which may keep them in ordinary tables of its own.
WITHOUT ROWID tables
A table whose definition ends WITHOUT ROWID (SQLite 3.8.2 and later) has
no rowid. It is stored in an index b-tree, whose entries are records of the PRIMARY KEY
columns followed by the remaining columns. Its schema row has type
table; only the SQL shows the difference.
A column named more than once in the PRIMARY KEY with the same collation counts only the first time, so all of these are the same table on disk:
CREATE TABLE t1(a,b,c,d,PRIMARY KEY(a,c)) WITHOUT ROWID;
CREATE TABLE t1(a,b,c,d,PRIMARY KEY(a,c,a,c)) WITHOUT ROWID;
CREATE TABLE t1(a,b,c,d,PRIMARY KEY(a,A,a,C)) WITHOUT ROWID;
A column repeated with a different collation is kept each time.
Indexes
Every index, whether made with CREATE INDEX or by SQLite for a UNIQUE or PRIMARY KEY constraint, is an index b-tree. Each entry is a record of the indexed columns followed by the row’s key: the rowid, or for a WITHOUT ROWID table, its PRIMARY KEY columns. To find a row from an index, a reader takes the key from the end of the entry and looks it up in the table’s b-tree. The entries are in the record sort order, under the collation each column of the index uses.
In a database of schema format 4, a column marked DESC in an index is stored in descending order.
A partial index, made with a WHERE clause, has an entry only for the rows the clause is true of.
For a WITHOUT ROWID table the row’s key is its PRIMARY KEY columns, but any of them the index already holds under the same collation is not repeated at the end of the entry. With the table and indexes:
CREATE TABLE ex25(a,b,c,d,e,PRIMARY KEY(d,c,a)) WITHOUT ROWID;
CREATE INDEX ex25ce ON ex25(c,e);
CREATE INDEX ex25acde ON ex25(a,c,d,e);
CREATE INDEX ex25ae ON ex25(a COLLATE nocase,e);
ex25ceholds c, e, d, a: the key’s c is already there, so only d and a are added.ex25acdeholds a, c, d, e: every key column is already there.ex25aeholds a, e, d, c, a: the indexed a uses NOCASE, the key’s a BINARY, so both are kept.
Index entries of an ordinary rowid table always end with the rowid. An index b-tree’s page gives no sign of these rules: a reader needs the table’s and the index’s SQL, which the explorer uses to name each value of an entry.
Internal tables and indexes
Names that start with sqlite_ belong to SQLite.
| Name | Type | What it is |
|---|---|---|
sqlite_autoindex_TABLE_N | index | An index SQLite made for a UNIQUE or PRIMARY KEY constraint of table TABLE, numbered from 1. Its sql is NULL. |
sqlite_sequence | table | (name, seq): one row for each AUTOINCREMENT table, holding the largest rowid it has used. Dropping the table removes its row. When seq reaches 9,223,372,036,854,775,807, further automatic inserts fail with SQLITE_FULL. Programs may read and change the rows, but not drop the table. |
sqlite_stat1 to sqlite_stat4 | table | The statistics ANALYZE collects for the query planner; see below. |
The sqlite_stat tables
ANALYZE writes these tables, which the query planner reads to choose
indexes. It always writes sqlite_stat1; sqlite_stat3 and
sqlite_stat4 exist only when SQLite was built with the options given
below. They are ordinary tables in the file, whose names and SQL are fixed. The
explorer decodes them in the rows preview, and adds a column that says what each row
means.
sqlite_stat1
CREATE TABLE sqlite_stat1(tbl,idx,stat);
One row for each index the statistics cover, or for a table with no indexes, with
idx NULL. stat is text: numbers, then optional words,
separated by spaces.
- The first number is the approximate number of rows in the index, which is the
same as in the table except for a partial index. With
idxNULL it is the rows in the table, and is all there is. - For an index of K columns, K more numbers follow. The number at position N is the average count of rows sharing the same values in the first N indexed columns. For a unique index the last number is 1.
- With
idxthe same astbl, the table is a WITHOUT ROWID table, and the row describes the index b-tree that holds the table itself. unordered: the index is not used for sorting or range queries.sz=NNN: the average row size, about NNN bytes.noskipscan: the index is not used with the skip-scan optimisation.- Any other word is ignored.
sqlite_stat2
CREATE TABLE sqlite_stat2(tbl,idx,sampleno,sample);
Used only by SQLite 3.6.18 to 3.7.8, and ignored by later versions. One row for each
of ten samples of an index’s left-most column. sampleno is 0 to 9, and
sample is the value at row number (i × C × 2 + C) ÷ 20 of the C rows
of the index, where i is the sample number.
sqlite_stat3
CREATE TABLE sqlite_stat3(tbl,idx,nEq,nLt,nDLt,sample);
One row for each sample of an index’s left-most column. sample is the
value; nEq estimates how many rows equal it; nLt how many
are less; and nDLt how many distinct left-most values are less. The
rows of one index are in index order.
Only SQLite 3.7.9 and later builds with SQLITE_ENABLE_STAT3 or
SQLITE_ENABLE_STAT4 write this table. From 3.8.1 a STAT4 build reads it
but no longer writes it.
sqlite_stat4
CREATE TABLE sqlite_stat4(tbl,idx,nEq,nLt,nDLt,sample);
Like sqlite_stat3, for all the columns of an entry. idx
names the index, or the table itself for a WITHOUT ROWID table. sample
is a BLOB that is itself a record in
the format of an entry of the index: the indexed columns then the row’s key. Let N be
the number of values in sample: the indexed columns plus 1 (the rowid)
for an index on a rowid table, the indexed columns plus the primary key’s columns
for an index on a WITHOUT ROWID table, or only the primary key’s columns for a
WITHOUT ROWID table’s own rows. nEq, nLt and
nDLt are text: N numbers separated by spaces, the Kth number covering
the first K columns. The rows of one index are in index order.
Only SQLite 3.8.1 and later builds with SQLITE_ENABLE_STAT4 write this
table.