From the course: Program Databases with Transact-SQL
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Ensure data consistency with transactions - SQL Server Tutorial
From the course: Program Databases with Transact-SQL
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.