How to update data in mysql with example

If you want to change or edit data/value in mysql tables than follow this steps.

homeno homename state country
206 chiguru karnataka india
20 NULL kerala india
28 krish goa India

Consider above example, for homeno 20 we forget to add homename, if you want add homename as ram than mysql common will be

UPDATE homedetails SET homename='ram' WHERE homeno=20;

homeno homename state country
206 chiguru karnataka india
20 ram kerala india
28 krish goa India

where homedetails is table name

Syntax for update

Syntax:-

UPDATE table_name SET col_name=data WHERE position_of_row;

Example:-

1. Editing data

Here we editing homename ram to sri ram

UPDATE homedetails SET homename='sri ram' WHERE homeno=20;

homeno homename state country
206 chiguru karnataka india
20 sri ram kerala india
28 krish goa India

2. If homeno is same for 2 row

homeno homename state country
206 chiguru karnataka india
20 sri ram kerala india
20 krish punjab pakistan

Here you want to change state kerala to karnataka than

UPDATE homedetails SET state='karnataka' WHERE homeno=20 and country='india';

homeno homename state country
206 chiguru karnataka india
20 sri ram karnataka india
20 krish punjab pakistan

3. Multiple data update

homeno homename state country
206 chiguru karnataka india
27 sri ram karnataka india
20 krish punjab pakistan

Here we changing punjab to goa and pakistan to india

UPDATE homedetails SET state='goa', country='india' WHERE homeno=20;

homeno homename state country
206 chiguru karnataka india
27 sri ram karnataka india
20 krish goa india

4. What will happened if we don`t use WHERE

UPDATE homedetails SET state='goa';

homeno homename state country
206 chiguru goa india
27 sri ram goa india
20 krish goa india

It changes all row to goa in country column

Previous Code:-
Insert data into tables

List Code:-
Mysql Codes

Next Code:-
Add multiple row

Leave reply

Add your comments here

Back to Top