Updating

The UPDATE statement is used to modify the existing records in a table:

mysql> UPDATE customers SET first_name='Rajiv', country='UK' WHERE id=4;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0

WHERE: This is the clause used for filtering. Whatever condition(s) are issued after the WHERE clause are evaluated and the filtered rows are updated.

The WHERE clause is mandatory. Failing to give it will UPDATE the whole table. It is recommended to do data modification in a transaction, so that you can easily rollback the changes if you find anything wrong. You can refer to Chapter 5, Transactions to learn more about transactions.

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.