UPDATE:
Updating a table based on joining another table:
You can update data in a table based on data from another table using a JOIN
condition:
UPDATE person
SET state_code = cities.state_code
FROM cities
WHERE cities.city = person.city;
In this example, the person
table is updated based on a join with the cities
table, matching the city
columns.
Update all rows in a table: To update all rows in a table, you can use a simple syntax:
UPDATE person SET planet = 'Earth';
This will set the value of the planet
column to 'Earth' for all rows in the person
table.
Update all rows meeting a condition:
If you want to update rows that meet a specific condition, you can use the WHERE
clause:
UPDATE person SET state = 'NY' WHERE city = 'New York';
This will set the value of the state
column to 'NY' for all rows where the city
column is 'New York'.
Updating multiple columns in a table: You can update multiple columns in a table in the same statement:
UPDATE person
SET country = 'USA',
state = 'NY'
WHERE city = 'New York';
This will update the country
and state
columns for rows where the city
column is 'New York'.