School DB Lab
All or nothing

Transaction Lab

Recording a payment is really two writes: insert the payment, then recalculate the invoice's status. A transaction makes sure both happen — or neither does.

فیس کی ادائیگی درج کرنا دراصل دو مراحل پر مشتمل عمل ہے: پہلے پیمنٹ (Payment) داخل کرنا، پھر انوائس (Invoice) کے اسٹیٹس (Status) کو دوبارہ حساب کرنا۔ ٹرانزیکشن (Transaction) اس بات کی ضمانت دیتی ہے کہ یا تو یہ دونوں کام مکمل ہوں گے — یا پھر دونوں میں سے کوئی بھی نہیں ہوگا۔

START TRANSACTION
↓
INSERT Payment
↓
UPDATE Invoice
↓
Success?

Record a real payment (COMMIT path)

Simulate a failure (ROLLBACK path)

This runs the exact same code, but forces an error after the payment INSERT and before COMMIT — so you can see the insert get undone.

After this runs, check the invoice's payment list — the payment row you "inserted" will not be there. The transaction ensured it never really happened.

Explain this page
What is COMMIT?
Tells MySQL "everything since START TRANSACTION is final — make it permanent."
What is ROLLBACK?
Tells MySQL "undo everything since START TRANSACTION as if it never happened."
What does a transaction protect against?
Half-finished work. Without one, a crash between the payment INSERT and the invoice UPDATE would leave the invoice showing the wrong status forever, with no record of why.
COMMIT کیا ہے؟
MySQL کو بتاتا ہے کہ "START TRANSACTION سے اب تک کی تمام تبدیلیاں حتمی ہیں — انہیں مستقل بنا دو۔"
ROLLBACK کیا ہے؟
MySQL کو بتاتا ہے کہ "START TRANSACTION سے اب تک کی تمام تبدیلیاں واپس کرو، جیسے یہ کبھی ہوئی ہی نہ ہوں۔"
ٹرانزیکشن کس سے محفوظ رکھتی ہے؟
آدھے ادھورے کام سے۔ اس کے بغیر، اگر پیمنٹ INSERT اور انوائس UPDATE کے درمیان کوئی خرابی آ جائے، تو انوائس ہمیشہ کے لیے غلط اسٹیٹس (Status) دکھاتی رہے گی اور کوئی ریکارڈ بھی نہیں ہوگا کہ ایسا کیوں ہوا۔