A logical unit of work performed on a database is referred to as a transaction.
Transactions are logically organised work units or sequences that can be done either manually or automatically by a database application.
A transaction is the process of propagating one or more changes to the database.
Creating, updating, or deleting a record from a table, for example, is performing a transaction on that table.
It's vital to keep track of these transactions in order to maintain data integrity and handle database concerns.
In practise, you'll combine numerous SQL queries and execute them all at once as part of a transaction.
A transaction is a logical unit of work that consists of a series of operations executed on a database (using one or more SQL statements).
An atomic, consistent, isolated, and durable database transaction is required.
These four points were covered afterwards.
Consider a banking database to grasp the concept of a transaction.
The statement will be divided into four blocks if a bank customer transfers money from his savings account (S a/c) to his overdraft account (O a/c):
🚀Debit S a/c.
🚀Credit O a/c.
🚀Record in Transaction Journal
🚀 End Transaction
UPDATE S_accounts SET amount = amount - 3000 WHERE account = 236456 ;
UPDATE O_accounts SET amount = amount + 3000 WHERE account = 534456 ;
INSERT INTO journal VALUES (100896, 'Tansaction on Vikek Kumar a/c', '2-SEP-08' 236456, 534456, 3000);
COMMIT WORKS;
Transactions contain the four standard qualities listed below, which are frequently abbreviated as ACID.
🚀 Atomicity : It ensures the successful completion of all processes inside the work unit.
Otherwise, the transaction will be aborted at the point of failure, and all preceding activities would be reversed.
🚀 Consistency : When a transaction is successfully committed, consistency ensures that the database changes states properly.
🚀 Isolation : It allows transactions to execute independently of one another while remaining transparent to them.
🚀 Durability ; It ensures that a committed transaction's outcome or effect is preserved in the event of a system failure.
The commands below are used to manage transactions.
🚀 COMMIT : To save your changes, press COMMIT.
🚀 ROLLBACK :TO REVERSE THE CHANGES, USE ROLLBACK.
🚀 ROLLBACK POINTS : They are created by SAVEPOINT inside groups of transactions.
🚀 SET TRANSACTION : It gives a transaction a name.
DML commands like as INSERT, UPDATE, and DELETE are the only ones that use transactional control commands. They can't be utilised when adding or removing tables because the database commits these activities automatically. The transactional command COMMIT is used to save changes made by a transaction to the database.
The execution of numerous SQL statements is being started by a transaction. The atomicity of a transaction is guaranteed when it starts. It can be committed to make the changes permanent or rolled back to undo the changes and leave the database unchanged after starting.
The transactional command COMMIT is used to save changes made by a transaction to the database.
Since the last COMMIT or ROLLBACK command, the COMMIT command saves all transactions to the database.
The COMMIT command has the following syntax.
COMMIT;
The transactional command ROLLBACK is used to undo transactions that have not yet been recorded to the database.
This command can only be used to reverse transactions that have occurred since the last COMMIT or ROLLBACK command.
The ROLLBACK command has the following syntax:
ROLLBACK;
A SAVEPOINT is a point in a transaction where you can revert to a previous state without reverting the entire transaction.
The SAVEPOINT command has the following syntax.
SAVEPOINT savepoint_name;
This command is only used to create a SAVEPOINT in the middle of all transactional statements.
To undo a collection of transactions, use the ROLLBACK command.
The following is the syntax for rolling back to a SAVEPOINT.
ROLLBACK to savepoint_name;
The RELEASE SAVEPOINT command is used to delete a previously established SAVEPOINT.
The RELEASE SAVEPOINT command has the following syntax.
RELEASE SAVEPOINT savepoint_name;
You can no longer use the ROLLBACK command to undo transactions made since the last SAVEPOINT if a SAVEPOINT has been released.
A database transaction can be started with the SET TRANSACTION command.
This command is used to define the properties of the next transaction.
The SET TRANSACTION has the following syntax.
SET TRANSACTION [ READ WRITE | READ ONLY ];With this article at Logicmojo, you must have the complete idea of SQL Transactions.