Transactions in SQL

 Transactions in SQL


A transaction is a sequence of SQL statements that are treated as a single unit of work. Transactions can ensure data consistency by allowing multiple SQL statements to be executed as a single atomic operation. Here's an example:

sql


START TRANSACTION;


UPDATE accounts SET balance = balance - 500 WHERE id = 1;

UPDATE accounts SET balance = balance + 500 WHERE id = 2;


COMMIT;

In this example, we're using a transaction to transfer 500 units of currency from account 1 to account 2. The transaction begins with the START TRANSACTION statement, followed by the SQL statements that modify the data. The transaction is then committed with the COMMIT statement, which ensures that both updates are applied as a single unit of work.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...