Structured Query Language (SQL)
Create records. Ask for them. Change what persists.
Gregory S. DeLozier, PhD
Which directory contains this database?
| Input | Who reads it? |
|---|---|
sqlite3 pets.db |
Operating-system shell |
.tables |
SQLite command-line shell |
SELECT * FROM pet; |
SQLite database engine |
The $ and sqlite> prompts are not
input.
Transcript excerpt:
sqlite> .schema
sqlite> .tables
sqlite> .headers on
sqlite> .mode column
Definitions, table names, and readable output
CREATE TABLE pet (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
kind TEXT NOT NULL,
age INTEGER,
food TEXT
);The table exists. There are no pets yet.
| name | kind | age | food |
|---|---|---|---|
| Dorothy | dog | 11 | peanut butter |
Where does id come from?
After inserting Sandy and Whiskers:
| id | name | kind | age | food |
|---|---|---|---|---|
| 1 | Dorothy | dog | 11 | peanut butter |
| 2 | Sandy | cat | 11 | tuna |
| 3 | Whiskers | hamster | 11 | hamster chow |
| name | food |
|---|---|
| Sandy | tuna |
Which part chooses columns? Which part chooses rows?
Remaining identifiers: 1 and 3
What would happen without WHERE?
Leave SQLite with .quit, then run:
| id | name | kind | age | food |
|---|---|---|---|---|
| 3 | Whiskers | hamster | 11 | hamster chow |
The closing SQL marks the end of the input.
The transcript skips this step.
| name | kind |
|---|---|
| Sandy | cat |
| Dorothy | dog |
| Whiskers | hamster |
What order would ORDER BY food produce?