SQLite Transaction (Transaction)
A transaction is a unit of work performed against a database. A transaction is a unit or sequence of work completed in a logical order, which can be performed manually by a user or automatically by some kind of database program.
A transaction refers to one or more extensions that change the database. For example, if you are creating a record, updating a record, or deleting a record from a table, you are performing a transaction on that table. It is important to control transactions to ensure data integrity and handle database errors.
In practice, you can combine many SQLite queries into a group and execute them all together as part of a transaction.
Properties of transactions
A transaction has the following four standard properties, usually referred to by the acronym ACID:
-
Atomicity:Ensures that all operations within a unit of work are completed successfully; otherwise, the transaction is terminated at the point of failure and previous operations are rolled back to their former state.
-
Consistency:Ensures that the database properly changes state upon a successfully committed transaction.
-
Isolation:Makes transaction operations independent of and transparent to each other.
-
Durability:Ensures that the results or effects of a committed transaction persist in the event of a system failure.
Transaction control
Use the following commands to control transactions:
-
BEGIN TRANSACTION: Begin a transaction.
-
COMMIT: Save the changes, or you can use END TRANSACTION command.
-
ROLLBACK: Roll back the changes made.
Transaction control commands are used only with the DML commands INSERT, UPDATE, and DELETE. They cannot be used when creating or dropping tables, because those operations are auto-committed in the database.
BEGIN TRANSACTION command
A transaction can be started with the BEGIN TRANSACTION command or simply the BEGIN command. Such transactions usually continue until the next COMMIT or ROLLBACK command is encountered. However, transactions are also rolled back when the database is closed or an error occurs. The following is the simple syntax to start a transaction:
BEGIN;
or
BEGIN TRANSACTION;
COMMIT command
The COMMIT command is the transaction command used to save the changes invoked by a transaction to the database.
The COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.
The syntax of the COMMIT command is as follows:
COMMIT;
or
END TRANSACTION;
ROLLBACK command
The ROLLBACK command is the transaction command used to undo transactions that have not yet been saved to the database.
The ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.
The syntax of the ROLLBACK command is as follows:
ROLLBACK;
Example
Assume the COMPANY table has the following records:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
Now, let's start a transaction and delete the record with age = 25 from the table; finally, we use the ROLLBACK command to undo all changes.
sqlite> BEGIN;
sqlite> DELETE FROM COMPANY WHERE AGE = 25;
sqlite> ROLLBACK;
Check the COMPANY table, which still has the following records:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
Now, let's start another transaction, delete the record with age = 25 from the table, and finally use the COMMIT command to commit all changes.
sqlite> BEGIN;
sqlite> DELETE FROM COMPANY WHERE AGE = 25;
sqlite> COMMIT;
Examine the COMPANY table, which has the following records:
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
3 Teddy 23 Norway 20000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0