4 5 6 7 8

Update statement

13

To modify data in a table, you use update statement.

update `user` set `name` = 'Tiffany';
Query OK, 4 rows affected (0.06 sec)
Rows matched: 4  Changed: 4  Warnings: 0

Let's see what happened:

select * from `user`;
+----+---------+-----------------------+--------------+---------------------+
| id | name    | email                 | password     | registered          |
+----+---------+-----------------------+--------------+---------------------+
|  1 | Tiffany | silvia@gmail.com      | silvia1990   | 2019-05-12 00:00:00 |
|  2 | Tiffany | angel@springville.com | 1PzIjSWmj3   | 2020-02-07 00:00:00 |
|  3 | Tiffany | carolina@studio.org   | funnybunny95 | 2020-06-12 00:00:00 |
|  4 | Tiffany | michael@mail.com      | 84738257342  | 2020-09-24 00:00:00 |
+----+---------+-----------------------+--------------+---------------------+
4 rows in set (0.00 sec)

Oh, all names were set to Tiffany! Obviously, that's not what we want. We should specify a particular record instead. To do that we should filter the record we're interested in with where clause as we did before with select statement:

update `user` set `name` = 'Tiffany' where id = 2;

Now only a single row is updated. In the example above we modify just a single field which is user's name. You can modify multiple fields at once like that:

update `user` set `name` = 'Tiffany', `email` = 'tiffany@mail.org', `password` = 'kitty01234' where `id` = 2;
Rate this post:
Share this page:

Learning plan

4. CRUD
Create, read, update, delete
How to create one or multiple records in a table
How to query records from a table
7. Update statement
How to update previously created data in a table
How to delete data from a table