Careeroza
COMMIT
The COMMIT Statement
COMMIT saves all changes made in the current transaction. After a commit the changes are permanent and visible to other users, even if the server crashes a second later.
Example
Move 50 units of stock from product 1 to product 3 as one unit of work:
BEGIN TRANSACTION;
UPDATE Products SET Stock = Stock - 50 WHERE ProductID = 1;
UPDATE Products SET Stock = Stock + 50 WHERE ProductID = 3;
COMMIT;
SELECT ProductID, ProductName, Stock FROM Products WHERE ProductID IN (1, 3);Result: 2 rows
| ProductID | ProductName | Stock |
|---|---|---|
| 1 | Basmati Rice 5kg | 70 |
| 3 | Olive Oil 1L | 130 |
Syntax per Database
-- MySQL
START TRANSACTION; ... COMMIT;
-- PostgreSQL / SQLite
BEGIN; ... COMMIT;
-- SQL Server
BEGIN TRANSACTION; ... COMMIT TRANSACTION;