hello friends database transactions can be confusing the first time you hear about the word transaction you imagine a bank transaction or some form of money transaction but this one is not that although the bank transaction and money transaction is a valid use case for database transaction but it's not the same thing even after we understand this concept we wonder where we use transactions in database or in any application and then the concept of acid what is that right well after watching this video i can assure you you will never ever have to watch any other video as far as database transactions are concerned so let's get started let's start with a simple example if i ask you to fill fuel in your car by yourself at a fuel station then in order to do it you'll have to open the lid of the fuel tank fill the fuel and then close the lid back here opening the lid filling the petrol or diesel whatever your car supports and then closing the lid is a single unit of work all these three steps have to be done together you cannot miss any of the single step otherwise you know there'll be some problem for example if i open the lid and then forget what i was doing sit back in the car without closing the lid and drive away then i'm moving around with my fuel tank open which is not safe if i open the lid fill the fuel and do not close the lid again dangerous if i open the lid and then close it without filling the tank then i will leave the fuel station without any fuel how dumb one could be to do that hence i'm calling it a single unit of work that has three steps and all the three steps must be completed in order to the task to be completed let's take another example imagine you have a simple application student book for a student to register for this application he she has to fill up form when a user registers the login information which is like the email and the password are saved in the user table whereas the other information is saved in a different user info table in this flow if the data is inserted in user table but for some reason the user info table insert query fails then our data would be inconsistent similarly if the first insert in the user table fails and the second insert is done then also data will be incorrect in the database hence in this simple setup we want both the queries to run successfully together acting as a single unit of work if any one insert query fails we should fail the entire registration process show a message to the user to try again there are many other similar use cases where we want this particular behavior for example if you are using an e-commerce website when you purchase any item on successful payment a new order is created the product stock is updated and the cart info is deleted that is one insert one update and one delete that must act as a single unit of work because if some of it passes and some of it fails the state of the database and the application becomes inconsistent agree maybe i don't know let's take one more example the bank transaction example if there are two bank accounts a and b where a has an account balance of rupees 5000 and b has an account balance of rupees 10 000 now a wants to transfer rupees 2000 to b the whole process of money transfer is a three-step process more or less check if a has rupees 2000 to transfer if he has the amount in his account then deduct the amount from his bank account and add rupees 2000 to b's account here it would not be right if we deduct money from a's account and do not credit it to b's account because then rupees 2000 will just vanish from the banking system or if we credit the money to b's account but fail to deduct the same amount from a's account then a will be happiest customer of the bank but the bank will lose rupees 2000. hence we can say for the money transfer process either all steps pass successfully or all steps must be failed and the user can try again debit and credit passes or debit and credit fails enabling this behavior in databases is done using transactions and it is also represented by the symbol tx when in a database we have to run multiple sql queries that affects the state of data in our database through insert and update and delete queries we must combine them into a single unit of work so that all of them pass together or fail together in case even a single query fails then the whole bundle of query executed in a transaction have to fail and that is done using transactions in database a transaction also follows acid properties acid not as the chemical acid but acid where a stands for atomicity c stands for consistency i stands for isolation and d stands for durability atomicity is derived from atomic which also means one or single a single unit of work hence no partial success no partial failure is allowed either everything passes or if even a single step fails then everything is marked as failed and any changes that were made have to be rollback c stands for consistency when a transaction is executed it follows the database constraints like validations on table columns or relationships like foreign key because if that is not followed then also the state of data may become inconsistent after running a couple of insert update or delete queries so a transaction must follow the constraints of the database i stands for isolation isolation means that if more than one process is trying to act upon the same data then they should be allowed to do it one by one for example on an e-commerce website if there is a single piece of particular t-shirt which is left and three users are trying to purchase it and coincidentally they trigger the purchase at the same time then if the three requests are not isolated chances are that the app will show that one piece is available to all of three of them and will create three orders which is not right hence a transaction makes sure that it is executed in isolation and once it is completed next transaction is initiated so out of the three users trying to bind the same t-shirt one will get it for others it will show sold out because updated inventory will show zero pieces available now one obvious side effect of this is that it makes the query execution slow because consecutive transactions have to wait there is no parallel execution but for important data data consistency is more important than speed last but not the least d stands for durability that means whatever actions are performed they must be permanent so once a transaction is completed the changes of the data are permanent so these are acid properties a transaction must always follow these properties because then only it will be able to do what it is supposed to do the concept of transactions is equally theoretical and practical i've seen a couple of videos on youtube which says it is more of a theory thing than a practical i totally disagree with them because we use transactions in our applications every day all the big applications all the big websites all the big softwares use transactions in their softwares all the time so it is absolutely a practical thing now understanding it can be theoretical but then that is true for all the concepts when you work on any application or a website or a software where you store data in the database and perform different operations on the data you will understand the true value of transactions because to make sure multiple sql queries are executed correctly together maintaining the correctness of the data you have to use database transactions if you want me to show some practical examples of using transactions do comment below and i will create a video where i can show you how database transactions are implemented while using a programming language in the code if you like this video give it a thumbs up shower your love by sharing the video with your friends and please post your sweet comments so that we can know what we're doing right and what we are doing wrong if you have any confusion around database transactions feel free to post a comment and i'll definitely answer all the comments posted in this video and do not forget to subscribe to the straight youtube channel and press that bell icon see you in the next video [Music] [Applause] [Music] [Applause] you