English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A transaction is a unit of work performed on a database. A transaction is a unit or sequence of work completed in logical order, whether manually completed by the user or automatically completed by some database program.
A transaction is the propagation of one or more changes to the database. For example, if you need to create, update, or delete records in a table, you will perform a transaction on the table. It is important to control transactions to ensure data integrity and handle database errors.
In fact, you will group many SQLite queries into a set and execute them as part of a transaction.
A transaction has the following four standard properties, usually represented by the acronym ACID.
Atomicity:Ensure that all operations within the work unit are successfully completed; otherwise, the transaction will terminate when a failure occurs, and the previous operations will also roll back to the previous state.
Consistency (Consistency):Ensure that the database correctly changes the state on transactions that have been successfully committed.
Isolation (Isolation):Make transaction operations independent and transparent.
Durability (Durability):Ensure that the results or effects of committed transactions are still present in the event of a system failure.
Here are the following commands used to control transactions:
BEGIN TRANSACTION −Start a transaction.
COMMIT−To save changes, you can also useEND TRANSACTIONCommand.
ROLLBACK −Undo changes.
Transaction control commands are only used with DML commands INSERT, UPDATE, and DELETE. They cannot be used when creating or deleting tables, as these operations are automatically committed in the database.
You can use BEGIN TRANSACTION or simply use BEGIN command to start a transaction. Such transactions usually last until the next COMMIT or ROLLBACK command is encountered. However, if the database is closed or an error occurs, the transaction will also be rolled back. Here is the simple syntax for starting a transaction.
BEGIN; or BEGIN TRANSACTION;
The COMMIT command is a transactional command used to save the changes made by transaction calls to the database.
Since the last COMMIT or ROLLBACK command, the COMMIT command will save all transactions to the database.
Here is the syntax of the COMMIT command.
COMMIT; or END TRANSACTION;
The ROLLBACK command is a transactional command used to undo transactions that have not been saved to the database.
Since the last COMMIT or ROLLBACK command, the ROLLBACK command can only be used to undo transactions.
Here is the syntax of the ROLLBACK command.
ROLLBACK;
Online Example
the COMPANY table with 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 from age = 25delete records from the table. Then, use the ROLLBACK command to undo all changes.
sqlite> BEGIN; sqlite> DELETE FROM COMPANY WHERE AGE = 25; sqlite> ROLLBACK;
Now, if you check the COMPANY table, it 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
Let's start another transaction and from AGE = 25delete records from the table, and finally we use the COMMIT command to submit all changes.
sqlite> BEGIN; sqlite> DELETE FROM COMPANY WHERE AGE = 25; sqlite> COMMIT;
If the COMPANY table still has the following records now-
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