sql · level 2 · unit 6 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
fills in a value whenever you leave a column out.
In Level 1, a column you left out of an INSERT became NULL. Often there is a better start. A new order is 'open', and a new player has 0 points.
Write DEFAULT and a value after the column's type, like status TEXT DEFAULT 'open'. Leave status out of an INSERT and the database writes 'open' for you.
Give the column a value yourself and the default steps aside. It only ever fills a gap.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
item TEXT NOT NULL,
status TEXT DEFAULT 'open'
);
INSERT INTO orders (item) VALUES ('Teapot');
INSERT INTO orders (item, status) VALUES ('Mug', 'sent');
SELECT * FROM orders; Writing NULL on purpose still stores NULL. Add NOT NULL too if the box must never be empty.
guess first
Two new players join. What are their scores?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
CREATE TABLE players (name TEXT, points INTEGER DEFAULT 0, level INTEGER DEFAULT 1);
INSERT INTO players (name) VALUES ('Mia');
INSERT INTO players (name, points) VALUES ('Leo', 50);
SELECT * FROM players;
your turn
Your turn. Set up a to-do list.
Type a starting value for done in the first blank, like no. Type a task in the second, like walk the dog. The new task gets your default.
result
Your answer appears here as a table.
fix it
Every new order should start as open. Fix it!
result
Your answer appears here as a table.
- Neither row gives a status. What does a left-out column get?
- Give the
statuscolumn a starting value inCREATE TABLE. - Change status TEXT to status TEXT DEFAULT 'open', then press run.
nailed it
your columns start with sensible values. :D
DEFAULTfills a column that anINSERTleaves out.- A value you give yourself always wins.
- Add
NOT NULLtoo if the box must never be empty.
new words in your collection
- DEFAULT
This lesson isn’t saved. A free account remembers every lesson you finish.