# UPDATE: updating rows
UPDATE command allows us to modify one or more columns in zero or more rows in a table.
Here's how you could change Smith's surname to be
UPDATE users SET surname = 'Wick' WHERE surname = 'Smith';
A bit of a different structure than what we've seen before. There are four main parts to this query:
UPDATE users: the table we want to change.
SET surname = 'Wick': the column we want to change, and the new value we want in it.
WHERE surname = 'Smith': this selects the rows that will be changed.
- The query-ending semicolon:
As previously, you can use multiple filters by using
OR with more comparisons inside the
# Updating multiple columns
To update multiple columns it doesn't make sense to use
OR, as you might take that to mean "update one or the other". Instead, we use comma separation when we want to change multiple columns:
UPDATE users SET first_name = 'John', surname = 'Wick' WHERE surname = 'Smith';
Note that I've split that query into multiple lines for readability.
# Comparison operators in
It also doesn't make sense to use operators like
!= in the
SET clause. Don't do this:
... SET first_name != 'John' ...
Reserve those comparisons for the