actualizacion-datos-update-sql

Data Update with UPDATE in SQL

  • 4 min

In the previous article, we learned to create data with INSERT INTO. Now we are going to see how to modify it.

Because data is not static. People move house, products change price, and order statuses progress.

The statement responsible for this task is UPDATE.

UPDATE is a powerful tool, but also one of the most dangerous in SQL. A poorly done INSERT only adds garbage. But a poorly done UPDATE can wipe out all your data irreparably.

Basic Syntax

The structure of an update statement has three key parts:

UPDATE TableName
SET Column = NewValue
WHERE Condition;
Copied!
  1. UPDATE: Which table we want to modify.
  2. SET: Which columns we want to change and what new value we give them.
  3. WHERE: Which rows the change affects.

For example, imagine the customer with ID 105 calls us because they have changed their phone number.

UPDATE Customers
SET Phone = '600-123-456'
WHERE CustomerID = 105;
Copied!

SQL Server will find the row where the ID is 105, change the value of the Phone column, and leave the rest of the data in that row intact.

The Danger of Forgetting the WHERE

This is the most important advice we will give you in this module: Be careful when executing an UPDATE without WHERE.

If you forget the WHERE condition (copying from here to there, in a hurry, you don’t notice, you hit execute), SQL Server will update ALL the rows in the table.

-- ☠️ DANGER! This will set the same phone number for ALL your customers
UPDATE Customers
SET Phone = '600-123-456';
Copied!

There is no “Are you sure?” message. You are going to delete all your data, destroy the planet, and get fired. No.

SQL Server will simply execute it and tell you: “5000 rows affected”. And you will be left with a bewildered look, not knowing what to do.

The pre-SELECT trick: Before executing an UPDATE, always write a SELECT with the same WHERE to verify that you are going to modify exactly the records you think.

Updating Multiple Columns

We can modify several fields of the same row in a single statement, separating them by commas.

-- We update the customer's address and city
UPDATE Customers
SET Address = 'Calle Nueva 123',
    City = 'Barcelona',
    PostalCode = '08001'
WHERE CustomerID = 105;
Copied!

Update with Expressions

We are not limited to setting fixed values (literals). We can use the current value of the column to calculate the new value. This is extremely useful for increments or percentage changes.

Example: Price increase due to inflation We want to increase the price of all products by 10%.

UPDATE Products
SET Price = Price * 1.10;
-- Here we intentionally do NOT use WHERE, because we want to affect all
Copied!

Example: Stock correction There has been a sale and we subtract 5 units from the current stock.

UPDATE Inventory
SET Quantity = Quantity - 5
WHERE ProductID = 20;
Copied!

UPDATE with JOIN (Exclusive to T-SQL)

Sometimes, the condition for updating a table depends on information that is in another table.

The ANSI SQL standard would force us to use subqueries, but T-SQL allows a very convenient proprietary syntax: using JOIN inside the UPDATE.

For example, we want to increase the price by 5%, but only for products in the “Electronics” category.

UPDATE P
SET P.Price = P.Price * 1.05
FROM Products P
INNER JOIN Categories C ON P.CategoryID = C.CategoryID
WHERE C.Name = 'Electronics';
Copied!

Notice the syntax:

  1. We specify the alias P in the UPDATE.
  2. We use FROM and JOIN to relate Products to Categories.
  3. We filter by the category name.

What happens if an UPDATE fails?

Just like with INSERT, an UPDATE can fail if the new value violates a constraint:

  • If you try to change an ID to one that already exists (PK violation).
  • If you try to put a NULL in a NOT NULL column.
  • If you break a CHECK rule (e.g., setting a negative price).

If an error occurs, the operation is canceled and the data returns to its original state (atomicity).