MySQL MariaDB

MySQL Transactions

MySQL Transactions
A transaction is a simultaneous collection of functions for manipulating datasets and is carried out as though it was a single entity of work. In other terms, a transaction can never be accomplished until each particular process is successful inside the unit. The whole process will collapse if any transaction inside the process crashes. Several SQL queries are clubbed into a unit, and all of them will be executed together as a portion of its transaction. Whenever a transaction incorporates several updates to a database, and the transaction is committed, all the modifications work, or all the updates are reversed whenever the operation is rolled back.

Transaction Properties

Transactions, frequently known by the term ACID, have four main general properties.

MySQL Transactions Working:

Within MySQL, the two terms, Commit and Rollback are primarily used only for MySQL transactions. The transactions start only with BEGIN WORK declaration and finish either by a COMMIT declaration or a ROLLBACK declaration. The SQL instructions comprise the majority of the transaction amongst both the starting and stopping statements. Such event series is irrespective of the specific programming language utilized. You will make a suitable path in whatever language you are using to build the application. The below SQL statements can be implemented using the mysql query() feature.

Example 01: AutoCommit Mode On:

MySQL operates with the Autocommit phase allowed through default. It ensures that MySQL saves the changes on the disk to create it perpetually as long as we run a query that adjusts (alters) a table. It is not necessary to turn back the move. Let's try with AUTOCOMMIT on mode. Open your MySQL command-line shell and type your password to begin.

Take an example of a table 'book' that has been created in a database 'data'. Right now, we haven't performed any query on it yet.

>> SELECT * FROM data.book;

Step 2: This process is to update the table 'book'. Let's update the value of column 'Author' where the 'Name' of a book is 'Home'. You can see changes have been made successfully.

>> UPDATE data.book SET Author = 'Cristian Steward' WHERE Name = 'Home';

By having a glimpse of the updated table, we have a modified value of the author where 'name' is 'Home'.

>> SELECT * FROM data.book;

Let's use the ROLLBACK command to revert the changes by simply adding the below query. You can see that the ROLLBACK query doesn't work here as it shows that '0 rows affected'.

>> ROLLBACK;

You can even see the table as well. The table has got no changes after the ROLLBACK statement execution so far. This means that the ROLLBACK doesn't work when we have AUTOCOMMIT on by default.

>> SELECT * FROM data.book;

Example 02: AutoCommit Mode Off:

To revert the changes made, let's try with AUTOCOMMIT off mode. Using the same example of table 'book', we will perform some changes on it. We will be using the START TRANSACTION declaration to deactivate its auto-commit phase or simply type the below command to set AUTOCOMMIT off.

>> SET AUTOCOMMIT = 0;

Let's say we have the same table 'book' in our database, and we have to make changes to it. Then revert those changes to the old ones again.

>> SELECT * FROM data.book;

If you haven't turned off the AUTOCOMMIT mode, then make a start with a START TRANSACTION query in the command line shell as below.

>> START TRANSACTION;

We will be updating the same table using the UPDATE command by setting the 'Author' as 'Aliana' where the 'Name' of a book is 'Dream'. Do it by using the below command. You will see that the changes will be made successfully and effectively.

>> UPDATE data.book SET Autho = 'Aliana' WHERE Name = 'Dream';

Let's check whether the above query has worked perfectly and made changes to the table or not. You can check the updated table by using the below SELECT command as always.

>> SELECT * FROM data.book;

You can see that the query has worked great, as shown below.

Now, it's a turn of the ROLLBACK command to perform its function. Try the ROLLBACK command in your command line to roll back the recent update to the table.

>> ROLLBACK;

Let's check whether the ROLLBACK query has been worked as it should work or not. For this, you have to check the table 'book' again by using the 'SELECT' command as always.

>> SELECT * FROM data.book;

You can see from the below output that ROLLBACK has finally worked. It has reverted the changes made by the UPDATE query on this table.

Conclusion:

That's all for MySQL transactions. I hope this guide will help you to perform MySQL transactions conveniently.

Gry Najlepsze dystrybucje Linuksa do gier w 2021 r
Najlepsze dystrybucje Linuksa do gier w 2021 r
System operacyjny Linux przeszedł długą drogę od pierwotnego, prostego, serwerowego wyglądu. Ten system operacyjny znacznie się poprawił w ostatnich l...
Gry Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
Jak przechwytywać i przesyłać strumieniowo sesję gry w systemie Linux
W przeszłości granie w gry było uważane tylko za hobby, ale z czasem branża gier odnotowała ogromny wzrost pod względem technologii i liczby graczy. P...
Gry Najlepsze gry do grania ze śledzeniem rąk
Najlepsze gry do grania ze śledzeniem rąk
Oculus Quest niedawno wprowadził świetny pomysł śledzenia rąk bez kontrolerów. Przy stale rosnącej liczbie gier i działań, które wspierają zarówno for...