SQL - UPDATE Statement
The SQL UPDATE statement is used to modify the existing records in a table. A SQL WHERE clause can be used with the UPDATE statement to update the selected rows, otherwise all the rows will be assigned the updated value.
Syntax
The syntax for using UPDATE statement is given below:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition(s);
Example:
Consider a database containing a table called Employee with the following records:
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
-
To update the City and Salary of an employee whose EmpID is 5, the SQL query is:
UPDATE Employee SET City = 'Mumbai', Salary = 2900 WHERE EmpID = 5; --see the update SELECT * from Employee;
Now the Employee table will contain following records:
EmpID Name City Age Salary 1 John London 25 3000 2 Marry New York 24 2750 3 Jo Paris 27 2800 4 Kim Amsterdam 30 3100 5 Ramesh Mumbai 28 2900 6 Huang Beijing 28 2800 -
Similarly, to update the Salary of John who lives in London, the SQL query is:
UPDATE Employee SET Salary = 3200 WHERE Name = 'John' AND City = 'London'; --see the update SELECT * from Employee;
Now the Employee table will contain following records:
EmpID Name City Age Salary 1 John London 25 3200 2 Marry New York 24 2750 3 Jo Paris 27 2800 4 Kim Amsterdam 30 3100 5 Ramesh Mumbai 28 3000 6 Huang Beijing 28 2800