0% found this document useful (0 votes)
18 views19 pages

Transaction

A transaction in MySQL is a sequence of operations that are treated as a single unit, which can either be committed or rolled back, ensuring that all operations succeed or none at all. Transactions adhere to the ACID properties: Atomicity, Consistency, Isolation, and Durability, which guarantee reliable processing of database operations. MySQL provides specific statements such as START TRANSACTION, COMMIT, and ROLLBACK to manage transactions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views19 pages

Transaction

A transaction in MySQL is a sequence of operations that are treated as a single unit, which can either be committed or rolled back, ensuring that all operations succeed or none at all. Transactions adhere to the ACID properties: Atomicity, Consistency, Isolation, and Durability, which guarantee reliable processing of database operations. MySQL provides specific statements such as START TRANSACTION, COMMIT, and ROLLBACK to manage transactions effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Transaction

MySQL Transaction

• A transaction in MySQL is a sequential group of statements, queries, or operations


such as select, insert, update or delete to perform as a one single work unit that can
be committed or rolled back. If the transaction makes multiple modifications into the
database, two things happen:
• Either all modification is successful when the transaction is committed.
• Or, all modifications are undone when the transaction is rollback.

• In other words, a transaction cannot be successful without completing each


operation available in the set. It means if any statement fails, the transaction
operation cannot produce results.
• A transaction in MySQL starts with the first executable SQL statement and ends
when it finds a commit or rolled back either explicitly or implicitly. It explicitly uses
COMMIT or ROLLBACK statement and implicitly when a DDL statement is used.
MySQL Transaction

• We can understand the concept of a transaction in MySQL by considering a banking


database. Suppose a bank customer wants to transfer money from one account to
another account. We can achieve this by using the SQL statements that will be divided
into the following steps:
• First, it is required to check the availability of the requested amount in the first account.
• Next, if the amount is available, deduct it from the first account. Then, update the first
account.
• Finally, deposit the amount in the second account. Then update the second account to
complete the transaction.
• If any of the above processes fails, the transaction will be rolled back into its previous
state.
Properties of Transaction

• The transaction contains mainly four properties, which referred to


as ACID property. Now, we are going to discuss the ACID property in detail.
The ACID property stands for:
• Atomicity
• Consistency
• Isolation
• Durability
Atomicity
• Atomicity: This property ensures that all statements or operations within the
transaction unit must be executed successfully. Otherwise, if any operation is
failed, the whole transaction will be aborted, and it goes rolled back into their
previous state. It includes features:
• COMMIT statement.
• ROLLBACK statement.
• Auto-commit setting.
• Operational data from the INFORMATION_SCHEMA tables.
Consistency
• Consistency: This property ensures that the database changes state only when
a transaction will be committed successfully. It is also responsible for
protecting data from crashes. It includes features:
• InnoDB doublewrite buffer.
• InnoDB crash recovery.
Isolation
• Isolation: This property guarantees that each operation in the transaction unit
operated independently. It also ensures that statements are transparent to each
other. It includes features:
• SET ISOLATION LEVEL statement.
• Auto-commit setting.
• The low-level details of InnoDB locking.
Durability
• Durability: This property guarantees that the result of committed
transactions persists permanently even if the system crashes or failed. It
includes features:
• Write buffer in a storage device.
• Battery-backed cache in a storage device.
• Configuration option innodb_file_per_table.
• Configuration option innodb_flush_log_at_trx_commit.
• Configuration option sync_binlog.
MySQL Transaction Statement

• MySQL control transactions with the help of the following statement:


1. MySQL provides a START TRANSACTION statement to begin the transaction. It also
offers a "BEGIN" and "BEGIN WORK" as an alias of the START TRANSACTION.
2. We will use a COMMIT statement to commit the current transaction. It allows the
database to make changes permanently.
3. We will use a ROLLBACK statement to roll back the current transaction. It allows the
database to cancel all changes and goes into their previous state.
4. We will use a SET auto-commit statement to disable/enable the auto-commit mode for
the current transaction. By default, the COMMIT statement executed automatically. So
if we do not want to commit changes automatically, use the below statement:
MySQL Transaction Statement
MySQL Transaction Example

• Suppose we have two tables named "employees" and "Orders" that


contains the following data:
COMMIT Example

• If we want to use a transaction, it is required to break the SQL statements into


logical portions. After that, we can define whether the data should be
committed or rollback.
1. The following steps illustrate to create a transaction:
2. Begin the transaction using the START TRANSACTION statement.
3. Then, select maximum income among the employee.
4. Add a new record to the employee table.
5. Add a new record into the order table.
6. Use the COMMIT statement to complete the transaction.
Below are the commands that perform the above operations:
The below image explains it more clearly:
ROLLBACK Example

• We can understand the rollback transaction with the help of the following
illustration. First, open the MySQL command prompt and log into the
database server using the password. Next, we have to select a database.
• Suppose our database contains the "Orders" table. Now, the following are the
scripts that perform the rollback operations:
we need to open a separate session of MySQL database server and execute the
below statement to verify the data in Orders table:

• Although we have made changes in the first session, we still can see the
records are available in the table. It is because the changes are not permanent
until we have not executed the COMMIT or ROLLBACK statement in the
first session.
MySQL Table Locking
• A lock is a mechanism associated with a table used to restrict
the unauthorized access of the data in a table. MySQL
allows a client session to acquire a table lock explicitly
to cooperate with other sessions to access the table's
data.
• MySQL provides two types of locks onto the table, which
are:
• READ LOCK: This lock allows a user to only read the data
from a table.
• WRITE LOCK: This lock allows a user to do both reading and
writing into a table.
MySQL Table Locking

You might also like