This section is from the "Practical PostgreSQL" book, by John Worsley and Joshua Drake. Also available from Amazon: Practical PostgreSQL.
You can also change the value of data within existing rows using the UPDATE command. Suppose the values for the suggested price column from the publish table needs to have updated prices. This adds $2.00 to every book price:
Example 4-31. Using UPDATE
UPDATE publish SET sugg_price = sugg_price + '2.00';
The publish table now contains these prices:
isbn | publ_name | sugg_price ---------+------------------+------------ 214872 | Young Readers | $8.00 355184 | ABC Books | $18.85 602071 | DEF Books | $6.55 676883 | Reading Rainbow | $15.29 2567841 | ODG Books | $14.99 8741205 | Children's Books | $21.99 (6 rows)
The syntax for UPDATE is:
UPDATE tablename SET expression WHERE condition ;
The update command can change the values for the entire column, and also allow specified values that meet certain conditions to be changed. The values that meet the condition in the WHERE statement will be changed to the SET value.
You can update more than one column values at a time by using a comma delimited list after specifying the SET clause. For instance, this modifies the suggested price and publisher name:
Example 4-32. Using UPDATE on Several Columns
UPDATE publish SET sugg_price = '9.25', publ_name = 'DEF Books' WHERE isbn = 602071;
The resulting table is:
isbn | publ_name | sugg_price ---------+------------------+------------ 214872 | Young Readers | $8.00 355184 | ABC Books | $18.85 602071 | DEF Books | $9.25 676883 | Reading Rainbow | $15.29 2567841 | ODG Books | $14.99 8741205 | Children's Books | $21.99 (6 rows)
 
Continue to: