← Back to MySQL Course | Chapter 17: Transactions | Lesson 1 of 4

Transaction Basics

एक transaction एक all-or-nothing deal जैसा है: steps का एक set जो या तो सब साथ हो जाते हैं या कोई भी नहीं होता।
Syntax
sql
START TRANSACTION;
-- SQL statements
COMMIT;

एक Transaction क्या है?

एक transaction कई SQL statements को एक all-or-nothing unit में group करता है: या तो इसके अंदर हर statement succeed करके साथ save होता है, या पूरा group roll back हो जाता है ऐसे जैसे इसमें से कुछ कभी चला ही न हो।

उदाहरण: What is a Transaction?

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

एक Transaction शुरू करना

START TRANSACTION उस group की शुरुआत mark करता है, जो हर statement को तुरंत save करने के MySQL के usual behavior को suspend करता है ताकि आप control कर सकें कि पूरा batch exactly कब permanent बने।

उदाहरण: Starting a Transaction

sql
CREATE TABLE accounts (id INT, balance INT);
INSERT INTO accounts VALUES (1, 500), (2, 200);
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Changes Commit करना

COMMIT transaction शुरू होने के बाद किए गए हर change को एक साथ permanent बना देता है। एक बार committed हो जाने पर, वे changes durable होते हैं और rollback से अब undo नहीं किए जा सकते।

उदाहरण: Committing Changes

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Changes Rollback करना

ROLLBACK transaction शुरू होने के बाद किया गया हर change discard कर देता है, database को exactly उस state में restore करते हुए जिसमें यह transaction शुरू होने से पहले था — जब बीच में कुछ गलत हो जाए तब उपयोगी।

उदाहरण: Rolling Back Changes

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Something went wrong -- undo it
ROLLBACK;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Autocommit Mode

Default रूप से MySQL autocommit mode में चलता है, execute होते ही हर individual statement save करते हुए। statements को एक explicit transaction में wrap करना उसे temporarily disable कर देता है ताकि कई changes साथ commit किए जा सकें।

उदाहरण: Autocommit Mode

sql
SELECT @@autocommit;
SET autocommit = 0;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

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. COMMIT भूल जाना, ताकि दूसरे sessions changes कभी न देखें, और connection बंद करना उन्हें roll back कर दे।
  2. MyISAM जैसा एक non-transactional table type इस्तेमाल करना, ताकि ROLLBACK कुछ undo न करे (InnoDB इस्तेमाल करें)।
  3. बीच में CREATE TABLE जैसा एक DDL statement चलाना, जो एक implicit commit force करता है।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.