how to update existing data in MySQL database

How to UPDATE existing data in MySQL database?

In this tutorial, you will learn how to update existing data in a SQL database. Let’s see –

Here is some dummy existing data of the users table, and we will see how we can update one of those.

dummy existing data of the SQL users table

SQL query syntax for updating data

UPDATE `table_name` SET column1='new value', column2='new value' ... WHERE condition;
table_nameWhich table’s data do you want to update? In my case table name is – users
column1Name of the column whose value want to update.
column2You can update multiple columns by separating them with a comma.
conditionYou have to define the condition, otherwise, it will update all the rows.

Example with explanation

UPDATE `users` SET `name`='Rahul' WHERE `user_id`=1; 
    UPDATE `users`update the users table.
    SET `name`=’Rahul’set Rahul in the name column.
    WHERE `user_id`=1Update the row in which the user_id column is equal to 1

    Result: after updating

    result after updating data in mysql database

    Example of updating multiple columns

    UPDATE `users` SET `name`='Ritesh', `email`='[email protected]' WHERE `user_id`=3; 
    result after updating multiple columns at once