CodeBlanks Logo Icon CodeBlanks

ROLLBACK

Undo on error with ROLLBACK.

ROLLBACK

ROLLBACK undoes a transaction. All changes before COMMIT are discarded.

BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;

The delete is undone.

ROLLBACK — details

ROLLBACK preserves data on error. It undoes mistaken deletes or updates.

For example, if you accidentally ran a DELETE without WHERE and the transaction is still open, ROLLBACK brings back every deleted row.

Practice ROLLBACK

Write a DELETE and undo it with ROLLBACK.

Starter code

query.sql
-- undo
BEGIN;
DELETE FROM orders WHERE id = 1

Sample solution

query.sql
BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;

Expected output

Transaction geri alındı

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 ROLLBACK: match the terms

Term Meaning
ROLLBACK undo
COMMIT commit
undo recovery
error protection

2 Add ROLLBACK

Question
BEGIN;
DELETE FROM orders WHERE id = 1;
____;
Answer
BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;

Explanation: ROLLBACK is the right answer.

3 What does ROLLBACK do?

  • Undoes the changes Correct answer
  • Makes them permanent
  • Starts a transaction
  • Deletes them

Explanation: ROLLBACK undoes changes.

All lessons in Intermediate