MySQL - START TRANSACTION 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 START TRANSACTION statement of MySQL is used to start a new transaction.
Following is the syntax of the START TRANSACTION statement −
START TRANSACTION [transaction_characteristic[, transaction_characteristic]...]

Assume we have created a table named emp and populated it with 3 records as −
CREATE TABLE accounts(Name VARCHAR(255), DOB DATE, Brach VARCHAR(255), balance INT);
INSERT INTO accounts VALUES ('Amit', DATE('1970-01-08'), 'Hyderabad', 2554);
INSERT INTO accounts VALUES ('Sumith', DATE('1990-11-02'), 'Vishakhapatnam', 5589);
INSERT INTO accounts VALUES ('Sudha', DATE('1980-11-06'), 'Vijayawada', 5587);

You can verify the contents of a table using SELECT statement as −
mysql> SELECT * FROM accounts; MySQL START TRANSACTION Statement
+--------+------------+----------------+---------+
| Name   | DOB        | Branch         | balance |
+--------+------------+----------------+---------+
| Amit   | 1970-01-08 | Hyderabad      | 2554    |
| Sumith | 1990-11-02 | Vishakhapatnam | 5589    |
| Sudha  | 1980-11-06 | Vijayawada     | 5587    |
+--------+------------+----------------+---------+
3 rows in set (0.00 sec)

In the below set of MySQL queries we are starting a transaction and transferring 1500/- from the account of Amit to Sudha.
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.07 sec)

mysql> UPDATE accounts SET balance= balance-1500 WHERE Name='Amit';
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> UPDATE accounts SET balance= balance+1500 WHERE Name='sudha';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> COMMIT;
Query OK, 0 rows affected (0.13 sec)

If you verify the contents of the accounts table again, you can observe the updated balances.
mysql> SELECT * FROM accounts;
+--------+------------+----------------+---------+
| Name   | DOB        | Branch         | balance |
+--------+------------+----------------+---------+
| Amit   | 1970-01-08 | Hyderabad      | 1054    |
| Sumith | 1990-11-02 | Vishakhapatnam | 5589    |
| Sudha  | 1980-11-06 | Vijayawada     | 7087    |
+--------+------------+----------------+---------+
3 rows in set (0.08 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