# UPDATE: updating rows
The UPDATE
[1] 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 'Wick'
:
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 AND
and OR
with more comparisons inside the WHERE
clause.
# Updating multiple columns
To update multiple columns it doesn't make sense to use AND
and 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 SET
It also doesn't make sense to use operators like <
or !=
in the SET
clause. Don't do this:
... SET first_name != 'John' ...
Reserve those comparisons for the WHERE
clause!