COMMIT
COMMIT is an SQL command used to save all the transactions to the database since the last COMMIT or ROLLBACK command. This statement permanently applies changes made by SQL commands within a transaction. It's a way to end a successful transaction, ensuring that all operations within the transaction are performed. If the COMMIT command executes successfully, it returns an acknowledgment to the user. After this command, the changes are visible to other users and are persistent even if a system failure occurs.
Example
Output
Explanation
In the provided example, a transaction is initiated by START TRANSACTION;
. Then an INSERT
statement is executed to add new records into the employees
table. At the end, the COMMIT;
statement is used to save all modifications made since the start of the transaction. The output corresponds to MySQL’s acknowledgment to the successful insertion of data.
Example
Output
Explanation
In this example, a new record is inserted into the ‘employees’ table with ‘John’ as ‘first_name’ and ‘Doe’ as ‘last_name’. The COMMIT
statement is used for making the changes permanent in the database. Here the insert operation is enclosed between BEGIN;
and COMMIT;
which represent a transaction. After the commit, the result ‘INSERT 0 1’ indicates that one record was successfully inserted.
Example
Output
No explicit output is generated by the COMMIT command. However, the result of above query would be the change in Salary for the employee with ID = 1.
Explanation
In the provided example, first a transaction is started with BEGIN TRANSACTION
, then an UPDATE
command is used to change the salary of the employee with ID = 1. Finally, the COMMIT
statement is used to save these changes permanently in the database. If an error occurred during the transaction, COMMIT
would not be executed, and all changes within the transaction would be rolled back. Thus, COMMIT
ensures that only successful transactions are saved in the database.
Example
Output
Explanation
The COMMIT
command in the example saves the changes made by the UPDATE
statement. The UPDATE
statement increases the salary of an employee with employee_id
= 100 by 500. After executing the COMMIT
command, the UPDATE
command’s changes are permanently stored in the database. If COMMIT
wasn’t executed, the changes could be rolled back.
Example
Output
Explanation
In this example, a SQL transaction begins with BEGIN TRANSACTION
, and an UPDATE
statement is executed to modify the salary of an employee whose ID is 1 within the transaction. If the UPDATE
statement is executed successfully, the COMMIT
statement is used to save the changes. The output indicates that one row has been affected by the update operation, meaning the transaction was successful.