In database, a transaction is a group of statements that are executed atomically.
Subtopics
- ACID (atomicity, consistency, isolation, durability)
- conflict serializability
In SQL
In SQL, we use begin
and commit
to start and end a transaction:
begin;
-- A group of SQL statements;
commit;
For example, consider the following table: R(id: integer, name: varchar(8))
. If we perform the following transaction:
begin;
insert into R values (1, 'Adam');
insert into R values (1, 'Smith');
commit;
We won’t add anything because of integrity constraint violation.
We can also explicitly use the rollback
keyword to abort a transaction. For example:
begin transaction;
...
if ... then
rollback transaction;
end
commit transaction;