A transaction is a set of operations which might include updating, deleting, inserting, and retrieving data. These operations are often embedded in a higher level language, or can be explicitly wrapped in a transaction block using BEGIN
and END
statements. A transaction is successfully executed if all the operations in the transaction are successfully executed. If an operation in a transaction fails, the effect of the partially executed operation on the transaction can be undone.
To control the beginning and the end of a transaction explicitly, one can use the BEGIN
statement to mark the the start of transaction , and the statements END
or COMMIT
to mark the end of the transaction. The following example shows how to explicitly execute an SQL statement in a transaction:
BEGIN; CREATE TABLE employee (id serial primary key, name text, salary numeric); COMMIT;
One usage of transactions, other than ensuring data integrity, is to provide an easy way to undo changes to the database in...