â The COMMIT statement is used to permanently save all changes made during the current transaction. Once a transaction is committed, its changes become visible to other database sessions and cannot be undone using ROLLBACK.
đ What is COMMIT?
A transaction groups multiple SQL statements into a single unit of work. After all statements execute successfully, the COMMIT statement makes every change permanent. If COMMIT is not executed (and the transaction is rolled back instead), none of the changes are saved.
Information
đ¯ Why Use COMMIT?
The COMMIT statement ensures that successful database operations are permanently stored.
- đ Permanently save transaction changes.
- đ Complete multi-step database operations.
- đ Maintain data consistency.
- đ Release transaction resources and locks.
- đ Finalize successful business operations.
đĄ Real-World Example
Consider transferring $500 from one bank account to another:
- Withdraw $500 from Account A.
- Deposit $500 into Account B.
- Commit the transaction.
If both operations succeed, COMMIT permanently records the transfer in the database.
đ Basic Syntax
COMMIT Syntax
BEGIN TRANSACTION;
-- SQL statements
COMMIT;â Example: Update Data and Commit
Commit an UPDATE Transaction
BEGIN TRANSACTION;
UPDATE Employees
SET Salary = Salary + 5000
WHERE EmployeeID = 101;
COMMIT;After COMMIT, the salary update becomes permanent.
â Example: Multiple Operations
A transaction often contains several related SQL statements.
Commit Multiple Statements
BEGIN TRANSACTION;
INSERT INTO Orders
(OrderID, CustomerID, Amount)
VALUES
(1001, 25, 750);
UPDATE Inventory
SET Quantity = Quantity - 1
WHERE ProductID = 50;
COMMIT;Both statements are saved together. If either statement fails before the commit, the transaction can be rolled back.
đ Transaction Flow
| Step | Action |
|---|---|
| 1 | Begin the transaction. |
| 2 | Execute one or more SQL statements. |
| 3 | Verify all operations succeed. |
| 4 | Execute COMMIT. |
| 5 | Changes become permanent. |
âī¸ COMMIT vs ROLLBACK
| Feature | COMMIT | ROLLBACK |
|---|---|---|
| Purpose | Save changes permanently. | Undo uncommitted changes. |
| Database State | Updated permanently. | Restored to its previous state. |
| Used After Success | â Yes | â No |
| Used After Failure | â No | â Yes |
đĄī¸ COMMIT and ACID
COMMIT plays an important role in the ACIDproperties of database transactions.
| ACID Property | How COMMIT Contributes |
|---|---|
| Atomicity | Confirms that all transaction operations completed successfully. |
| Consistency | Leaves the database in a valid state. |
| Isolation | Makes committed changes visible according to the database's isolation rules. |
| Durability | Ensures committed changes survive system failures. |
Remember
đŧ Real-World Applications
- đĻ Bank transfers.
- đ Online purchases.
- đĻ Inventory updates.
- đī¸ Ticket booking systems.
- đĨ Hospital management.
- đŗ Payment processing.
đī¸ Database Compatibility
| Database System | COMMIT Support |
|---|---|
| MySQL | â Supported for transactional storage engines such as InnoDB. |
| PostgreSQL | â Full transaction support. |
| SQL Server | â Full transaction support. |
| Oracle | â Full transaction support. |
| SQLite | â Full transaction support. |
â ī¸ Common Mistakes
- â Forgetting to commit a successful transaction.
- â Committing before verifying all operations completed successfully.
- â Assuming committed changes can be rolled back later.
- â Leaving transactions open longer than necessary.
Warning
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ COMMIT permanently saves all changes in the current transaction.
- đ After a commit, changes are generally no longer reversible with ROLLBACK.
- đ It completes successful business operations.
- đ Committing also releases transaction resources and locks.
- đ COMMIT is a core part of ACID transaction processing.
- đ Supported by all major relational database systems.