Changing data
Add, change and remove rows with INSERT, UPDATE and DELETE, and avoid the forgotten WHERE.
18 min
Until now you only read. Three statements change data.
INSERT INTO books (id, title, author, genre, price, year, stock)
VALUES (11, 'Tide Tables', 'R. Okafor', 'nonfiction', 15.00, 2024, 10);Name the columns, then give the values in the same order. Columns you leave out get their default, usually NULL. Add several rows with VALUES (...), (...), (...).
UPDATE books
SET price = ROUND(price * 1.1, 2)
WHERE genre = 'cooking';SET lists the new values; WHERE says which rows to change.
DELETE FROM books WHERE stock = 0;UPDATE and DELETE without WHERE change every row. There is no confirmation and no undo. Habits that protect you:
- 1.Write the
WHEREfirst, or write it as aSELECTfirst (SELECT * FROM books WHERE stock = 0;), check the rows, then changeSELECT *intoDELETE. - 2.Use a primary key to target one row:
WHERE id = 5. - 3.On a real database, wrap changes in a transaction:
BEGIN;... thenCOMMIT;to keep them orROLLBACK;to undo.
In the exercises below you work on a private copy of the data each time, so you can experiment freely. In your own database, take a backup before you try anything risky.
You run the bookshop database. Rivers of Salt (id 5) is out of print, so you must delete it. You type the statement quickly before a meeting.
What do you type?
Add a new book: id 11, title 'Tide Tables', author 'R. Okafor', genre 'nonfiction', price 15.00, year 2024, stock 10. Then show its title and price.
Tables you can query
- books (id, title, author, genre, price, year, stock)
INSERT INTO books (id, title, author, genre, price, year, stock)
VALUES ;
SELECT title, price FROM books WHERE id = 11;Raise the price of every cooking book by 10 percent, rounded to 2 decimals. Then show the title and price of the cooking books.
Tables you can query
- books (id, title, author, genre, price, year, stock)
UPDATE books
SET price =
WHERE ;
SELECT title, price FROM books WHERE genre = 'cooking';Delete every book with no stock (stock equal to 0). Then show how many books are left, as left_over.
Tables you can query
- books (id, title, author, genre, price, year, stock)
DELETE FROM books WHERE ;
SELECT COUNT(*) AS left_over FROM books;Before you run UPDATE or DELETE
INSERT INTO t (cols) VALUES (...)adds rows;UPDATE t SET ... WHERE ...changes them;DELETE FROM t WHERE ...removes them.- Without WHERE, UPDATE and DELETE affect every row.
- Preview with a SELECT using the same WHERE before you change anything.
- Transactions (
BEGIN,COMMIT,ROLLBACK) and backups make mistakes recoverable.
Check your safety habits.
What does UPDATE books SET stock = 0; do?
- ASets stock to 0 for every book
- BDoes nothing without WHERE
- CSets it for the first row only
- DRaises an error
Which command undoes the changes since BEGIN?
- ACOMMIT
- BROLLBACK
- CUNDO
- DRESET
Which is the safest way to remove one specific book?
- A`DELETE FROM books;`
- B`DELETE FROM books WHERE id = 5;`
- C`DROP TABLE books;`
- D`UPDATE books SET stock = NULL;`
Put the safe workflow in order.
- ACheck the rows it returns
- BRun the DELETE with the same WHERE
- CWrite a SELECT with the WHERE condition
- DStart a transaction with BEGIN
- ECOMMIT once the result looks right
In an INSERT, the values follow the keyword .
Capitalisation, spacing and punctuation are ignored when grading.
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.