DEV Community

Cover image for 📊 Exploring SQL Transactions, Deadlocks & Log Based Recovery
ROHITH
ROHITH

Posted on

📊 Exploring SQL Transactions, Deadlocks & Log Based Recovery

Working with databases is more than just storing data—it’s about managing changes reliably! In this post, let’s explore SQL transaction operations step by step, using actual Oracle Live SQL screenshots from a banking accounts scenario.

🏦 Step 1: Creating Accounts
First, we establish our account table and insert Alice, Bob, and Charlie’s starting balances.

Alice: 1000

Bob: 1500

Charlie: 2000

🔀 Step 2: Updating Balances
Transactions are vital for balance adjustments. Check out how we use UPDATE SQL to debit and credit in action.

Alice's account debited by 300:

Bob receives a 500 credit using a transaction block

đź“‹ Step 3: Viewing Account Status
After the transactions, let’s check balances to see instant results! The SELECT * FROM Accounts; SQL displays exactly how each transaction impacts records.

Current balances for Alice, Bob, Charlie:

🔄 Step 4: Rollback and Transaction Control
Sometimes, errors or changes require rolling back the last transaction. The ROLLBACK command ensures that any accidental modifications are safely undone, keeping the database consistent.

đź”’ Step 5: Isolation with UPDATE and SELECT FOR UPDATE
Real-world systems often require locking for isolated updates, preventing two users from updating the same record simultaneously. The SQL SELECT ... FOR UPDATE in the screenshots shows how databases handle this:

📝 Final Thoughts
By visualizing each operation, it’s clear how SQL transactions like INSERT, UPDATE, ROLLBACK, and SELECT interact to maintain data integrity.
Try these examples in Oracle Live SQL, and see how transaction control can safeguard your app’s critical data!

Drop questions or share how you use transactions below! đź’¬

Top comments (0)