mysql  update statement

the MySQL update statement is used to updating the data in a table.

the update statement in MySQL is used to update existing data in tables.it can be used to change column values of a single row, group of rows or all rows in a tables.

mysql update syntax

       update       table_name1, table_name2, ...
           
       set            column_name1  =  expression_value,
                 
                        column_name2  =  expression_value, ...
                 
       where        condition ;                 
  • you can change the data of multiple tables using a single update statement
  • the set clause determines the column names of the table and the new values.
       new values could be literal, result of expression or subqueries

you can also use a limit clause to limit the number of rows to be updated.

the where clause determines which rows will be updated.it is an optional element of the update statement.

note : if a where clause is not provided, all rows would be updated with the same changes.


below is a selection from the "user" table:

useridfirstnamelastnameemailcity
1sandeepkumarnehrasandeep76@gmail.comindia
2shyamdhakashyamkumar12@gmail.comjaipur
3ankitnehraankit11@gmail.comuae

mysql  update statement example

we can update current email and lastname to the new email and lastname using the update statement.

 
               update      user          
               
               set        email = 'kumarshyam@gmail.com',
                
                            lastname = 'bond' 
      
               where    userid = 2 ;

output : look updating result

useridfirstnamelastnameemailcity
1sandeepkumarnehrasandeep76@gmail.comindia
2shyambondkumarshyam@gmail.comjaipur
3ankitnehraankit11@gmail.comuae

update multiple tables

 
               update      user,  person        
               
               set        email = 'kumarshyam@gmail.com',
                
                            lastname = 'bond' 
      
               where    userid = 2 ;

output : try yourself, please first create user, person table .