SQL UPDATE
SQL UPDATE
This statement is to modify/Update the existing records in a table.
Student table:
Regno |
Name |
Dob |
Contact |
state |
1001 |
funda |
20-june-2000 |
9696985457 |
karnataka |
1002 |
of |
18-march-2003 |
8585969674 |
haryana |
1003 |
web |
12-may-1998 |
8256968475 |
karnataka |
1004 |
it |
24-nov-1994 |
9785956748 |
goa |
Syntax:
UPDATE table_Name set Column1 = value1,Column2 = value2.. WHERE Condition;Example :
Update student set name='om', contact='8596874858' WHERE regno=1001;
The OUTPUT for the above query:
Regno |
Name |
Dob |
Contact |
state |
1001 |
om |
20-june-2000 |
8596874858 |
karnataka |
1002 |
of |
18-march-2003 |
8585969674 |
haryana |
1003 |
web |
12-may-1998 |
8256968475 |
karnataka |
1004 |
it |
24-nov-1994 |
9785956748 |
goa |
UPDATE MULTIPLE RECORDS
Here, Where clause decides how many rows or records will get updated.
Following statement will updated the name to om where state is karnataka
Example:
UPDATE student set name='om' WHERE state='Karnataka';
The OUTPUT for the above query:
Regno |
Name |
Dob |
Contact |
state |
1001 |
om |
20-june-2000 |
9696985457 |
karnataka |
1002 |
of |
18-march-2003 |
8585969674 |
haryana |
1003 |
om |
12-may-1998 |
8256968475 |
karnataka |
1004 |
it |
24-nov-1994 |
9785956748 |
goa |
When you update any record, you must be careful and use the WHERE condition. If you dont use the WHERE condition all the records will get updated.
Example:
UPDATE Student SET state='karnataka';
The OUTPUT for the above query:
Regno |
Name |
Dob |
Contact |
state |
1001 |
funda |
20-june-2000 |
9696985457 |
karnataka |
1002 |
of |
18-march-2003 |
8585969674 |
karnataka |
1003 |
web |
12-may-1998 |
8256968475 |
karnataka |
1004 |
it |
24-nov-1994 |
9785956748 |
karnataka |
IF you want to update a single record use Where condition.