PostgreSQL TRANSACTION
A TRANSACTION is a logical unit in the execution process of a database management system, consisting of a finite sequence of database operations.
A database transaction usually contains a sequence of read/write operations on the database. It has the following two purposes:
- It provides a way to recover a sequence of database operations from failure to a normal state, and also provides a way for the database to maintain consistency even in abnormal states.
- When multiple applications access the database concurrently, it provides a method of isolation between these applications to prevent their operations from interfering with each other.
When a transaction is submitted to the database management system (DBMS), the DBMS must ensure that all operations in the transaction complete successfully and their results are permanently saved in the database. If some operations in the transaction do not complete successfully, all operations in the transaction must be rolled back to the state before the transaction was executed. At the same time, the transaction has no effect on the database or the execution of other transactions, and all transactions appear to run independently.
Properties of transactions
Transactions have the following four standard properties, commonly abbreviated as ACID:
- Atomicity: A transaction is executed as a whole; the database operations contained in it are either all executed or none are executed.
- Consistency: A transaction should ensure that the database state transitions from one consistent state to another. A consistent state means the data in the database satisfies integrity constraints.
- Isolation: When multiple transactions execute concurrently, the execution of one transaction should not affect the execution of others.
- Durability: Changes made to the database by a committed transaction should be permanently saved in the database.
Example
Someone wants to buy something worth 100 yuan in a store using electronic money, which involves at least two operations:
- The person's account is reduced by 100 yuan.
- The store's account is increased by 100 yuan.
A transaction-capable database management system ensures that the above two operations (the entire "transaction") both complete or are both cancelled; otherwise, the 100 yuan could seemingly disappear or appear out of nowhere.
Transaction control
Use the following commands to control transactions:
Transaction control commands are used only with INSERT, UPDATE, and DELETE. They cannot be used when creating or dropping tables, because these operations are auto-committed in the database.
BEGIN TRANSACTION command
A transaction can be started with the BEGIN TRANSACTION command or the simpler BEGIN command. Such transactions usually continue until the next COMMIT or ROLLBACK command is encountered. However, transaction processing also rolls back when the database is closed or an error occurs. The following is the simple syntax for starting a transaction:
BEGIN;
或者
BEGIN TRANSACTION;
COMMIT command
The COMMIT command is the transaction command used to save the changes made by a transaction to the database, i.e., to confirm the transaction.
The syntax of the COMMIT command is as follows:
COMMIT;
或者
END TRANSACTION;
ROLLBACK command
The ROLLBACK command is the transaction command used to undo a transaction that has not yet been saved to the database, i.e., to roll back the transaction.
The syntax of the ROLLBACK command is as follows:
ROLLBACK;
Example
Create the COMPANY table (Download the COMPANY SQL file ), with the following data:
runoobdb# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
(7 rows)
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.
runoobdb=# BEGIN;
DELETE FROM COMPANY WHERE AGE = 25;
ROLLBACK;
Check the COMPANY table, which still has the following records:
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
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.
runoobdb=# BEGIN;
DELETE FROM COMPANY WHERE AGE = 25;
COMMIT;
Check the COMPANY table; the record has been deleted:
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
3 | Teddy | 23 | Norway | 20000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | Houston | 10000
(5 rows)