D1 is compatible with most SQLite's SQL convention since it leverages SQLite's query engine. D1 supports a number of database-level statements that allow you to list tables, indexes, and inspect the schema for a given table or index.
D1 supports some SQLite PRAGMA ↗ statements. The PRAGMA statement is an SQL extension for SQLite. PRAGMA commands can be used to:
Modify the behavior of certain SQLite operations.
Query the SQLite library for internal data about schemas or tables (but note that PRAGMA statements cannot query the contents of a table).
Control environmental variables.
The PRAGMA statement examples on this page use the following SQL.
PRAGMA table_list
Lists the tables and views in the database. This includes the system tables maintained by D1.
Return values
One row per each table. Each row contains:
Schema: the schema in which the table appears (for example, main or temp)
name: the name of the table
type: the type of the object (one of table, view, shadow, virtual)
ncol: the number of columns in the table, including generated or hidden columns
wr: 1 if the table is a WITHOUT ROWID table, 0 otherwise
strict: 1 if the table is a STRICT table, 0 otherwise
Example of PRAGMA table_list
PRAGMA table_info("TABLE_NAME")
Shows the schema (columns, types, null, default values) for the given TABLE_NAME.
Return values
One row for each column in the specified table. Each row contains:
cid: a row identifier
name: the name of the column
type: the data type (if provided), '' otherwise
notnull: 1 if the column can be NULL, 0 otherwise
dflt_value: the default value of the column
pk: 1 if the column is a primary key, 0 otherwise
Example of PRAGMA table_info
PRAGMA table_xinfo("TABLE_NAME")
Similar to PRAGMA table_info(TABLE_NAME) but also includes generated columns.
Example of PRAGMA table_xinfo
PRAGMA index_list("TABLE_NAME")
Show the indexes for the given TABLE_NAME.
Return values
One row for each index associated with the specified table. Each row contains:
seq: a sequence number for internal tracking
name: the name of the index
unique: 1 if the index is UNIQUE, 0 otherwise
origin: the origin of the index (c if created by CREATE INDEX statement, u if created by UNIQUE constraint, pk if created by a PRIMARY KEY constraint)
partial: 1 if the index is a partial index, 0 otherwise
Example of PRAGMA index_list
PRAGMA index_info(INDEX_NAME)
Show the indexed column(s) for the given INDEX_NAME.
Return values
One row for each key column in the specified index. Each row contains:
seqno: the rank of the column within the index
cid: the rank of the column within the table being indexed
name: the name of the column being indexed
Example of PRAGMA index_info
PRAGMA index_xinfo("INDEX_NAME")
Similar to PRAGMA index_info("TABLE_NAME") but also includes hidden columns.
Example of PRAGMA index_xinfo
PRAGMA quick_check
Checks the formatting and consistency of the table, including:
Incorrectly formatted records
Missing pages
Sections of the database which are used multiple times, or are not used at all.
Return values
If there are no errors: a single row with the value OK
If there are errors: a string which describes the issues flagged by the check
Example of PRAGMA quick_check
PRAGMA foreign_key_check
Checks for invalid references of foreign keys in the selected table.
PRAGMA foreign_key_list("TABLE_NAME")
Lists the foreign key constraints in the selected table.
PRAGMA case_sensitive_like = (on|off)
Toggles case sensitivity for LIKE operators. When PRAGMA case_sensitive_like is set to:
ON: 'a' LIKE 'A' is false
OFF: 'a' LIKE 'A' is true (this is the default behavior of the LIKE operator)
PRAGMA ignore_check_constraints = (on|off)
Toggles the enforcement of CHECK constraints. When PRAGMA ignore_check_constraints is set to:
ON: check constraints are ignored
OFF: check constraints are enforced (this is the default behavior)
PRAGMA legacy_alter_table = (on|off)
Toggles the ALTER TABLE RENAME command behavior before/after the legacy version of SQLite (3.24.0). When PRAGMA legacy_alter_table is set to:
ON: ALTER TABLE RENAME only rewrites the initial occurrence of the table name in its CREATE TABLE statement and any associated CREATE INDEX and CREATE TRIGGER statements. All other occurrences are unmodified.
OFF: ALTER TABLE RENAME rewrites all references to the table name in the schema (this is the default behavior).
PRAGMA recursive_triggers = (on|off)
Toggles the recursive trigger capability. When PRAGMA recursive_triggers is set to:
ON: triggers which fire can activate other triggers (a single trigger can fire multiple times over the same row)
OFF: triggers which fire cannot activate other triggers
PRAGMA reverse_unordered_selects = (on|off)
Toggles the order of the results of a SELECT statement without an ORDER BY clause. When PRAGMA reverse_unordered_selects is set to:
ON: reverses the order of results of a SELECT statement
OFF: returns the results of a SELECT statement in the usual order
PRAGMA foreign_keys = (on|off)
Toggles the foreign key constraint enforcement. When PRAGMA foreign_keys is set to:
ON: stops operations which violate foreign key constraints
OFF: allows operations which violate foreign key constraints
PRAGMA defer_foreign_keys = (on|off)
Allows you to defer the enforcement of foreign key constraints until the end of the current transaction. This can be useful during database migrations, as schema changes may temporarily violate constraints depending on the order in which they are applied.
This does not disable foreign key enforcement outside of the current transaction. If you have not resolved outstanding foreign key violations at the end of your transaction, it will fail with a FOREIGN KEY constraint failed error.
Note that setting PRAGMA defer_foreign_keys = ON does not prevent ON DELETE CASCADE actions from being executed. While foreign key constraint checks are deferred until the end of a transaction, ON DELETE CASCADE operations will remain active, consistent with SQLite's behavior.
To defer foreign key enforcement, set PRAGMA defer_foreign_keys = on at the start of your transaction, or ahead of changes that would violate constraints: