sqlite.org/download
.sqlite3.exe
file is essential; the others are optional.sqlite3.exe
) to a dedicated folder (e.g., C:\sqlite
).C:\sqlite
).sqlite3
to verify the installation..help
to see available commands..q
or .quit
to exit.cd
command (e.g., cd C:\sqlite
).sqlite3 myDB.db
(replace myDB.db
with your desired database name)..tables
: Shows existing tables in the current database.CREATE TABLE
: Creates a new table.CREATE TABLE store (store_id INT, item_id INT, item_name TEXT);
NULL
: Represents a missing or undefined value.INTEGER
: Signed integer values of varying sizes (e.g., INT
, SMALLINT
, SIGNED INTEGER
).REAL
: Floating-point values.TEXT
: Stores text strings.BLOB
: Stores binary data, such as images.INSERT INTO
: Inserts records into a table.INSERT INTO store (store_id, item_id, item_name) VALUES (1, 11, 'pen');
INSERT INTO store VALUES (2, 22, 'pencil');
SELECT * FROM
: Retrieves all records from a table.SELECT * FROM store;
SELECT
: Select specific columns from a tableSELECT item_name FROM Store;
.save
: Saves the current database to a file..save myDB.db
.quit
or .q
: Exits the SQLite command-line tool.Using sqlite3 myDB.db
or using dot open after connecting:
First Method
* sqlite3 myDB.db
Second mehtod
* sqlite3
* .open myDB.db
SELECT * FROM table_name WHERE condition;
SELECT * FROM store WHERE store_id = 1;
(retrieves records where store_id
is 1)..header on
: Displays column headers in the output..header off
: Hides column headers.AND
: Both conditions must be true.SELECT * FROM store WHERE item_name = 'pencil' AND item_id = 22;
OR
: At least one condition must be true.SELECT * FROM store WHERE item_name = 'pencil' OR store_id = 1;
SELECT columnA, COUNT(columnB) FROM table_name GROUP BY columnA;
SELECT * FROM table_name ORDER BY column_name;
(sorts in ascending order by default).SELECT * FROM table_name ORDER BY column_name DESC;
(sorts in descending order).SELECT * FROM invoice WHERE invoice_amount BETWEEN 1700 AND 6500;
WHERE
clause.SELECT invoice_id FROM invoice WHERE invoice_id IN (10, 50);
(equivalent to invoice_id = 10 OR invoice_id = 50
).UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
UPDATE invoice SET invoice_name = 'test' WHERE invoice_id = 10 AND invoice_amount = 100;
WHERE
clause is omitted, all records in the table will be updated..schema
: Displays the SQL schema of all tables..schema table_name
: Displays the schema of a specific table.sqlite.org/lang_datefunc.html
for a complete list of date and time functions.SELECT date('now');
(returns the current date in GMT).SELECT time('now');
(returns the current time in GMT).SELECT datetime('now');
(returns the current date and time in GMT).NOT NULL
: Ensures that a column cannot contain NULL
values.CREATE TABLE student (sid INTEGER PRIMARY KEY, sname TEXT, phone TEXT, email TEXT);
.NULL
.CREATE TABLE student_not_null1 (store_id INT PRIMARY KEY, store_name TEXT NOT NULL);
.NULL
values is important.CREATE TABLE t1 (tid INTEGER UNIQUE, tname TEXT);
CREATE TABLE t2 (tid INTEGER UNIQUE NOT NULL, tname TEXT DEFAULT 'unknown');
CREATE TABLE t3 (t_amount REAL CHECK(t_amount > 900));
ALTER TABLE
: Modifies the structure of an existing table.ALTER TABLE table_name ADD column_name data_type;
. Adds a new column.ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
Renames a column.ALTER TABLE table_name RENAME TO new_table_name;
. Renames a table.DROP TABLE table_name;
. Permanently deletes a table and its data.DELETE FROM table_name WHERE condition;
- Used to delete rows from the table.DELETE FROM table_name;
. Deletes all records from a table. The table structure remains..backup filename.db
. Creates a backup of the current database..restore backup_filename.db
. Restores a database from a backup file..mode
: Controls how query results are displayed.sqlite.org/cli.html
) for different CLI commands..mode csv
: Sets output to CSV format (comma-separated values)..mode insert
: Displays output as INSERT
statements..mode tabs
: Uses tab characters as separators..mode column
: Formats output in columns..mode list
: Sets the default output mode (pipe-separated values)..save myDB.db
dbeaver.io
): A database tool that provides a GUI for managing databases.sqlitebrowser.org
): Another GUI tool for managing SQLite databases.