Changing data
Add, modify, and remove rows with INSERT, UPDATE, and DELETE.
- Add rows with INSERT
- Modify rows with UPDATE, scoped by WHERE
- Remove rows with DELETE, scoped by WHERE
So far you've only read data. Three statements change it. (Each Run below starts from a fresh copy of the shop inside a transaction, so experiment freely — you can't break anything.)
INSERT
Add a row by listing the values:
The new customer appears at the end. (Running setup + your statements + a SELECT in one go is just for the demo — normally you'd run the INSERT on its own.)
UPDATE — mind the WHERE
UPDATE changes existing rows. The WHERE decides which rows:
An UPDATE or DELETE without a WHERE changes every row. UPDATE products SET price = 0; zeroes all prices. Always write (and double-check) the WHERE
first — this is one of the most common, most painful SQL mistakes.
DELETE
DELETE removes rows matching the WHERE:
Edsger (NL) is gone. Same warning applies: no WHERE means delete everything.
Where to go next
That completes the reading and writing basics. Next up — the module lab, where you'll write graded queries against the shop.