Transaction Control Commands

Excellent ๐Ÿ‘
Now TCL (Transaction Control Language) in the SAME structured pattern:

  • Base tables

  • Question

  • Transaction SQL

  • State BEFORE & AFTER (outputs)

  • ✔ crystal-clear takeaways


๐Ÿ“˜ Base Tables (Used for ALL TCL examples)

employees

emp_idnameage
1Amit30
2Neha28

products

product_idcategoryprice
1Electronics20
2Furniture100

customers

customer_idnameage
1Ram65
2Sita45

๐Ÿงพ Summary Table (TCL Questions)

No.TCL CommandQuestion
1COMMITSave all changes permanently
2ROLLBACKUndo all changes
3SAVEPOINTCreate rollback checkpoints
4ROLLBACK TO SAVEPOINTRoll back to a specific point
5SET TRANSACTIONSet isolation level

1️⃣ COMMIT

Question: Insert an employee and update product price, then save changes.

BEGIN TRANSACTION;

INSERT INTO employees (name, age)
VALUES ('Alice', 30);

UPDATE products
SET price = 25
WHERE category = 'Electronics';

COMMIT;

✅ Result AFTER COMMIT

employees

emp_idnameage
1Amit30
2Neha28
3Alice30

products

categoryprice
Electronics25
Furniture100

✔ Changes saved permanently
✔ Cannot be undone


2️⃣ ROLLBACK

Question: Insert & update data, then undo everything.

BEGIN TRANSACTION;

INSERT INTO employees (name, age)
VALUES ('Bob', 35);

UPDATE products
SET price = 30
WHERE category = 'Electronics';

ROLLBACK;

✅ Result AFTER ROLLBACK

employees (unchanged)

emp_idnameage
1Amit30
2Neha28
3Alice30

products (unchanged)

categoryprice
Electronics25
Furniture100

✔ All changes cancelled
✔ Database returns to last COMMIT


3️⃣ SAVEPOINT

Question: Create savepoints and roll back partially.

BEGIN TRANSACTION;

INSERT INTO employees (name, age)
VALUES ('Carol', 28);

SAVEPOINT before_update;

UPDATE products
SET price = 40
WHERE category = 'Electronics';

SAVEPOINT after_update;

DELETE FROM customers
WHERE age > 60;

ROLLBACK TO before_update;

COMMIT;

✅ Final Result

employees

nameage
Carol28

products

categoryprice
Electronics25
Furniture100

customers

nameage
Ram65
Sita45

✔ DELETE rolled back
✔ UPDATE rolled back
✔ INSERT kept


4️⃣ ROLLBACK TO SAVEPOINT

Question: Roll back only the UPDATE, keep INSERT.

BEGIN TRANSACTION;

INSERT INTO employees (name, age)
VALUES ('David', 42);

SAVEPOINT before_update;

UPDATE products
SET price = 50
WHERE category = 'Electronics';

SAVEPOINT after_update;

DELETE FROM customers
WHERE age > 60;

ROLLBACK TO SAVEPOINT before_update;

COMMIT;

✅ Final Result

employees

nameage
David42

products

categoryprice
Electronics25
Furniture100

customers

nameage
Ram65
Sita45

✔ INSERT kept
✔ UPDATE cancelled
✔ DELETE cancelled


5️⃣ SET TRANSACTION

Question: Set isolation level and commit changes.

BEGIN TRANSACTION;

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

INSERT INTO employees (name, age)
VALUES ('Emily', 35);

UPDATE products
SET price = 60
WHERE category = 'Electronics';

COMMIT;

✅ Result AFTER COMMIT

employees

nameage
Emily35

products

categoryprice
Electronics60
Furniture100

✔ Controls transaction behavior
✔ Affects concurrency & consistency


๐Ÿ”ฅ TCL Command Comparison (Exam Favorite)

CommandPurpose
COMMITSave changes
ROLLBACKUndo all changes
SAVEPOINTCreate checkpoint
ROLLBACK TOUndo to checkpoint
SET TRANSACTIONConfigure transaction

๐Ÿง  One-Line Memory Trick (TCL)

COMMIT saves, ROLLBACK cancels, SAVEPOINT bookmarks


If you want next:

  • All SQL commands in ONE final master table

  • MCQs with transaction output prediction

  • ACID properties mapped to TCL

  • Hindi + exam-trap explanations

Just say ๐Ÿ‘

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.