Mark As Completed Discussion

Commit Example

So, the steps and commands we should follow and write when creating a COMMIT Transaction are: 1. BEGIN TRANSACTION to start the transaction 2. Various SQL statements such as INSERT, UPDATE… 3. COMMIT command to finish the transaction and save the changes.

Take a look at the following example where we insert a new order in the table shown above and we also update the number of items ordered of an already existing order in the database:

TEXT/X-SQL
1-- Start the transaction    
2BEGIN TRANSACTION  
3-- SQL Statements  
4 INSERT INTO Table2 VALUES(15, 8112, 2021-11-12, ‘Nadia’, ‘Female’, Stuttgart, Germany, 17, 7)  
5 UPDATE Table2 SET NumberOfItems = 5 WHERE CustomerID = 2  
6 -- Commit changes   
7COMMIT TRANSACTION  

The INSERT and UPDATE statements cannot be reverted back once the transaction has been committed. If there are no errors, we will see the resulting table shown below, with each transaction's SQL query having been individually executed:

Commit Example