sql · level 1 · unit 7 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
gives every row its own number.
Two pets could both be called Rex. To tell rows apart, give each one an : a number no other row has, like a cloakroom ticket.
id INTEGER PRIMARY KEY makes that column the id. Leave it out of your INSERT and the database fills it in for you, counting 1, 2, 3.
Two rows can never share an id. Try it and you get UNIQUE constraint failed. So WHERE id = 2 always finds exactly one row.
CREATE TABLE members (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO members (name) VALUES ('Rex'), ('Rex'), ('Luna');
SELECT * FROM members; An id is the safest thing to put in the WHERE of an UPDATE or DELETE.
guess first
Which Rex goes? Guess what is left.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
CREATE TABLE members (id INTEGER PRIMARY KEY, name TEXT);
INSERT INTO members (name) VALUES ('Rex'), ('Rex'), ('Luna');
DELETE FROM members WHERE id = 2;
SELECT * FROM members;
your turn
Your turn. Sign up some members.
Type three names in the blanks, keeping their quotes. Leave the id out and let the database number them for you.
result
Your answer appears here as a table.
fix it
Two members got the same ticket. Fix it!
result
Your answer appears here as a table.
- Read the error. Which column does it name?
- Every id must belong to one row only, but Ava and Leo both have 1.
- Change Leo's id on line 3 to 2, then press run.
nailed it
every row has its own id now. :D
- An id is a number that belongs to one row only.
INTEGER PRIMARY KEYnumbers new rows for you.- Two rows can never share an id.
new words in your collection
- id
- PRIMARY KEY
This lesson isn’t saved. A free account remembers every lesson you finish.