Appearance
Setup
Just enough tooling to run SQL locally - no Docker, no cloud console, no drama.
What you need
- SQLite - the database engine (ships as the
sqlite3CLI on most systems) - A text editor (you already have one)
- Optional: DB Browser for SQLite if you like clicking around
Think of SQLite as "a database that lives in one file". No server process. You open the file, run SQL, close it. Perfect for learning.
Install sqlite3
Linux
bash
# Debian / Ubuntu
sudo apt install sqlite3
# Fedora
sudo dnf install sqlitemacOS
bash
brew install sqlite(macOS often has a system sqlite3 already - brew gives you a newer one.)
Windows
Install from the SQLite download page, or use WSL and follow the Linux bits.
Check it:
bash
sqlite3 --versionYour first database
bash
mkdir -p ~/sql-playground
cd ~/sql-playground
sqlite3 hello.dbYou're now inside the SQLite prompt (starts with sqlite>). Try:
sql
CREATE TABLE greetings (
id INTEGER PRIMARY KEY,
message TEXT NOT NULL
);
INSERT INTO greetings (message) VALUES ('hello from sql');
SELECT * FROM greetings;Exit with .quit.
Comments for the code above:
hello.dbis just a file on disk - that's the whole databaseCREATE TABLEdefines the shape (we'll go deep later)INSERTadds a row;SELECTreads rows back
Running SQL from files
You don't have to type everything interactively. Later chapters point at examples/crm/*.sql:
bash
sqlite3 crm.db < schema.sql
sqlite3 crm.db < seed.sqlOr from inside the prompt:
sql
.read schema.sqlHandy CLI dots
| Command | What it does |
|---|---|
.tables | list tables |
.schema contacts | show CREATE for a table |
.mode column | prettier output |
.headers on | show column names |
.quit | leave |
I usually turn headers on once and forget about it:
sql
.headers on
.mode columnPostgres / MySQL note
If your day job uses Postgres or MySQL - good. The SQL we'll write is mostly portable. I'll call out the few spots where SQLite is weird (types, AUTOINCREMENT, etc.).
Next: Mental model - tables as collections of objects.