Make a table with CREATE TABLE, choose a type for each column, and give every row a key.
Every table you have used was designed by someone. Now you design one.
A table starts as a list of columns. For each one you choose a name and a type: INTEGER for whole numbers, TEXT for text. One column is the primary key: its value is different in every row, so a row can always be found by it.
Make your prediction first. Help opens once you've locked it in.
CREATE TABLE playlists (
id INTEGER PRIMARY KEY,
name TEXT,
owner TEXT
);
INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');
SELECT * FROM playlists;
Run it and open Tables in this database. Then run it again and read the message.
| Part | What it does |
|---|---|
CREATE TABLE playlists (...) |
Makes a new table with these columns |
name TEXT |
A column called name that holds text |
id INTEGER PRIMARY KEY |
A whole-number column that is different in every row |
The second run fails because the table is already made. Reset starts again.
CREATE TABLE playlists (
id INTEGER PRIMARY KEY,
name TEXT,
owner TEXT
);
INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');
SELECT * FROM playlists;
CREATE TABLE playlists (
id INTEGER PRIMARY KEY,
name TEXT,
owner TEXT
);
INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');
SELECT * FROM playlists;
Numbers you add up are numbers.
What goes wrong if two rows look the same?
Add a fourth column called description, and fill it in for the playlist.
Before a hint: what kind of stuck is this? Try a strategy first
Add description TEXT to the columns, then give the INSERT a fourth value.
The new row needs four values in the same order as the columns.
Add a description column to the playlists table and fill it in.
Run your program. This ticks when your code does all of the above.
Design a table called gigs for concerts. Give it a key, a place and a ticket price, add two rows, and show them.
Before a hint: what kind of stuck is this? Try a strategy first
Name the columns first: id as the key, then a place and a price.
CREATE TABLE gigs (id INTEGER PRIMARY KEY, place TEXT, price INTEGER);
Create a table called gigs with a primary key, add two rows and show them.
Run your program. This ticks when your code does all of the above.
Lesson done. You've finished every step you need.
Students create a table, choose a type for each column, give it a primary key and fill it with INSERT. About 25 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Choosing columns. Start from a real list on paper and ask what one row is.
Stretch: Add a second table of your own and join it to the first.
Support: Give the CREATE TABLE and ask students to write the INSERT.
Next lesson: link two tables with a key, so a review knows which song it is about.