BEGIN TRANSACTION Statement
The BEGIN TRANSACTION
statement starts a new transaction explicitly. It groups a set of SQL statements into a single unit, ensuring all or none of the changes are made.
Example: Using BEGIN TRANSACTION
BEGIN TRANSACTION;
INSERT INTO FreedomFighters (Name, City, Contribution) VALUES ('Nila', 'Trichy', 'Healthcare Initiatives');
UPDATE FreedomFighters SET City = 'Erode' WHERE Name = 'Vijay';
COMMIT;
Output:
Transaction started, operations performed, and changes committed successfully.
Do's and Don'ts
Do's
- Use
BEGIN TRANSACTION
to control complex sequences of operations. - Ensure you commit or roll back the transaction to avoid locking resources.
Don'ts
- Don't leave transactions open without a commit or rollback, as it can lead to database locks.
- Don't use transactions unnecessarily for simple operations.