Transactions (BEGIN/COMMIT/ROLLBACK) का उपयोग
एक transaction statements को group करता है ताकि वे सब साथ succeed हों या सब undo हो जाएँ।
In this page:
Syntax
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)
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
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- transactions को लंबे समय तक open छोड़ना
- एक error के बाद roll back करना भूल जाना
- यह मान लेना कि 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: