Updating Data
What is Updating Data in PostgreSQL?
Updating data in an SQL database table is changing (aka modifying) values in all, some or one of the already existing table cells of a given column or given columns.
Updating All Cells of a Given Column
It is possible to update all cells of a given column simultaneously.
UPDATE users
SET role = 'client';
The above command updates the role
column in all rows of the table users
to the string 'client'.
Updating Only Specific Cells of a Given Column
It is possible to update only cells belonging to rows meeting specific criteria. The criteria (conditions) are provided using the keyword WHERE
appended with a boolean expression which can reference the updated table columns.
UPDATE horses
SET free_roaming = TRUE
WHERE status = 'mustang';
The above command updates the column free_roaming
to TRUE
for all rows in the table horse
where status
equals mustang
. The remaining rows remain unaffected.
UPDATE users
SET role = 'admin'
WHERE id = 42;
The above command updates the column role
in only the row with the id of 42
.