In a database, transaction means successful execution of a SQL statements with valid changes on the database. A database transaction consists of one of the following :
Transactions give you more flexibility and control when changing data, and they ensure data consistency in the event of user process failure or system failure.
A transaction begins when a first DML statement is executed and ends when one of the following occurs :
You can control the logic of transaction by using the transaction control statement.
The statements are :
Update emp
Set emp_name = 'chandini'
Where emp_id =50 ;
1 row updated.
SAVEPOINT upd ;
Savepoint created.
Insert into emp
1 row inserted.
ROLLBACK to upd ;
Rollback completed.
In the above example emp table is updated and a save point is marked after that then a record is inserted to the emp table, up till here all the changes have been done but they have not been made permanent. At the end the rollback statement is issued which ends the transaction at the place where it has been marked and discards all the changes after that.
Tip 1 :
Select account_no, trans_date, amount
From transaction
Where amount + 3000 < 5000 ;
Replace the above query with the following
Select account_no, trans_date, amount
From transaction
Where amount < 2000;
Reason : Avoid unnecessary computational overhead in queries.
Tip 2 :
Select quantity, avg(actual_price)
From item
Group by quantity
Having quantity > 40 ;
Replace the above query with the following :
Select quantity, avg(actual_price)
From item
Where quantity > 40
Group by quantity ;
Reason :- The where clause filters the rows from the table according to the search condition. Then the group by clause is applied only on the filtered rows. It saves time. If as opposed to this, if the rows are grouped first then the row groups are filtered using the having clause, it leads to an increased overhead in terms of time required for execution of the query.
Tip-3 :
Problem statement : to retrieve the average salary for 'presidents' and 'managers'.
Select job, avg(sal)
From emp group by job
Having job='president' or job='manager' ;
Replace the above query with the following :
Select job, avg(sal)
From emp
Where job='president' or job='manager'
Group by job ;
Reason:- the where clause filters the rows from the table according to the search condition. Then the group by clause is applied only on the filtered rows. It saves time. If as opposed to this, if the rows are grouped first then the row groups are filtered using the having clause, it leads to an increased overhead in terms of time required for execution of the query.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc