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

SAVEPOINT

एक SAVEPOINT एक लंबे game में एक bookmark जैसा है जो आपको पूरे level को restart करने के बजाय एक middle point पर वापस जाने देता है।
Syntax
sql
SAVEPOINT savepoint_name;
ROLLBACK TO SAVEPOINT savepoint_name;
RELEASE SAVEPOINT savepoint_name;

एक SAVEPOINT क्या है?

एक SAVEPOINT एक लंबे transaction के बीच में रखे एक bookmark जैसा काम करता है, आपको बाद में उसी transaction में पहले हुई हर चीज़ discard किए बिना exactly उस point पर roll back करने देते हुए।

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

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
SAVEPOINT after_withdrawal;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;

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

एक SAVEPOINT पर Rolling Back करना

ROLLBACK TO एक specific savepoint के बाद हुई हर चीज़ undo करता है साथ ही इससे पहले किए गए changes preserve करते हुए, आपको पूरे transaction के एक all-or-nothing rollback के बजाय fine-grained control देते हुए।

उदाहरण: Rolling Back to a SAVEPOINT

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
SAVEPOINT after_withdrawal;
UPDATE accounts SET balance = balance + 999 WHERE id = 2;
ROLLBACK TO after_withdrawal;
COMMIT;

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

एक SAVEPOINT Release करना

RELEASE SAVEPOINT बिना किसी data save या undo किए एक ऐसा savepoint हटाता है जिसकी अब ज़रूरत नहीं — यह बस उस particular checkpoint track करने के लिए MySQL इस्तेमाल कर रहे resources free कर देता है।

उदाहरण: Releasing a SAVEPOINT

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
SAVEPOINT after_withdrawal;
RELEASE SAVEPOINT after_withdrawal;
COMMIT;

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

एक Transaction में कई SAVEPOINTs

एक single transaction में कई savepoints हो सकते हैं, जो multi-step processes के लिए उपयोगी है जहाँ आपको exactly यह ढूँढने के लिए incrementally पीछे step करना पड़ सकता है कि कहाँ कुछ गलत हुआ।

उदाहरण: Multiple SAVEPOINTs in One Transaction

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
SAVEPOINT step1;
UPDATE accounts SET balance = balance - 20 WHERE id = 1;
SAVEPOINT step2;
ROLLBACK TO step1;
COMMIT;

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

SAVEPOINT Lifespan और Limits

Savepoints सिर्फ अपने transaction की lifetime तक exist करते हैं — transaction के एक COMMIT या एक पूरे ROLLBACK के साथ खत्म होते ही, इसके अंदर का हर savepoint automatically discard हो जाता है।

उदाहरण: SAVEPOINT Lifespan and Limits

sql
START TRANSACTION;
SAVEPOINT step1;
COMMIT;
-- step1 no longer exists here -- savepoints don't survive past COMMIT/ROLLBACK

⚠️ 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. यह उम्मीद करना कि एक savepoint पर ROLLBACK TO transaction खत्म कर देगा, जबकि transaction खुला रहता है और आपको अभी भी COMMIT चाहिए।
  2. एक ऐसे savepoint पर rollback करना जो पहले से released हो चुका हो या exist न करे, जो एक error देता है।
  3. एक plain ROLLBACK इस्तेमाल करना जब सिर्फ काम का एक हिस्सा undo होना चाहिए, जो सब कुछ cancel कर देता है।
🔒

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.