UPDATE statement in SQL
UPDATE
, is the DML command that allows you to update database records. In other words, UPDATE
allows you to modify the values of the table columns respecting the corresponding data type.
The syntax is as it follows:
UPDATE tableName SET columName = newValue WHERE conditions
As with the DELETE
statement, it is very important to take into account the WHERE
clause to prevent changes in the table from being propagated to all the records.
To better understand how it works, we are going to perform some examples of UPDATE
on the Customers
and Employees
tables.
Reviewing the records of the Customers
table, we notice that some of them were inserted with spelling errors that need to be corrected:
SELECT *
FROM Customers
WHERE customerId = 1;
customerId | name | lastName | address | bankAccount |
---|---|---|---|---|
1 | Janice | Haines | 4822 Radio Park Drive | 111222333 |
The last name Haynes was written with an i instead of a y:
UPDATE Customers
SET lastName = 'Haynes'
WHERE customerId = 1;
We run the query again and check that the record has been successfully updated:
customerId | name | lastName | address | bankAccount |
---|---|---|---|---|
1 | Janice | Haynes | 4822 Radio Park Drive | 111222333 |
Now we will see another example:
SELECT *
FROM Customers
WHERE customerId = 2;
customerId | name | lastName | address | bankAccount |
---|---|---|---|---|
2 | Mavin | Petitt | 2336 cottonwood Lane | 123456789 |
The last name Pettitt is missing a t, and the street name should start with a capital letter.
UPDATE Customers
SET lastName = 'Pettitt',
[address] = '2336 Cottonwood Lane'
WHERE [name] = 'Mavin';
We run the query again and check that the record has been successfully updated:
customerId | name | lastName | address | bankAccount |
---|---|---|---|---|
2 | Mavin | Pettitt | 2336 Cottonwood Lane | 123456789 |
In this case we have filtered by name
, because in our Customers
table there is only one Mavin. However, we want to stress that this would be bad practice, since if there was more than one Mavin, the change would affect all records. For this reason, it is essential to always filter by unique fields or, failing that, limit the required record with more conditions in the WHERE
clause.
Let’s move on to the following example:
SELECT *
FROM Orders
WHERE orderId = 1;
orderId | product | productDescription | price | orderDate | productQuantity | totalPrice | customerId |
---|---|---|---|---|---|---|---|
1 | Xiami Mi 11 | Smartphone | 286,95 | 2022-07-09 | 15 | 4304,25 | 3 |
The Xiaomi product is missing the O and we also want to change the orderDate
to 2022-07-10:
UPDATE Orders
SET product = 'Xiaomi Mi 11',
orderDate = '2022-07-10'
WHERE orderId = 1;
orderId | product | productDescription | price | orderDate | productQuantity | totalPrice | customerId |
---|---|---|---|---|---|---|---|
1 | Xiaomi Mi 11 | Smartphone | 286,95 | 2022-07-10 | 15 | 4304,25 | 3 |
To delete the value of a column, just do an UPDATE
and set the field to NULL
or ''
depending on its constraints.
UPDATE Orders
SET productDescription = NULL
WHERE orderId = 2;
orderId | product | productDescription | price | orderDate | productQuantity | totalPrice | customerId |
---|---|---|---|---|---|---|---|
2 | Play Station 5 | NULL | 549,95 | 2022-07-18 | 4 | 2199,8 | 2 |
It is also possible to use UPDATE
in tables joined by some JOIN
clause:
UPDATE C SET bankAccount= 111111111
FROM Customers C
INNER JOIN Orders O
ON C.customerId = O.customerId
WHERE O.price > 45
AND C.customerId = 4;
customerId | name | lastName | address | bankaccount |
---|---|---|---|---|
4 | Helen | Ward | 711 Hershell Hollow Road | 111111111 |
In this case, since there is a product in the Orders
table with a price greater than 45 euros for the client with customerId = 4
, the customers’s account has been correctly updated to the new value.
También es posible recuperar el valor de una SELECT
para actualizar un campo:
UPDATE Orders
SET productDescription = (SELECT productDescription
FROM Orders
WHERE orderId = 1)
WHERE orderId = 2;
orderId | product | productDescription | price | orderDate | productQuantity | totalPrice | customerId |
---|---|---|---|---|---|---|---|
2 | Play Station 5 | Smarthphone | 549,95 | 2022-07-18 | 4 | 2199,8 | 2 |