MySQL - COMMIT Statement

In general, transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.
A database transaction is the propagation of one or more changes as a single action on the database.
For example, if you consider the transfer of certain amount from one account to other as a single transaction. This basically contains three steps
Reading the money to be transferred.
Verifying whether the amount is available in the sender’s account.
Crediting money from the sender account.
Debiting money to the receiver account.
For the amount to be transferred all the above operations should be successfully completed. To make sure this happens you can use Transactions and treat all these steps as a single action.
MYSQL provides supports for transactions using the SET autocommit, START TRANSACTION, COMMIT, and ROLLBACK statements.
The COMMIT statement saves all the modifications made in the current transaction since the last commit or the START TRANSACTION statement.
Following is the syntax of the MySQL COMMIT statement −
COMMIT

MySQL saves the changes done after the execution of each statement. To save changes automatically, set the autocommit option as shown below −
SET autocommit=0;

Assume we have created a table with name Players in MySQL database using CREATE statement as shown below −
CREATE TABLE Players( 
   ID INT,
   First_Name VARCHAR(255),
   Last_Name VARCHAR(255),
   Date_Of_Birth date,
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);
Now, we will insert 7 records in Players table using INSERT statements −
insert into Players values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
insert into Players values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
insert into Players values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
insert into Players values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
insert into Players values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
insert into Players values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
insert into Players values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following query saves the changes −
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)

Now, let us delete the last record from the table −
mysql> Delete from Players where id = 7;
Query OK, 1 rows affected (0.16 sec)

You can retrieve the contents of the above create table using the SELECT statement as shown below −
mysql> SELECT * FROM Players;
+----+------------+------------+---------------+----------------+-------------+
| ID | First_Name | Last_Name  | Date_Of_Birth | Place_Of_Birth | Country     |
+----+------------+------------+---------------+----------------+-------------+
| 1  | Shikhar    | Dhawan     | 1981-12-05    | Delhi          | India       |
| 2  | Jonathan   | Trott      | 1981-04-22    | CapeTown       | SouthAfrica |
| 3  | Kumara     | Sangakkara | 1977-10-27    | Matale         | Srilanka    | 
| 4  | Virat      | Kohli      | 1988-11-05    | Delhi          | India       |
| 5  | Rohit      | Sharma     | 1987-04-30    | Nagpur         | India       |
| 6  | Ravindra   | Jadeja     | 1988-12-06    | Nagpur         | India       |
+----+------------+------------+---------------+----------------+-------------+
6 rows in set (0.00 sec)
Following statement reverts the changes after the last commit.
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
All the changes done past the last commit will be reverted if we rollback a transaction. Since we have deleted the 7th record after setting the commit, this delete is reverted at the time of roll back.
mysql> SELECT * FROM Players;
+----+------------+------------+---------------+----------------+-------------+
| ID | First_Name | Last_Name  | Date_Of_Birth | Place_Of_Birth | Country     |
+----+------------+------------+---------------+----------------+-------------+
| 1  | Shikhar    | Dhawan     | 1981-12-05    | Delhi          | India       |
| 2  | Jonathan   | Trott      | 1981-04-22    | CapeTown       | SouthAfrica |
| 3  | Kumara     | Sangakkara | 1977-10-27    | Matale         | Srilanka    |
| 4  | Virat      | Kohli      | 1988-11-05    | Delhi          | India       |
| 5  | Rohit      | Sharma     | 1987-04-30    | Nagpur         | India       |
| 6  | Ravindra   | Jadeja     | 1988-12-06    | Nagpur         | India       |
| 7  | James      | Anderson   | 1982-06-30    | Burnley        | England     |
+----+------------+------------+---------------+----------------+-------------+
7 rows in set (0.00 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page