SQL UPDATE Statement
SQL UPDATE statement is a type of statement in Structured Query Language (SQL) used to modify data in a database table. The UPDATE statement allows us to update one or more columns of a single row or multiple rows of data in a table.
Orders Table:
OrderID | CustomerID |
---|---|
1 | 10 |
2 | 20 |
3 | 30 |
Suppose we want to change the CustomerID of OrderID 2 from 20 to 25. In that case, we can use the following UPDATE statement:
1UPDATE Orders 2SET CustomerID = 25 3WHERE OrderID = 2;
Updated row data in Orders Table:
OrderID | CustomerID |
---|---|
1 | 10 |
2 | 25 |
3 | 30 |
As we can see from the output, the UPDATE statement successfully changes the CustomerID of OrderID 2 to 25.
In addition to updating a single column, the UPDATE statement can also be used to update multiple columns of data in a table using the following syntax:
1UPDATE Orders 2SET CustomerID = 35, OrderID = 4 3WHERE OrderID = 3;
Updated row data in Orders Table:
OrderID | CustomerID |
---|---|
1 | 10 |
2 | 25 |
4 | 35 |
As we can see from the output, the UPDATE statement successfully updates both the OrderID and CustomerID of OrderID 3.