Sub topic 7
DELETE versus TRUNCATE
This chapter assumes you can write SELECT statements with WHERE clauses and understand primary keys, foreign keys, and transactions. Both DELETE and TRUNCATE remove rows, but they have different transaction, locking, and storage behavior.
DELETE removes rows as DML
DELETE is a data manipulation language (DML) statement. It can remove selected rows or every row in a table.
DELETE FROM emp_demo
WHERE status = 'INACTIVE';
ROLLBACK;The WHERE clause limits the rows affected. Because DELETE is transactional, ROLLBACK restores the deleted rows if you have not committed. You can also use a savepoint to undo only part of a transaction.
To remove every row with DELETE, omit the WHERE clause:
DELETE FROM emp_stage;
COMMIT;Oracle processes the deletion as row-level DML. It records undo information, maintains indexes, checks referential constraints, and fires applicable delete triggers. A large delete can therefore generate substantial undo and redo and can take time.
A missing WHERE clause is a common error. Before running a destructive delete, run the same condition as a query:
SELECT COUNT(*)
FROM emp_demo
WHERE status = 'INACTIVE';TRUNCATE removes all rows as DDL
TRUNCATE TABLE removes every row from a table. It does not accept a WHERE clause.
TRUNCATE TABLE emp_stage;TRUNCATE is a data definition language (DDL) statement. Oracle commits the current transaction before the statement and commits the truncate operation afterward. You cannot roll it back with ROLLBACK.
It also resets the table's high water mark, which is the point Oracle uses to track blocks that have contained table data. This usually makes a later full-table scan avoid previously used empty blocks. Storage behavior can be controlled with options such as DROP STORAGE or REUSE STORAGE, depending on your space-management goal.
TRUNCATE is often faster than deleting every row because it does not generate row-by-row undo in the same way. However, it does not fire delete triggers. It also requires stronger privileges and has stricter foreign-key restrictions. An enabled foreign key can prevent truncating a referenced parent table, even when the child currently has no rows.
| Operation | Selective rows | Rollback possible | Delete triggers | Typical use |
|---|---|---|---|---|
| `DELETE` | Yes | Yes, before commit | Yes | Controlled transactional removal |
| `TRUNCATE` | No | No | No | Fast emptying of a staging table |
Choosing between them
The decision can be viewed as a safety-versus-speed choice:
For a parent table, check foreign-key dependencies before either operation. DELETE may fail with ORA-02292 when child rows exist. TRUNCATE may fail with ORA-02266 when an enabled foreign key references the table.
Exercise
You need to empty a temporary staging table after a successful load. The operation must be fast, must not be reversible, and no delete trigger is required. Which statement should you choose, and what limitation must you remember?
Show answer
Choose TRUNCATE TABLE staging_table. It removes all rows, cannot use a WHERE clause, commits implicitly, and cannot be rolled back.