We can change the values stored in our tables using the update command. As insert and delete, also update has to be confirmed with a commit, or could be canceled with a rollback.

Say that we want to change the price of the existing stock of blueberries:

update doughnut
price = 3.09
where name = 'Blueberry';

We asks to the database to set the price to a (new) value for all the rows having the specified name. If we don't use the "where" clause, we have the unpleasant (if not required) effect to change the price to all the items in the table.

We can change more than one field at once. Say that we want to reset price and stock for the blueberries:

update doughnut
set stock = 39, price = 3.19
where name = 'Blueberry';

If we want to increase the price of a fixed amount (say 0.20) to both blueberry and raspberry we can do it in one shot:

update doughnut
set price = price + 0.20
where name = 'Blueberry' or name = 'Raspberry';

Post written while having fun reading Head First SQL

No comments:

Post a Comment