Introduction to MySQL Transaction
Mysql transactions can be defined as the atomic unit that comprises multiple SQL query statements that need to executed completely or rollbacked when some issue occurs. Filing any of the database operations will result in inconsistencies and inefficiency of the application. For this, we use the transactions in Mysql. Mysql transactions allow you to perform a set of database operations in entirety. That means either all the commands will execute completely and the transaction will be committed or none of the commands will be executed and the transaction will be rollbacked having no changes on the database in case some error occurs due to some cause such as table locking. In this article, we will learn about transactions in MySQL, properties of transactions, transactional statements, and how we can use transactions in MySQL with the help of an example.
Properties of MySQL Transaction
As Mysql supports different storage engines, out of them InnoDB is completely ACID supported. By the term ACID, we refer to the properties of the transactions.
The transaction supports four properties namely:
whose acronym is ACID. All the four properties are explained below –
1. Atomicity: All the operations within a work unit need to be completed successfully. In case, if some problem or failure occurs while executing operation then all the operations of that work unit should be rollbacked and the state of the database should be brought to the previous state and no effect of that work unit operations should be seen on database if its rollbacked. Completeness of work unit execution is ensured by the atomicity property.
2. Consistency: It ensures that the changes that are made to the database if the transaction is successfully committed.
3. Isolation: All the transactions work independently of each other and are transparent to each other.
4. Durability: If the system fails, the changes made to the database need to be persisted.
Transactional statements in MySQL
To control the transactions, MySQL provides us with ceratin statements that can be used to define the behavior of execution. All the transaction-related statements are mentioned below –
1. START TRANSACTION, BEGIN and BEGIN WORK: To begin the transaction in MySQL, the START TRANSACTION statement is used. BEGIN and BEGIN WORK statements also provide the same functionality.
2. COMMIT: When all the statements inside the transaction or work unit are completely executed then the transaction can be committed by using the COMMIT statement.
3. ROLLBACK: In case if there is a failure in the execution of certain queries inside the transaction then database effects of all the previously executed queries of the same transaction need to be rollbacked. This can be done with the help of the ROLLBACK statement in MySQL.
4. SET autocommit: By default, all the operations in MySQL are automatically committed and its changes are saved permanently. To remove the auto commitment working, we have to set the autocommit property to off or 0. This can be done by using the SET autocommit statement.
This can be done in the following way:
SET autocommit = OFF;
SET autocommit = 0;
To again reset the autocommit mode to yes, you can use the following statement:
SET autocommit = ON;
SET autocommit = 1;
Examples to Implement MySQL Transaction
Consider that we need to add the developers and the technologies that the developers are aware of and used in the database. For this, we will need to store the data in two different tables as a single developer may use and aware of multiple technologies. So, by applying concepts of normalization, we will create two tables namely developers and used_technologies. In the table of technologies, we will have to store the reference of the developer id, so there will be a foreign key to the developer’s table from the used_technologies table. So, our queries will be as follows –
CREATE TABLE Developer
ID INT PRIMARY KEY AUTO_INCREMENT,
CREATE TABLE Used_technologies
ID INT PRIMARY KEY AUTO_INCREMENT,
FOREIGN KEY(developer_id) REFERENCES Developer(ID)
Now, the working should be such that whenever the new developer entry is done in the developer’s table then all the technologies known by him are to be inserted at the same time in the used_technologies table. If a problem occurs while inserting the technologies related to the developer then no entry should be seen in the developers table too. That means either all the entries of developer and related technologies should be inserted or none of them should be inserted.
For this, we can use the transactions in the following way:
SET autocommit = 0;
INSERT INTO Developer (ID, developer_name, email) VALUES
INSERT INTO Used_technologies (name, description,experience, developer_id) VALUES
('Angular','Knows basic commands and syntaxes, uses good logic while writing the code',1,5),
('Mysql','Aware of most of the functionality and features of MySQL and its usage.',0,5);
You can store this transaction code in a file say, for example, we will store it into query.sql. Then using the pipe command as follows can run the transaction code on MySQL server using the terminal:
cat query.sql |sudo mysql -u root -p
Let us check the contents of Developer table and Used_technologies table by logging in to SQL and using educba database in which they reside:
sudo mysql -u root -p
This is because we mentioned the developer_id value as 5 which didn’t exist in the Developer table as we inserted the developer record with ID as 1. As the second insertion command on Used_technologies failed the insert command on developers table too rollbacked as we were using transaction. Try executing the two insert queries without transaction and compare the results of the transaction and non-transactional working.
Transactions help us to execute the set of the statements following the acid properties and thus maintaining the integrity and consistency of the MySQL database.
This is a guide to MySQL Transaction. Here we discuss the Introduction of MySQL Transaction and the practical examples and different subquery expressions. You can also go through our suggested articles to learn more –