#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
Step 14
5 min read

COMMIT

Save your transaction permanently - make all changes final.

What is COMMIT?

COMMIT saves all changes made during a transaction. Once you COMMIT, the changes are permanent and cannot be undone.

Think of COMMIT like clicking the Save button after editing a document. Before you save, you can still undo your changes. After you save, the changes are final.

How COMMIT Works

Basic Syntax

BEGIN; -- Make your changes UPDATE table SET column = value WHERE condition; INSERT INTO table (columns) VALUES (values); COMMIT; -- All changes are now permanent

What Happens When You COMMIT

Before COMMIT:

  • Your changes exist only in your session
  • Other users cannot see your changes
  • The database can still undo everything
  • Changes are in a temporary state

After COMMIT:

  • Changes are written to permanent storage
  • All other users can now see the changes
  • The changes survive server restarts
  • You cannot undo with ROLLBACK anymore

Real-life Example: Updating User Profile

A user wants to update their profile information. They change their name, email, and phone number.

BEGIN; UPDATE users SET name = 'John Smith' WHERE user_id = 1; UPDATE users SET email = 'john.smith@email.com' WHERE user_id = 1; UPDATE users SET phone = '555-1234' WHERE user_id = 1; -- All changes look good, save them COMMIT;

After COMMIT, the user profile is updated permanently.

Real-life Example: Processing Payment

When a customer pays for an order, several things must happen and be saved together.

BEGIN; -- Record the payment INSERT INTO payments (order_id, amount, status) VALUES (1001, 99.99, 'completed'); -- Update order status UPDATE orders SET status = 'paid', paid_at = NOW() WHERE order_id = 1001; -- Update customer purchase history UPDATE customers SET total_purchases = total_purchases + 99.99 WHERE customer_id = 5; COMMIT; -- Payment is now officially recorded

COMMIT vs ROLLBACK

You have two choices at the end of a transaction:

COMMIT - Save all changes permanently

  • Use when everything worked correctly
  • Changes become visible to everyone
  • Cannot be undone

ROLLBACK - Cancel all changes

  • Use when something went wrong
  • All changes are discarded
  • Database returns to state before BEGIN
BEGIN; UPDATE accounts SET balance = balance - 500 WHERE user_id = 1; UPDATE accounts SET balance = balance + 500 WHERE user_id = 2; -- If transfer is correct: COMMIT; -- If something is wrong: -- ROLLBACK;

When to COMMIT

COMMIT when:

  • All operations completed successfully
  • Data has been verified
  • You are ready to make changes permanent

Do not COMMIT when:

  • An error occurred during the transaction
  • You are not sure the data is correct
  • You might need to undo the changes

Summary

COMMIT makes your transaction permanent:

  • All changes are saved to the database
  • Other users can now see the changes
  • The changes survive crashes and restarts
  • You cannot undo after COMMIT

Always verify your changes before running COMMIT.

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses