Sub topic 4
Changing rows with UPDATE
This chapter assumes that you can write SELECT statements with WHERE clauses and understand primary keys, foreign keys, NOT NULL, and common Oracle data types. The examples use the emp_demo table:
UPDATE emp_demo
SET salary = salary * 1.05
WHERE dept_id = 10;UPDATE changes existing rows. The SET clause describes the new values. The WHERE clause chooses which rows receive the change. Oracle evaluates the expression separately for each selected row, so salary * 1.05 uses each employee's current salary.
The basic UPDATE structure
An UPDATE statement has three important parts:
| Part | Purpose | Example |
|---|---|---|
| Target table | Names the table to change | `emp_demo` |
| `SET` clause | Assigns new column values | `salary = salary + 500` |
| `WHERE` clause | Limits the affected rows | `dept_id = 20` |
You can update more than one column in the same statement:
UPDATE emp_demo
SET salary = salary + 500,
status = 'ACTIVE'
WHERE emp_id = 1;A column assignment can use another column, a literal value, or a function. If an expression produces NULL, the update can fail when the column has a NOT NULL constraint.
Preview the affected rows
Before changing data, run the same condition in a SELECT. This confirms which rows the update will affect:
SELECT emp_id, emp_name, salary
FROM emp_demo
WHERE dept_id = 10;
UPDATE emp_demo
SET salary = salary * 1.05
WHERE dept_id = 10;If you omit WHERE, Oracle updates every row:
UPDATE emp_demo
SET status = 'ACTIVE';This is valid SQL, but it may be a serious data error. A condition based on a key is usually safest when you intend to change one row.
Using a subquery
A subquery can select rows from another table. This example gives a raise to employees in the department named Sales:
UPDATE emp_demo e
SET e.salary = e.salary * 1.05
WHERE e.dept_id IN (
SELECT d.dept_id
FROM dept_demo d
WHERE d.dept_name = 'Sales'
);The subquery returns department IDs. The outer update changes employees whose dept_id is in that result.
An update can also assign a value returned by a correlated subquery, but the subquery must return at most one row for each target row. If it returns several rows, Oracle raises ORA-01427.
Transactions and row counts
An update is part of the current transaction. Use COMMIT to make it permanent or ROLLBACK to undo uncommitted changes:
SAVEPOINT before_raise;
UPDATE emp_demo
SET salary = salary + 500
WHERE dept_id = 20;
ROLLBACK TO SAVEPOINT before_raise;
COMMIT;The savepoint allows the update to be undone while preserving earlier work in the transaction.
The database also checks constraints during the update. Changing a foreign-key value to a nonexistent department causes a foreign-key error. Changing a primary-key value to one already in use causes a unique-constraint error.
Exercise
What is wrong with this statement if only employee 1 should receive a raise? UPDATE emp_demo SET salary = salary + 100;
Show answer
It has no WHERE clause, so it raises every employee's salary. Use WHERE emp_id = 1.