PostgreSQL Commit

Transaction Control:
There are following commands used to control transactions:
  • BEGIN TRANSACTION: to start a transaction.
  • COMMIT: to save the changes, alternatively you can use END TRANSACTION command.
  • ROLLBACK: to rollback the changes.
Transactional control commands are only used with the DML commands INSERT, UPDATE and DELETE only. They can not be used while creating tables or dropping them because these operations are automatically committed in the database.

The BEGIN TRANSACTION Command:
Transactions can be started using BEGIN TRANSACTION or simply BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command is encountered. But a transaction will also ROLLBACK if the database is closed or if an error occurs.

Following is the simple syntax to start a transaction:
BEGIN;
or
BEGIN TRANSACTION;
The COMMIT Command:
  • The COMMIT command is the transactional command used to save 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 for COMMIT command is as follows:
COMMIT;
or
END TRANSACTION;


No comments:

Post a Comment