Changing and removing rows
After this: Change and remove rows with UPDATE and DELETE, and read how many rows changed.
On this page · 7 sections · 3 checks
Chloe's History essay has been marked: 68. Her enrolment row is already in the table, with NULL as the mark. You do not want a second row for the same course; you want to change the one that is there. In lesson 1 you added rows. This lesson changes rows and removes them, using the WHERE you already know from SELECT to pick which ones.
Changing rows
The SQL word for changing rows is . Read Chloe's change in three parts:
UPDATE enrolmentsnames the table.SET mark = 68says which column gets which new value.WHERE student_id = 3 AND course_id = 3picks the rows to change: Chloe (student 3) in History (course 3). Only rows that pass the WHERE are changed, as only rows that pass it are shown by a SELECT.
The list under the result says "1 row changed". That count is how the database tells you what the statement did, and it is worth reading every time. The SELECT after it shows the mark now reads 68.
When no row passes the WHERE, nothing changes, and that is not an error:
"0 rows changed". If you expected to change a row and read 0, check your WHERE: a misspelt name changes nothing, without any warning.
The UPDATE with no WHERE
The WHERE is optional. Without it, every row passes, so every row changes:
"6 rows changed". All six students now live in York.
A rule stops the whole statement
At the end of the school year, every student moves up a year. Try it:
It fails with CHECK constraint failed: year BETWEEN 1 AND 3. Dev and Femi are in year 3, and year 4 breaks the rule. But what about Ana, Ben and the others, whose new years would be fine?
None of them changes. One statement is all-or-nothing: if any row breaks a rule, the database undoes the rows it had already changed, and the table is exactly as it was before the statement.
The Run button stops at the first error, so you cannot read the table after it here. A program that catches the error and then reads the table sees this:
UPDATE students SET year = year + 1;SELECT name, year FROM students;| name | year |
|---|---|
| Ana | 2 |
| Ben | 1 |
| Chloe | 2 |
| Dev | 3 |
| Eve | 1 |
| Femi | 3 |
Ana is still in year 2. Half a year-change, with some students moved up and some not, never happens.
Removing rows
The SQL word for removing rows is . It has no column list, because it removes whole rows. Remove every enrolment with a mark under 50:
One row passes the WHERE, Dev's Physics with 47, so "1 row changed" and 9 enrolments are left. Chloe's History row stays: its mark is NULL, and NULL is not "under 50", so it does not pass.
Rows that other rows point at
Now try to remove Ana, student 1:
It fails with FOREIGN KEY constraint failed. Ana has three enrolments, and each one says student_id = 1. That column is a : it must hold the id of a real student. Removing Ana would leave three enrolments pointing at nobody, so the database refuses.
Eve has no enrolments, so nothing points at her, and she can go:
To remove Ana, you would delete her enrolments first, then her row. The final challenge does exactly that for a new student.
Check yourself
Course 1 (Maths) has four enrolments: Ana 72, Ben 58, Chloe 85 and Femi 91. How many rows does this UPDATE change?
UPDATE enrolments SET mark = mark + 5WHERE course_id = 1;Someone means to move Ben to Hull, but runs UPDATE students SET city = 'Hull'; with no WHERE. What happens?
This fails with CHECK constraint failed: year BETWEEN 1 AND 3, because Dev and Femi are in year 3. How many students moved up a year?
UPDATE students SET year = year + 1;Honesty notes
- Change rows with UPDATE table SET column = value WHERE condition; only the rows that pass the WHERE change.
- Read the rows-changed count after every change: 1 for Chloe's mark, 0 when no row matches, which is not an error.
- Say that an UPDATE or DELETE with no WHERE changes or removes every row in the table.
- Say that one statement is all-or-nothing: when Dev and Femi could not move to year 4, nobody moved up.
- Remove rows with DELETE FROM table WHERE condition, and say why a row that other rows point at (a foreign key) cannot be removed first.