SQL & My SQL Tutorial
- What is SQL?
- SQL Microsoft
- SQL Management Tools
- SQL Developer
- Docker Commands
- Composite Key in SQL
- SQL Constraints
- Transactions in SQL Server
- SQL Server Data Types
- SQL Update Join
- SQL Operators
- SQL Clauses
- SQL Commands
- SQL Alter Command
- Distinct Keyword in SQL
- SQL Statements
- SQL Index
- SQL Injections
- Wildcards in SQL
- Alter and Truncate Command in SQL
- SQL Null Functions – ISNULL, IFNULL, Combine, & NULLIF
- SQL Sequence
- How to Find Duplicate Records in SQL
- Primary and Foreign Key in SQL with Examples
- COUNT Function
- SUM Function in SQL
- Dynamic SQL
- Database Tuning
- Pseudocolumn in Oracle SQL
- Triggers in SQL
- Embedded SQL in DBMS
Transactions in SQL Server
Transactions in SQL server are a single or consecutive set of processes completed in a rational definite flow that can be performed manually or by creating an automatic database programming. Every transaction in SQL starts as a flow from a particular job and should complete with another working job. If any or all of the jobs are incomplete, the transaction status is said to be ‘fail’. A Transaction is said to be ‘complete’ or ‘success’ if and only if all the jobs are completed successfully.
The transaction, once committed, cannot be a rollback; it can be a rollback only if the transaction is not committed.
SET autocommit = 0;
Properties of the Transaction
Following are the important properties of the transactions; every transaction must follow these properties.
A transaction must be atomic; mean data manipulation should be completed for a certain logical unit. This property ensures that data changes taken place completely; otherwise, roll back the transaction.
Once the Transaction finishes, all the available records will be consistent throughout the transaction. This property ensures that the database property switched state after a successful commit or not.
Isolation refers to data changes at a certain logical unit that should not affect on another unit. It allows a transaction to execute independently.
Changes made during transactions should be permanent in the system. In case of a system error, this property also ensures that data changes take place or not.
The above-given property of the transaction is also known as ACID property.
Steps of Transaction
Below are the steps:
A transaction may occur in multiple SQL executions, but all SQL should run at once. If any of the transactions fails, then the whole transaction would be reverted. The statement for starting the transaction is “START TRANSACTION”,, .begins the acronym for the START TRANSACTION.
Commits permanently reflect the changes to the database. The statement for starting the transaction is “COMMIT”.
Rollback is used to revert the changes, i.e. record will not be changed, It would be in the previous state. The statement for starting the transaction is “ROLLBACK”.
SAVEPOINT is also a transaction statement. This statement used to create a store point in the system so that the ROLLBACK operation can achieve the state of the savepoint.
RELEASE SAVEPOINT is a statement to release the savepoint& memory consumed by the system in creating a save point.
RELEASE SAVEPOINT SP
The SET TRANSACTION command is used to specify the transaction attribute, such as the given transaction is a read-only or read-write session.
SP was the name of the savepoint when this savepoint was created before the transaction start.
SET TRANSACTION [ READ-WRITE | READ ONLY ];
The transaction is used to perform complex changes in the database. Its mainly used in banking-related information changes into a relational database.
The transaction is supported by the MYSQL engine InnoDB. By default, auto-commit remains enabled; that’s why whenever any SQL executes after execution, commits automatically take place.
Benefits of using Transaction in SQL
- Using Transaction improves the performance; when inserting 1000 records using transactions, in that case, time taken would be lesser than normal insertion. As in a normal transaction, every time COMMIT would take place after each query execution & it would increase the time of execution every time while in a transaction, no need to execute COMMIT statement after each SQL query. COMMIT at the end would reflect all the changes to the database permanently at once. Also, if using a transaction, then reverting the changes would be much easier than the normal transaction. ROLLBACK will revert all the changes at once & keep the system in the previous state.
- The transaction ensures data integrity in the relational database. Most of the database uses multiple tables to maintain the data, and while making updates, there may have been changes in the multiple tables at that time; if any of the SQL queries fail, then the transaction would keep the data unchanged.