SQL UPDATE Statement
The SQL UPDATE statement is used to update or modify a record that already exists in a table.We can use WHERE clause with the update statement.
UPDATE Statement Syntax :
The syntax of the UPDATE statement is −
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
In this, the value of column 1 will be modify to value 1, column 2 to value 2 and similar columnsN to valueN.
UPDATE Statement Example :
Consider the Customer table with the following records -
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
2 | Vivaan | 25 | Mumbai | 30000 |
3 | Reyansh | 28 | Chennai | 35000 |
4 | Muhammad | 24 | Udaipur | 50000 |
5 | Sai | 30 | Mumbai | 27000 |
TEST CASE 1 :- The following code is an example, which would modify the CustomerName and Address fields from Customer table where CustomerID = 4.
UPDATE Customer SET CustomerName = 'Aadesh', Address= 'Mysore' WHERE CustomerID = 4;
The result for the respective sql query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
2 | Vivaan | 25 | Mumbai | 30000 |
3 | Reyansh | 28 | Chennai | 35000 |
4 | Aadesh | 24 | Mysore | 50000 |
5 | Sai | 30 | Mumbai | 27000 |
TEST CASE 2:- The following code is an example, which would modify the CustomerName fields from Customer table where Address='Mumbai'
UPDATE Customer SET CustomerName='Gaurav' WHERE Address='Mumbai';
The result for the respective sql query is as follows −
CustomerID | CustomerName | Age | Address | CustomerSalary |
---|---|---|---|---|
1 | Aarav | 28 | Udaipur | 28000 |
2 | Gaurav | 25 | Mumbai | 30000 |
3 | Reyansh | 28 | Chennai | 35000 |
4 | Muhammad | 24 | Udaipur | 50000 |
5 | Gaurav | 30 | Mumbai | 27000 |
Visit :
Discussion