100 Days of SQL

sql

Day 85 – Transactions

These are a series of SQL statements that are executed as a single, atomic operation. Transactions are used to ensure data integrity by ensuring that either all of the SQL statements in the transaction are executed, or none of them are.

BEGIN TRANSACTION
UPDATE Customers SET CreditLimit = CreditLimit + 5000 WHERE CustomerID = 1
INSERT INTO Orders (CustomerID, OrderDate, OrderAmount) VALUES (1, GETDATE(), 1000)
COMMIT TRANSACTION

In this example, a transaction is used to update the credit limit of a customer and insert a new order record into the Orders table. The BEGIN TRANSACTION, COMMIT TRANSACTION, and ROLLBACK TRANSACTION statements are used to control the transaction.