postgresqlHow do I use a PostgreSQL transaction?
A PostgreSQL transaction is a sequence of SQL statements that will either all succeed or all fail.
To use a PostgreSQL transaction, you must first begin the transaction with the BEGIN command.
BEGIN;
Then, you can execute any number of SQL statements that you desire. For example:
INSERT INTO table1 (col1, col2) VALUES (val1, val2);
UPDATE table2 SET col3 = val3 WHERE col4 = val4;
Once all the desired statements have been executed, you must either commit the transaction with the COMMIT command or rollback the transaction with the ROLLBACK command.
COMMIT;
If the COMMIT command is executed, all the statements within the transaction will be executed. If the ROLLBACK command is executed, none of the statements within the transaction will be executed.
Helpful links
More of Postgresql
- How can I monitor PostgreSQL performance using Zabbix?
- How can I retrieve data from PostgreSQL for yesterday's date?
- How can Zalando use PostgreSQL to improve its software development?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I set a PostgreSQL interval to zero?
- How do I use PostgreSQL's XMIN and XMAX features?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL and ZFS together?
- How do I use PostgreSQL's ON CONFLICT DO NOTHING clause?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
See more codes...