Mark As Completed Discussion

Transactions

In database management, a transaction is a sequence of database operations that are executed as a single unit of work. Transactions ensure data consistency and integrity by allowing multiple operations to be treated as a single, indivisible operation.

The ACID properties (Atomicity, Consistency, Isolation, Durability) define the characteristics of a transaction and ensure that it is executed reliably.

Example

Let's consider an example where we have a database table 'Orders' with a column 'Quantity' representing the quantity of a product ordered by a customer.

We want to update the 'Quantity' column to increase the quantity by 1 for all orders. We can perform this operation within a transaction to ensure data consistency.

TEXT/X-SQL
1-- Begin the transaction
2START TRANSACTION;
3
4-- Update the 'Quantity' column
5UPDATE Orders SET Quantity = Quantity + 1;
6
7-- Commit the transaction
8COMMIT;
JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment