Cheatsheet: SQLite

SQLite Specific Commands

List all tables in the database

SELECT name FROM sqlite_master WHERE type='table';

Get schema of a table

PRAGMA table_info(table_name);

List all views in the database

SELECT name FROM sqlite_master WHERE type='view';

Analyze a query

EXPLAIN QUERY PLAN SELECT * FROM table_name;

Display a low-level description of how SQLite will execute the SELECT statement

EXPLAIN SELECT * FROM table_name;

Enable foreign key support

PRAGMA foreign_keys = ON;

Set journal mode to Write-Ahead Logging (WAL)

PRAGMA journal_mode = WAL;

Set cache size

PRAGMA cache_size = 10000;

Set synchronous mode to FULL

PRAGMA synchronous = FULL;

Basic Commands

Create a new table

CREATE TABLE table_name (column1 datatype, column2 datatype, ...);

Insert data into a table

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Select data from a table

SELECT column1, column2, ... FROM table_name;

Table Modifications

Add a new column to a table

ALTER TABLE table_name ADD column_name datatype;

Delete a table

DROP TABLE table_name;

Update data in a table

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Delete data from a table

DELETE FROM table_name WHERE condition;

Advanced Queries

Select data with a pattern

SELECT * FROM table_name WHERE column_name LIKE 'pattern';

Group data by a column and count occurrences

SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;

Perform an inner join between two tables

SELECT column1, column2 FROM table1 INNER JOIN table2 ON table1.column = table2.column;