Modifying data
3. Modify multiple rows

Instruction

Excellent! When you write an UPDATE query, the database modifies all rows for which the WHERE condition is true. You can modify many rows with one UPDATE command. You can build up your WHERE condition with AND, OR and NOT. You can also use any other condition you would use in a SELECT command.

To change the age value for all users with the id lower than 4, you can run the command:

UPDATE user SET age = 17 WHERE id < 4; 

The database always tells you how many rows have actually been modified. In the previous examples, it only modified a single row each time. Always check the number of modified rows to see if your modification did what you had expected it to.

Exercise

Happy hours at our restaurant! Change the price of all main courses to 20.

Stuck? Here's a hint!

Type

UPDATE dish 
SET price = 20 
WHERE type = 'main course';