0% found this document useful (0 votes)
7 views3 pages

7 Transactions

Sql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

7 Transactions

Sql
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

In Server, Transactions are used to ensure a group of operations are completed

successfully.
If any part of the transaction fails, it can be rolled back, meaning all changes
made
in the transaction are undone. This ensures data integrity and consistency.

Step 1: Understanding Transactions


A transaction is a group of operations that are executed as a single unit.
Transactions have four main properties, often referred to as ACID:
- Atomicity: Either all operations succeed, or none do.
- Consistency: Transactions leave the database in a consistent state.
- Isolation: Transactions don’t affect each other’s execution.
- Durability: Once a transaction is committed, it is saved permanently.

Step 2: Basic Syntax of Transactions


Transactions in Server use these keywords:
- BEGIN TRANSACTION: Starts a transaction.
- COMMIT: Saves the changes made in the transaction.
- ROLLBACK: Cancels the transaction, undoing all changes.

Syntax:

BEGIN TRANSACTION;

-- operations go here

COMMIT; -- or ROLLBACK;

Step 3: Example Table Setup


Let’s start with a table called Accounts to demonstrate transactions.

CREATE TABLE Accounts (


AccountID INT,
AccountName NVARCHAR(50),
Balance DECIMAL(10, 2)
);

Now, let’s add some sample data to Accounts.

INSERT INTO Accounts (AccountID, AccountName, Balance)


VALUES
(1, 'John Doe', 1000.00),
(2, 'Jane Smith', 500.00);
Step 4: Example of a Successful Transaction
Suppose we want to transfer $200 from John Doe’s account to Jane Smith’s account.
To ensure that both deductions and additions happen correctly, we’ll use a
transaction.

BEGIN TRANSACTION;

-- Deduct $200 from John Doe's account


UPDATE Accounts
SET Balance = Balance - 200
WHERE AccountID = 1;

-- Add $200 to Jane Smith's account


UPDATE Accounts
SET Balance = Balance + 200
WHERE AccountID = 2;

-- Commit the transaction


COMMIT;

Explanation:
- BEGIN TRANSACTION;: Starts a transaction.
- UPDATE Accounts SET Balance = Balance - 200 WHERE AccountID = 1;: Deducts $200
from John Doe’s account.
- UPDATE Accounts SET Balance = Balance + 200 WHERE AccountID = 2;: Adds $200 to
Jane Smith’s account.
- COMMIT;: Finalizes the transaction, saving all changes.

If all steps execute successfully, the changes are saved.

Step 5: Example of a Failed Transaction with ROLLBACK


Let’s say something goes wrong during the transfer (for example, an error occurs).
We can use ROLLBACK to undo the transaction.

BEGIN TRANSACTION;

BEGIN TRY
-- Deduct $200 from John Doe's account
UPDATE Accounts
SET Balance = Balance - 200
WHERE AccountID = 1;

-- Add $200 to Jane Smith's account (Let's say an error happens here)
UPDATE Accounts
SET Balance = Balance + 200
WHERE AccountID = 2;

-- If all goes well, commit the transaction


COMMIT;
END TRY
BEGIN CATCH
-- If an error occurs, rollback the transaction
ROLLBACK;
PRINT 'Transaction failed. All changes have been undone.';
END CATCH;

Explanation:
- BEGIN TRY ... END TRY: Code within the TRY block attempts to run the transaction.
- BEGIN CATCH ... END CATCH: If any error occurs, the CATCH block rolls back the
transaction.
- ROLLBACK;: Undoes all changes made in the transaction if there is an error.

This ensures that if anything goes wrong, all changes are canceled, and the
database is left in a consistent state.

Note
1. Start a Transaction with BEGIN TRANSACTION.
2. Write Operations: Perform your updates, inserts, or deletes.
3. Commit or Rollback:
- Use COMMIT to save changes if everything succeeds.
- Use ROLLBACK to cancel changes if any part fails (usually with TRY and CATCH
blocks).

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy