Using transactions in SQL CLI

In this blog post, we will explore how to work with transactions in the SQL CLI (Command Line Interface). Transactions are an indispensable feature in databases that ensure data integrity and consistency. By grouping a set of database operations into a single transaction, we can guarantee that either all the operations will be successfully completed or none of them will be applied.

Table of Contents

Introduction to Transactions

In SQL, a transaction is a sequence of SQL statements that are executed as a single unit. The statements within a transaction can include any data manipulation language (DML) or data definition language (DDL) operations, like SELECT, INSERT, UPDATE, DELETE, CREATE, etc. Transactions are typically used when we need to ensure the atomicity, consistency, isolation, and durability (ACID) of our database operations.

Starting a Transaction

In SQL CLI, we can start a transaction using the BEGIN TRANSACTION command. This command marks the starting point of a transaction and ensures that all subsequent operations are treated as part of the same transaction. Until the transaction is committed or rolled back, the changes made within it are not visible to other concurrent transactions.

BEGIN TRANSACTION;

Committing and Rolling Back Transactions

After executing a set of SQL statements within a transaction, we have two options. We can either commit the transaction, making all the changes permanent, or roll it back, undoing all the changes made within the transaction.

To commit a transaction, we use the COMMIT command:

COMMIT;

To roll back a transaction, we use the ROLLBACK command:

ROLLBACK;

Example Usage

Let’s say we have a database table called “orders” and we need to insert a new order and update the inventory count in a transaction. Here’s an example of how we can achieve that:

BEGIN TRANSACTION;

INSERT INTO orders (order_number, customer_name, total_amount) 
VALUES ('123456', 'John Doe', 150.00);

UPDATE inventory 
SET quantity = quantity - 1
WHERE product_id = 'P123';

COMMIT;

In this example, if any of the statements fail to execute successfully, the entire transaction will be rolled back, ensuring the consistency of our data.

Conclusion

Using transactions in the SQL CLI provides a powerful mechanism for maintaining data integrity and consistency. By grouping related database operations into a single transaction, we can ensure that our changes are applied atomically, providing ACID properties to our application. Transactions are essential for numerous real-world scenarios such as financial transactions, inventory management, and order processing.