Tending the Bed
and changes the bed itself — planting, pruning, pulling
Page 2 · Welcome
Until now, you've only looked
today you start to change what's there
Until now you only looked. In Chapter I you walked the bed and read the flowers with
SELECT. You asked the bed questions. The bed answered. Nothing
in the bed was moved.
Today is different. The bed will change. You will plant a rose where there was none. You will prune one shorter. You will paint another a new color. You will pull a dying one out of the soil.
There are three verbs for this work. INSERT puts a flower in. UPDATE changes one that is there. DELETE takes one away. You will meet each. By the end of the day the bed will be different, and you will be the one who made it different.
Page 3 · Three new verbs
Three tools, three verbs
INSERT plants, UPDATE prunes, DELETE pulls
Each verb has its own shape — its own syntax — but they all share one thing: they change the bed instead of just looking at it. After running one, the rose_bed table will be different. The roses you can see, count, and walk past will be different.
All three accept the familiar WHERE from Chapter I. With INSERT, WHERE isn't used (you're planting something new). With UPDATE and DELETE, WHERE decides which flowers the change applies to. Forget the WHERE and you'll change every flower in the bed — sometimes that's what you want, but more often it's a mistake the bed can't undo.
Page 4 · INSERT
Plant a flower
INSERT INTO rose_bed VALUES (...)
To plant a new rose, name the table you're planting into, list the columns you're filling, and list the values for each. The shape of an INSERT looks like a small recipe:
INSERT INTO rose_bed (id, species, color, height)
VALUES (6, 'Pink Lady', 'pink', 17);
Press Run query. A new rose appears in the bed.
VALUES matches the value list inside the
parentheses, in order. Text values go in single quotes; numbers don't.
The query returns "1 row inserted" — your status message confirms what happened.
Shortcut: you can drop the column list entirely —
INSERT INTO rose_bed VALUES (7, 'Mary Rose', 'pink', 19); —
but then you must supply every column in the table, in the order it was defined.
Try the no column list chip to see it. Most experienced gardeners stick with the
explicit column list — it's longer, but it survives the bed gaining a new column without
breaking.Page 5 · INSERT multiple rows
Plant several at once
multiple value rows, comma-separated
You can plant more than one rose in a single statement. After VALUES,
list one set of values per new flower, each set in its own parentheses, separated by commas. Two new
roses join the bed — including Sun Flare, a yellow rose. The color
column is just text; the bed will accept any colour name you write. (In Chapter III you'll see how to
add a rule that restricts it to a chosen list.)
Page 6 · UPDATE
Prune a rose taller
UPDATE rose_bed SET height = ... WHERE ...
Iceberg (id 4) is only 16 cm — the shortest rose in the bed. Suppose she's had a strong season — measure her again at 24 cm and write the new height into her tag. The verb is UPDATE, followed by which column to change, set to which value, and which rose to apply it to:
SET col = value
names the column and its new value. WHERE is the filter that
decides which rows the change applies to.Page 7 · UPDATE multiple columns
Repot and repaint
several SET clauses, comma-separated
You can change more than one column at once. Carefree Beauty (id 5) has been pruned taller and her petals have faded — let's record both at once, her new color and her new height:
name = value, separated by commas. And the right side of
= doesn't have to be a literal — it can be an
expression that reads the current row. The
grow all pinks +2 chip uses
height = height + 2, which means
take each rose's current height and add two.Page 8 · DELETE
Pull a flower
DELETE FROM rose_bed WHERE ...
The third tool is the simplest. DELETE FROM a table, optionally with a WHERE filter, and the matching rows are gone. Let's pull the purple roses — Crimson Glory will be the only casualty in the baseline bed:
Page 9 · WHERE is everything
WHERE is everything
precision matters most when the bed will actually change
WHERE was first introduced in Chapter I as a filter for SELECT. The same filter shapes UPDATE and DELETE. The only difference: when SELECT misreads a WHERE, you get a wrong answer on screen. When UPDATE or DELETE misreads a WHERE, the bed itself is wrong, and it stays wrong.
Three habits that experienced gardeners share:
- Read the WHERE out loud before running. "Only roses where color is purple." Say it to yourself. It catches a surprising number of mistakes.
- SELECT first, mutate second. Run
SELECT * FROM rose_bed WHERE color = 'purple'first. Confirm the rows you see are the ones you mean. Then change the SELECT to UPDATE or DELETE. - If there's no WHERE, double-check that's intentional. Sometimes it is — you really do want to update or delete everything. But pause and confirm.
-- 1. Look first. SELECT * FROM rose_bed WHERE color = 'purple'; -- 2. Confirm those are the rows you mean. -- 3. Then change SELECT * to UPDATE/DELETE. DELETE FROM rose_bed WHERE color = 'purple';
Page 10 · The anatomy of writes
The three shapes
INSERT, UPDATE, DELETE side by side
Each verb has its own template. Memorize the shape; the details follow.
INSERT INTO rose_bed (id, species, color, height) VALUES (6, 'Pink Lady', 'pink', 17); UPDATE rose_bed SET color = 'white', height = 20 WHERE id = 5; DELETE FROM rose_bed WHERE color = 'purple';
Drop the WHERE on UPDATE or DELETE and the change applies to every row in the bed. It runs without complaint and cannot be undone — see Page 9.
Page 11 · Tend the bed yourself
Tend the bed yourself
any INSERT, UPDATE, DELETE — or a SELECT to peek
The bed is yours. Try the chips below in order — peek, plant, grow, pull, peek again — to feel a small gardener's workflow. Or write anything you like; the bed keeps each change until you press Reset bed.
rose_bed — schema
Page 12 · End of Chapter II
The bed is yours
what you've learned, and what's next
In the garden of Tending the Bed, the gardener touches the soil itself. Flowers are added where there were none; ones already present are altered; others are pulled from the ground. By the day's end, the bed is no longer the bed of the morning.
After this garden lies one more in Spring: it is called the Greenhouse. In it, no flower is yet planted — only a card is drafted, on which the columns of the future bed are named and the rules it will obey are stamped. By such cards, every bed in the garden is shaped before its first seed.
Further on, in Winter, you'll learn how to wrap risky changes so they can be undone if you change your mind. Until then, the Reset bed button is your stand-in. For today, the trowel and shears are cleaned, and the bed is yours.
Check your bed
Three questions to confirm INSERT, UPDATE, and DELETE feel natural. Write a statement and press Check.
1. Plant a new rose: id 6, species 'Sweet Surrender', color 'pink', height 19. Use INSERT.
2. Rename rose id 4 from 'Iceberg' to 'Polar Star', and reset its height to 20. Use UPDATE.
3. Pull every rose shorter than 18 cm out of the bed. Use DELETE.
the bed shaped by your own hand