You've learned how to change data in a database using commands like INSERT, UPDATE, and DELETE. But what if you need to make multiple changes that should succeed or fail together?
Transactions let you group commands like INSERT, UPDATE, and DELETE into one atomic unit—either all succeed or all get undone.
Engagement Message
Why might you want multiple changes to be treated as one unit?
Think of transactions like online shopping. You add items to cart, enter payment info, confirm shipping—but if your card gets declined, everything gets cancelled.
Your cart doesn't keep some items while rejecting others. It's all or nothing!
Name another database task where every step must succeed together or be cancelled.
Every transaction starts with BEGIN. This tells the database "I'm about to make several changes that belong together."
BEGIN;
creates a safe workspace where you can make changes without affecting other users until you're ready.
Engagement Message
What happens to your changes while you're still inside a transaction?
Inside your transaction, you can run multiple INSERT, UPDATE, and DELETE statements. The database keeps track of all your changes but doesn't make them permanent yet.
Think of it like working on a rough draft before publishing your final version.
Engagement Message
Can you give me one advantage of keeping changes temporary at first?
When you're satisfied with all your changes, use COMMIT to make them permanent. COMMIT tells the database "Yes, I want to save all these changes for everyone to see."
COMMIT;
is like hitting the "Publish" button on your draft.
Engagement Message
