
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we implement a MySQL transaction?
As we know that in a transaction, statements are executed as a unit. If any operation within the transaction fails, the entire transaction will fail and should be rolled back; otherwise, any changes made by the statements are saved to the database. For implementing a transaction MySQL provides the following statements −
START TRANSACTION
As the name suggests, the transaction begins with this statement. Basically, it notifies MySQL that the statements that follow should be treated as a single work unit until the transaction has been ended.
COMMIT
COMMIT Statement commits changes to the database. In other words, when a successful transaction is completed, the COMMIT command should be issued so that the changes to all involved tables will take effect.
ROLLBACK
ROLLBACK command undoes any changes made by the statement and returns the database to the previous state i.e. in the state it was in when the transaction began.
Example
Following is an example to exhibit the implementation of a MySQL transaction −
mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO Marks Values(1, 'Aarav','Maths',50); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Marks Values(2, 'Harshit','Maths',55); Query OK, 1 row affected (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.06 sec)
In this example, the transaction is initiated by the START TRANSACTION statement. Two INSERT statements are then executed followed by a COMMIT statement. COMMIT statement will save the changes to the database which can be observed from the following result set which shows that the values have been inserted into the table −
mysql> SELECT * FROM Marks; +------+---------+---------+-------+ | Id | Name | Subject | Marks | +------+---------+---------+-------+ | 1 | Aarav | Maths | 50 | | 2 | Harshit | Maths | 55 | +------+---------+---------+-------+ 2 rows in set (0.00 sec) mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO Marks Values(1, 'Aarav','History',40); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO Marks Values(1, 'Harshit','History',48); Query OK, 1 row affected (0.00 sec) mysql> ROLLBACK; Query OK, 0 rows affected (0.04 sec)
In this example, the transaction is initiated by the START TRANSACTION statement. Two INSERT statements are then executed followed by a ROLLBACK statement. ROLLBACK statement will undo the changes made to the database which can be observed from the following result set which shows that no new values have been inserted into the table −
mysql> SELECT * FROM Marks; +------+---------+---------+-------+ | Id | Name | Subject | Marks | +------+---------+---------+-------+ | 1 | Aarav | Maths | 50 | | 1 | Harshit | Maths | 55 | +------+---------+---------+-------+ 2 rows in set (0.00 sec)
- Related Articles
- How can we check the current MySQL transaction isolation level?
- How can we find out the current transaction mode in MySQL?
- How can a user start new MySQL transaction?
- How can a user explicitly end current MySQL transaction?
- How can a user implicitly end current MySQL transaction?
- How can we implement a JToggleButton in Java?
- Can we implement nested insert with select in MySQL?
- How can we implement a Custom HashSet in Java?
- How can we implement a scrollable JPanel in Java?
- How can we implement a rounded JTextField in Java?
- How can we implement a timer thread in Java?
- How can we implement a custom iterable in Java?
- Can we implement 'LIKE' and ‘IN’ in a single MySQL query?
- How can we Implement a Stack using Queue in Java?
- How can we implement editable JComboBox in Java?
