Sub topic 6
Removing rows with DELETE
Assume emp_demo already exists and contains employee data. You should know basic SELECT statements, WHERE clauses, and primary and foreign key constraints.
DELETE removes rows from a table. It changes the current transaction, but the change is not permanent until you run COMMIT. Before committing, you can use ROLLBACK to undo the deletion.
Delete selected rows
A DELETE statement normally includes a WHERE clause that identifies the rows to remove:
DELETE FROM emp_demo
WHERE status = 'INACTIVE';
COMMIT;This removes every inactive employee. The condition is evaluated for each row. If five rows match, five rows are deleted.
Always test the condition with SELECT first:
SELECT emp_id, emp_name, status
FROM emp_demo
WHERE status = 'INACTIVE';If the result is correct, reuse the same WHERE clause in DELETE.
A DELETE without a WHERE clause removes every row in the table:
DELETE FROM emp_demo;The table remains available, and its definition and indexes remain. However, restoring the data may be difficult, so treat this statement as a destructive operation.
Delete using a subquery
A subquery can select related rows. For example, this removes employees whose department is no longer listed in dept_demo:
DELETE FROM emp_demo e
WHERE NOT EXISTS (
SELECT 1
FROM dept_demo d
WHERE d.dept_id = e.dept_id
);The subquery is correlated because it refers to e.dept_id from the row currently considered by the outer DELETE. NOT EXISTS is often safer than NOT IN, because NOT IN can produce unexpected results when its subquery returns NULL.
You can also protect a deletion by requiring a matching archive row:
DELETE FROM emp_demo e
WHERE e.status = 'INACTIVE'
AND EXISTS (
SELECT 1
FROM emp_archive a
WHERE a.emp_id = e.emp_id
);
COMMIT;This deletes an inactive employee only when the employee's ID is already present in the archive.
Foreign keys and related rows
A foreign key prevents deletion of a parent row while child rows still refer to it. For example, deleting a department that still has employees can raise ORA-02292: integrity constraint violated - child record found.
Choose an intentional order:
- Delete or move the child rows.
- Delete the parent row.
- Commit the transaction.
The exact behavior can differ when the foreign key uses ON DELETE CASCADE. With that option, deleting a parent can automatically delete its children, so verify the constraint definition before using it.
DELETE versus TRUNCATE
Both commands remove data, but they have different behavior.
| Operation | Transaction control | Can use WHERE | Typical use |
|---|---|---|---|
| `DELETE` | Can roll back before commit | Yes | Remove selected rows |
| `TRUNCATE` | Implicitly commits | No | Empty an entire table quickly |
Use TRUNCATE TABLE emp_demo only when you intentionally want to remove all rows and accept its transaction behavior. It also cannot be used when enabled foreign keys reference the table in ways that prevent truncation.
Exercise
Which statement removes only inactive employees while keeping the transaction reversible?
Show answer
DELETE FROM emp_demo WHERE status = 'INACTIVE'; Do not run COMMIT until the result has been checked.