Oracle Database SQL (1Z0-071) free question 3 / 10 · Domain 12: Use DDL to manage tables and their relationships
The employees table has 5 rows. The following statements were run in order against a regular table. What value does the last SELECT return? TRUNCATE TABLE employees; ROLLBACK; SELECT COUNT() FROM employees;
- 5: ROLLBACK brings the rows back
- 0: the removal is not undone and the table stays empty
- An error: the table is unreachable after ROLLBACK
- 4: the last row that was deleted comes back
Show the answer and explanation
Answer: B
Why this is correct
"TRUNCATE TABLE" in the SQL Language Reference says that you cannot roll back a TRUNCATE TABLE statement, and that you cannot use Flashback Table to retrieve the contents of a table that has been truncated. The table is still empty after ROLLBACK, so COUNT() is 0.
Why the other options are wrong
"4: the last row that was deleted comes back" … Rows do not come back in part. TRUNCATE removes every row, and its result cannot be undone.
"5: ROLLBACK brings the rows back" … A TRUNCATE TABLE statement cannot be undone with ROLLBACK. Rows can be brought back by ROLLBACK after a DELETE statement.
"An error: the table is unreachable after ROLLBACK" … The table can still be referenced after ROLLBACK. In this example the count is taken in the emptied state, which gives 0.
Key takeaway
DELETE can be undone with ROLLBACK, but TRUNCATE cannot. TRUNCATE is a DDL statement, and it is committed when it runs. Trace the example: TRUNCATE removes the 5 rows and commits at once, ROLLBACK then has nothing to undo, and COUNT() sees an empty table. If the same rows had been removed with DELETE FROM employees, the ROLLBACK would have restored all 5 rows. That difference is the reason to choose one statement or the other.