ROLLBACK;
If everything is correct:
COMMIT;
This can be useful when performing potentially dangerous data modifications.
1️⃣8️⃣ A Safe Pattern for Data Changes
Before executing a large UPDATE or DELETE, analysts often first run a SELECT using the same condition.
Instead of immediately doing:
UPDATE customers
SET status = 'Inactive'
WHERE last_order_date < DATE '2024-01-01';
first check:
SELECT *
FROM customers
WHERE last_order_date < DATE '2024-01-01';
Then, where transaction support and operational rules permit:
BEGIN;
UPDATE customers
SET status = 'Inactive'
WHERE last_order_date < DATE '2024-01-01';
-- Verify the affected rows
COMMIT;
If something looks wrong:
ROLLBACK;
This is a valuable habit when working with production data.
1️⃣9️⃣ Transactions and Autocommit
Many database clients use an autocommit mode.
When autocommit is enabled, individual statements may be committed automatically.
For example:
UPDATE customers
SET status = 'Active'
WHERE customer_id = 101;
may be committed immediately.
That means you may not be able to simply run:
ROLLBACK;
after the statement has already been committed.
The exact behavior depends on:
• Database system
• Client/tool
• Connection settings
• Transaction configuration
Always understand the transaction mode before modifying production data.
20️⃣ Transactions and DDL
Statements such as:
• CREATE
• ALTER
• DROP
are DDL statements.
Their transaction behavior varies significantly across database systems.
Some databases implicitly commit certain DDL operations.
Therefore, don't assume:
BEGIN;
DROP TABLE test_table;
ROLLBACK;