ACID - atomicity
ACID - Atomicity
Introduction to Atomicity
Atomicity is one of the key properties of the ACID model in database systems. It ensures that a series of operations within a transaction are completed successfully as a single unit. If any operation fails, the entire transaction is aborted, and the database state is left unchanged.
Example Scenario
In this example, we will consider a transaction involving two users. We are withdrawing funds from the balance of user id=1 and crediting those funds to the balance of user id=2. Although these are two separate operations, if either of them fails, it would compromise the integrity of the data.
Transaction Implementation
To maintain atomicity, we encapsulate these operations within a single transaction. According to the principle of Atomicity, all operations must complete successfully. If funds are deducted from id=1 but an error occurs while crediting id=2, a ROLLBACK will occur, reverting the database to its initial state.
BEGIN;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.