The UPDATE statement is a powerful tool for modifying existing records in your tables. In this article, we will break down how to use it correctly, from the basics to advanced techniques, so you can manage your data with confidence.
Understanding the UPDATE Statement in Oracle
The UPDATE statement is used to modify data in a database. In Oracle, the basic structure of an UPDATE command looks like this:
UPDATE nombre_tabla
SET columna1 = valor1,
columna2 = valor2
WHERE condition;
Basic Structure
To illustrate, suppose you have a table named empleados with the following columns: id, nombre, and salario. If you want to update an employee’s salary using a specific id, your UPDATE query could look like this:
UPDATE empleados
SET salario = 50000
WHERE id = 123;
In this example, the salario column for the employee with id 123 will be updated to 50,000.
Using Multiple Conditions
You can use multiple conditions in the WHERE clause to refine your search. For example, if you want to update the salary only for employees who work in a specific department and currently earn less than 40,000, you could write:
UPDATE empleados
SET salario = salario + 5000
WHERE departamento = 'Sales' AND salario < 40000;
This command will increase the salary by 5,000 for all employees in the sales department whose salary is below 40,000.
Intermediate UPDATE Techniques
Updating with Subqueries
Subqueries allow you to update one table based on the results of another query. Suppose you have two tables, empleados and departamentos, and want to update the salary of every employee in a department based on that department’s budget. You could do the following:
UPDATE empleados e
SET e.salario = e.salario * 1.10
WHERE e.departamento_id IN (
SELECT d.id
FROM departamentos d
WHERE d.presupuesto > 100000
);
This example increases the salary of employees in departments whose budget exceeds 100,000.
Updating Based on Aggregate Data
Sometimes, you need to update data based on aggregate results. Imagine that you want to adjust salaries based on the average salary in each department. You can do this in two steps:
First, retrieve the average salary for each department:
SELECT departamento_id, AVG(salario) AS salario_promedio
FROM empleados
GROUP BY departamento_id;
Then, use that information to update the salaries:
UPDATE empleados e
SET e.salario = e.salario * 1.05
WHERE e.salario < (
SELECT AVG(salario)
FROM empleados
WHERE departamento_id = e.departamento_id
);
Advanced UPDATE Options
Updating with a JOIN
Sometimes, you need to update a table based on data from another table. Use a JOIN in a subquery to do this. Suppose you want to adjust salaries based on data from a salary-adjustment table:
UPDATE empleados e
SET e.salario = e.salario + a.ajuste
FROM ajustes_salariales a
WHERE e.id = a.empleado_id;
Here, ajustes_salariales is a table containing salary adjustments for specific employees.
Using Transactions for Safety
To make sure your updates are safe and do not leave data in an inconsistent state, use transactions. An Oracle transaction can be handled with the COMMIT and ROLLBACK commands:
BEGIN
UPDATE empleados
SET salario = salario + 1000
WHERE departamento = 'IT';
-- If everything is correct, save the changes
COMMIT;
EXCEPTION
WHEN OTHERS THEN
-- If an error occurs, roll back the changes
ROLLBACK;
END;
With this technique, if something goes wrong, you can roll back the changes and preserve the integrity of your data.
Conclusion
Mastering the UPDATE statement in Oracle will allow you to manage your data effectively and precisely. From simple updates to advanced techniques such as subqueries and transactions, understanding these tools gives you finer control over your database. We hope this article has provided a solid foundation and encourages you to explore ways to optimize your queries and maintain data integrity.
Do you have a question or need more specific examples? Leave a comment and we will be happy to help.

