From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Ensure data consistency with transactions

Ensure data consistency with transactions - SQL Server Tutorial

From the course: Complete Guide to Advanced SQL Server

Ensure data consistency with transactions

When you need to ensure that multiple actions are successfully performed on the database in order to maintain the consistency of the data, then those actions should be grouped together as a transaction. Transactions create a wrapper that surrounds a batch of commands that you send to the database server. This wrapper allows you to treat the entire batch as a single unit of work, and you can choose to finalize or undo the entire transaction all at once. This allows you to perform multi-step processes without having to worry about the data being in an inconsistent state. To illustrate what I mean, let's take a look at an example. Suppose we have two bank accounts, a checking account with $500 in it, and a savings account that has a zero balance. If we want to transfer $200 from checking to savings, that requires two separate operations in the database. First, we need to deduct $200 from the checking balance. Then we need to add $200 to the savings balance. If everything finishes…

Contents