← Back to PostgreSQL Course | Chapter 10: Advanced Features | Lesson 5 of 7

Transactions (BEGIN/COMMIT/ROLLBACK) का उपयोग

एक transaction statements को group करता है ताकि वे सब साथ succeed हों या सब undo हो जाएँ।
Syntax
sql
BEGIN;
-- statements
COMMIT;

BEGIN;
-- statements
ROLLBACK;

Transactions (BEGIN/COMMIT/ROLLBACK)

BEGIN एक transaction शुरू करता है, COMMIT इसके changes permanent बनाता है, और ROLLBACK इन्हें discard करता है। PostgreSQL ACID guarantees देता है, और DDL statements भी transactional हैं।

SAVEPOINT partial rollbacks allow करता है। Isolation levels (read committed default से, repeatable read, serializable) control करते हैं कि concurrent transactions क्या देखते हैं।

Note: BEGIN के बाहर हर statement इसका अपना automatic transaction है।

उदाहरण: Transactions (BEGIN/COMMIT/ROLLBACK)

sql
CREATE TABLE accounts (id INTEGER PRIMARY KEY, owner TEXT, balance INTEGER);
INSERT INTO accounts VALUES (1, 'Ada', 100), (2, 'Bob', 50);
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
UPDATE accounts SET balance = balance + 30 WHERE id = 2;
COMMIT;
SELECT owner, balance FROM accounts ORDER BY id;
BEGIN;
UPDATE accounts SET balance = 0 WHERE id = 1;
ROLLBACK;
SELECT owner, balance FROM accounts ORDER BY id;

-- Output:
-- owner | balance
-- Ada | 70
-- Bob | 80
-- owner | balance
-- Ada | 70
-- Bob | 80
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. transactions को लंबे समय तक open छोड़ना
  2. एक error के बाद roll back करना भूल जाना
  3. यह मान लेना कि BEGIN के अंदर autocommit काम करता है
चैप्टर सारांश
  • BEGIN, COMMIT, ROLLBACK commands इस्तेमाल होते हैं
  • सब कुछ या कुछ नहीं
  • ACID guarantees मिलती हैं
  • Isolation levels concurrency control करते हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.