UPDATE Statement - SQL Query
Different possible ways to use UPDATE statement.
UPDATE statement is used to update the records present in the table.
Consider the following table 'Students'.
Studentid | Firstname | Lastname | Email | Phone |
---|
1 | Jaya | Singh | Jaya@tutorialride.com | null |
2 | Shruti | Shrabya | Shruti@tutorialride.com | null |
i) Update only one field.
Update the column Phone where condition is given as Stud_id = 1
ii) Update multiple fields.
Such as F_name, L_name, Phone where condition is given as Stud_id= 2
To update column Phone where Stud_id= 1 the query should be written as:
UPDATE Students SET Phone = 34542892 WHERE Stud_ID = 1
Note: The 'SET' specifies the variable names to be updated.
Result:
Studentid | Firstname | Lastname | Email | Phone |
---|
1 | Jaya | Singh | Jaya@tutorialride.com | 34542892 |
2 | Shruti | Shrabya | Shruti@tutorialride.com | null |
So to update multiple fields where Stud_id= 2.
The query should be written as:
UPDATE Students SET F_name = 'Surendra', L_name = 'Morya', Phone = 92384900, email = 'Surendra@tutorialride.com' WHERE Stud_ID = 2;
Result:
Studentid | Firstname | Lastname | Email | Phone |
---|
1 | Jaya | Singh | Jaya@tutorialride.com | 34542892 |
2 | Surendra | Morya | Surendra@tutorialride.com | 92384900 |