Definition of MariaDB Transaction
- MariaDB Transaction is defined as the process that come for rescue. This transaction procedure permits a user to run a set of MariaDB operations to confirm that the database does not include the result of partial operations ever.
- In case, suppose we have a group of operations in the server, and one of them fails to operate then in such a condition the transaction rollback is implemented for restoring the database server to its original transactional state.
- Again, for the case where no error has been occurred then there the whole set of statements will be committed to the database server.
Syntax:
Generally, MariaDB transactions is initiated by using the SQL statement as START TRANSACTION and is ended by using the COMMIT or ROLLBACK statements. Here, a synonym for the START TRANSACTION is BEGIN WORK but it does not operate within the stored programs since the keywords BEGIN and END are implemented to encompass the code blocks.
Therefore, the over-all syntax can be defined as follows:
START TRANSACTION;
{transactional_characteristic {, transactional_characteristic}…}
Transactional characteristic:{
WITH CONSISTENT SNAPSHOT | READ WRITE | READ ONLY
}
BEGIN {WORK}
COMMIT {WORK} {AND {NO} CHAIN} {{NO} RELEASE}
ROLLBACK {WORK} {AND {NO} CHAIN} {{NO} RELEASE}
SET autocommit = (1, 0);
These MariaDB statements are provided with few helpful queries to manage the transactions explained below:
- For starting a transaction in MariaDB, firstly we need to type the command statement as START TRANSACTION where the aliases of this can be the BEGIN or the BEGIN WORK.
- Next, we can apply the command as COMMIT which helps to commit the present transaction in MariaDB and also can make its alterations permanent.
- Again, similarly, we can use the command as ROLLBACK that is responsible to roll back the present transaction and also to cancel its alterations or changes.
- Also, we can apply the command as SET autocommit statement useful to enable or disable the auto-commit mode for the present transaction.
- But by default, the changes are committed permanently to the database automatically by MariaDB server. The user can also force the MariaDB server not to commit the modifications spontaneously by applying the succeeding query statements:
SET autocommit = 0;
Or,
SET autocommit = OFF;
- In the same way, for enabling the auto-commit mode we can use the statements below explicitly as:
SET autocommit = 1;
Or,
SET autocommit = ON;
- The parser treats the command BEGIN {WORK} for the beginning of the BEGIN…..END code block within entire stored programs such as stored events, functions, procedures, and triggers.
- The WORK keyword is optional and supports methods of ROOLBACK and COMMIT in the transaction whereas the clauses RELEASE and CHAIN are used because they provide extra control over the completion of transaction.
How does Transaction work in MariaDB?
- In MariaDB, a user applies a group of SQL query statements that regulates and controls the server transactions. So, these transactions permit the user to obviously start or commit or also rollback a MariaDB transaction but in few cases these operations can be implicit.
- Again, we can fix an isolation level that controls which transactional locks are attained and how constant the reads are. In advance, a user can also state that a transaction is read-only and this permits InnoDB to run additional internal optimizations.
- The Transactions life cycle starts with the keyword START TRANSACTION and terminates with either the ROLLBACK or COMMIT command.
- It is important to note that several APIs implemented for writing client applications in MariaDB deliver their own procedures to begin transaction that may be sometimes be applied in place of sending a START TRANSACTION query statement form the client.
- There is a significant concept in the field of transaction processing defined as the ACID properties. These properties are the four key to perform transaction which is named as: Atomicity, Consistency, Isolation, and Durability. Due to this the whole changes to the data are operated as if they denote one only operation.
- Also, these properties are useful to provide consistency in a database server always before and after any transaction processing.
Examples
Let us now discuss the examples to demonstrate the MariaDB Transaction in the server explained as follows:
We will implement the transaction taking two tables as Orders and OrderInfo created in the MariaDB database. Here, the orders table contains the fields as OrderNum, OrderDate, datereq, dateship, status & customernum and the OrderInfo table contains the fields as OrderNum, procode, orderquantity, eachprice and orderlinenum.
Let us view some demo contents inserted in both the tables as follows:
Orders:
select * from orders;
OrderInfo:
select * from orderinfo;
Firstly, we will start the transaction using the command START TRANSACTION. After that we will select the newest sale order number from the table Orders and then applying the subsequent sale order number referred as the fresh sale order number.
Next, we will add a new sale order into the table orders. Following to it, again we will enter the sale order items into the table OrderInfo. And then lastly, we will commit the whole transaction by means of COMMIT command.
Optionally, we can choose data rows from both the tables i.e. orders and orderinfo, in order to check the fresh sale order. Now, perform the script code below to execute the above actions:
START TRANSACTION;
SELECT
@OrderNum:=MAX(OrderNum)+1
FROM
Orders;
INSERT INTO Orders(OrderNum, OrderDate,datereq,dateship,status,customernum) VALUES(@OrderNum, ‘2020-11-08’,’2020-12-10’,’2021-02-02’,’In Process’,125);
INSERT INTO OrderInfo(OrderNum,procode,orderquantity,eachprice,orderlinenum) VALUES(@OrderNum, ‘P12_100’,10,150,1), (@OrderNum, ‘P12_110’,20,200,2);
COMMIT;
Output:
Now, to fetch the newly constructed sale order we will run the query as follows:
SELECT a.OrderNum,OrderDate,datereq,dateship,status,customernum,orderlinenum,procode,orderquantity,eachprice FROM Orders a
INNER JOIN
OrderInfo b USING (OrderNum)
WHERE
a.OrderNum = 113;
Hence, you can view the output as:
Conclusion
- MariaDB Transaction can be said as the SQL transaction processing which signifies a series of executions of SQL query statements which is atomic with admiration to recovery.
- This denotes that in the server with the MariaDB Transaction either the query execution output will be completely successful one or it may have no effect on any SQL data or SQL schemas. The transactions in MariaDB cannot be implemented in Stored Triggers or Functions.
Recommended Articles
This is a guide to MariaDB Transaction. Here we discuss the definition, How does Transaction work in MariaDB? and examples with code implementation respectively. You may also have a look at the following articles to learn more –