0% found this document useful (0 votes)
5 views

Lecture 1.1.1

Uploaded by

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

Lecture 1.1.1

Uploaded by

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

Transaction Control - Commit, Rollback, Savepoint

Introduction to Transaction Control

Transaction control commands in SQL are essential for maintaining data integrity and
consistency. Transactions must be atomic, consistent, isolated, and durable (ACID properties).
This lecture will cover the primary transaction control commands: Commit, Rollback, and
Savepoint.

1. Commit

Definition :
The `COMMIT` command is used to save all changes made during the current
transaction permanently. Once committed, these changes are visible to other users and cannot
be undone by a rollback.

Syntax :
COMMIT;

Examples :
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;
COMMIT;
- This statement increases the salary of all employees in department 10 by 10% and then
commits the changes.

Usage :
- Finalizing changes made during data manipulation operations such as `INSERT`,
`UPDATE`, and `DELETE`.
- Ensuring that data modifications are made permanent and visible to other users.

Key Points :
- Once a commit is issued, the changes are irreversible.
- Ensures data consistency by making all modifications made during the transaction
permanent.

2. Rollback

Definition :
- The `ROLLBACK` command is used to undo all changes made during the current
transaction, reverting the database to its last committed state.

Syntax :
ROLLBACK;

Examples :
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;
ROLLBACK;
- This statement increases the salary of all employees in department 10 by 10%, but then
the rollback command undoes this change.
Usage :
- Undoing changes when an error or unexpected result occurs during a transaction.
- Reverting the database to a consistent state after a mistake or unintended modification.

Key Points :
- Rollback can only undo changes made in the current transaction since the last commit.
- Useful for error recovery and maintaining data integrity by discarding erroneous
changes.

3. Savepoint

Definition :
- The `SAVEPOINT` command creates a point within a transaction to which you can
later roll back. This allows for partial rollbacks within a transaction without affecting the
entire transaction.

Syntax :
SAVEPOINT savepoint_name;

Examples :
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;
SAVEPOINT sp1;
UPDATE employees SET salary = salary * 1.2 WHERE department_id = 20;
ROLLBACK TO sp1;
- This sequence of commands updates salaries in department 10, sets a savepoint,
updates salaries in department 20, and then rolls back to the savepoint, undoing only the
second update.

Usage :
- Managing complex transactions by creating multiple savepoints.
- Rolling back part of a transaction to a specific savepoint, leaving earlier changes
intact.

Key Points :
- Multiple savepoints can be set within a single transaction.
- Using `ROLLBACK TO savepoint_name` reverts the transaction to the state at the
specified savepoint.
- The savepoint itself remains after a rollback to it and can be used again unless
explicitly released or the transaction is committed or fully rolled back.

#Detailed Scenario Example

Imagine a banking system where you need to transfer funds between accounts, and this involves
multiple steps:

1. Start Transaction:
BEGIN TRANSACTION;

2. Step 1: Debit Account A:


UPDATE accounts SET balance = balance - 100 WHERE account_id = 'A';

3. Create Savepoint:
SAVEPOINT sp1;

4. Step 2: Credit Account B:


UPDATE accounts SET balance = balance + 100 WHERE account_id = 'B';

5. Encounter an Error:
- Suppose there's an error in the next step (e.g., logging the transaction).

6. Rollback to Savepoint:
ROLLBACK TO sp1;
- This reverts the credit operation without affecting the debit operation.

7. Fix the Error and Retry:


-- Correct the error
UPDATE accounts SET balance = balance + 100 WHERE account_id = 'B';

8. Commit the Transaction:


COMMIT;

## Best Practices for Transaction Management

1. Minimize Transaction Scope:


- Keep transactions short to reduce lock contention and improve performance.
- Commit as soon as all related operations are complete.

2. Use Savepoints Judiciously:


- Use savepoints to manage complex transactions but avoid overusing them, which can add
overhead.

3. Handle Errors Gracefully:


- Implement error handling to catch and manage exceptions, ensuring proper rollback or retry
mechanisms.

4. Consistency and Isolation:


- Ensure transactions maintain data consistency and isolation, following the ACID properties.

## Conclusion

- Transaction control commands are crucial for maintaining data integrity and consistency in
databases.
- `COMMIT` makes changes permanent, `ROLLBACK` undoes changes, and `SAVEPOINT`
allows for partial rollbacks.
- Proper use of these commands ensures robust and reliable database operations.

You might also like