Welcome
A database is a program whose whole job is to store data and answer questions about it. Your bank uses one to remember your balance. Instagram uses one to remember who follows whom. Almost every app you have ever used sits on top of a database.
The most common kind is a relational database, and it stores everything in tables. A table looks like a simple grid:
- Each column is one kind of fact, with a name. A
petstable might have columnsname,species, andage. - Each row is one record: one pet, one user, one order.
- Each cell holds a single value, like
'Mochi'or5.
That is the entire mental model. A database is a collection of tables, a table is rows and columns, and you talk to it with a language called SQL (Structured Query Language, often said "sequel").
Running SQL in this course
Every code block in this course is real SQL that runs on SQLite, a small, real database engine, inside an isolated sandbox. Each block is self-contained: it creates a table, fills it with rows, then queries it. Three statements to recognize:
CREATE TABLEmakes a new empty table and names its columns.INSERT INTO ... VALUES (...)adds one row.SELECT ... FROM ...asks a question and prints the answer.
Every statement ends with a semicolon ;. Text values go in single quotes ('Mochi'), numbers do not (5).
One thing about the output: the sandbox prints each result row on its own line, with a | between values and no header row. So the row for Mochi prints as Mochi|cat|5.
Run the block below. SELECT * FROM pets means "give me every column of every row" (* is shorthand for all columns).
Code exercise · sql
Press Run. The script builds a pets table, adds three rows, then selects everything. Match the output to the table in the figure above.
Quiz
In the pets table, what is `('Mochi', 'cat', 5)`?
Code exercise · sql
Your turn. Add one more INSERT so the table also contains a 1-year-old dog named Rex, then keep the SELECT so all four rows print. The expected output shows Rex last because he was inserted last.