Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 August PostgreSQL: COMMIT, ROLLBACK and SAVEPOINT for Transactions

PostgreSQL: COMMIT, ROLLBACK and SAVEPOINT for Transactions

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Today morning, one of our Associate DB Developer working with Transactions and PostgreSQL is also new for him.
The COMMIT, ROLLBACK and SAVEPOINT are very common for all RDBMS.

In this post, I am sharing one basic demonstration on COMMIT, ROLLBACK and SAVEPOINT of PostgreSQL which helps to Associate level DB Developer.

Create a table with Sample Data:

1
2
3
4
5
6
7
8
9
10
CREATE TABLE tbl_Employees
(
EmpID INT
,EmpName CHARACTER VARYING
);
 
INSERT INTO tbl_Employees
VALUES
(1,'Anvesh'),(2,'Loother'),(3,'Roy'),(4,'Martin')
,(5,'Jenny'),(6,'Monika'),(7,'Rajesh'),(8,'Eric');

Commit: We can use COMMIT to end our all transactions and make It as permanent changes.

Commit the Transaction:

1
2
3
4
5
BEGIN;
UPDATE tbl_Employees
SET EmpName = 'Nivu'
WHERE EmpID = 3;
COMMIT;

Rollback: Using ROLLBACK, we can jump to the previous last committed state of the Transaction.
Rollback the Transaction:

1
2
3
4
5
BEGIN;
UPDATE tbl_Employees
SET EmpName = 'Vinay'
WHERE EmpID = 2;
ROLLBACK;

Savepoint: Using SAVEPOINT, we can jump to the particular point which defines by the SAVEPOINT.

Rollback changes to the Save Point:

1
2
3
4
5
6
7
8
9
10
11
12
BEGIN;
UPDATE tbl_Employees
SET EmpName = 'Grace'
WHERE EmpID = 4;
SAVEPOINT EmpSave;
 
UPDATE tbl_Employees
SET EmpName = 'Kunal'
WHERE EmpID = 5;
ROLLBACK TO EmpSave;

The Result:

1
2
3
4
5
6
7
8
9
10
11
12
SELECT * FROM tbl_Employees ORDER BY 1 ;
 
EmpID EmpName
--------------------
1 Anvesh
2 Loother
3 Nivu
4 Grace
5 Jenny
6 Monika
7 Rajesh
8 Eric

Aug 17, 2016Anvesh Patel
PostgreSQL: Why New User can access all Databases without any GrantsPostgreSQL: Multiple GROUP BY using GROUPING SETS in Single SQL Query
Comments: 1
  1. joy
    June 4, 2018 at 3:22 am

    Nice sharing anvesh

Anvesh Patel
Anvesh Patel

Database Engineer

August 17, 2016 PostgreSQLAnvesh Patel, BEGIN, COMMIT, database, database research and development, dbrnd, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, ROLLBACK, SAVEPOINT
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....