Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 84

DBMS और MySQL में ACID Properties

ACID का क्या अर्थ है?

ACID database transactions के लिए reliability model है: Atomicity, Consistency, Isolation और Durability। यह समझाती है कि statement fail होने, two users द्वारा same time data update करने, application disconnect या server crash में database का behavior क्या होना चाहिए।

One-line faculty answer: ACID सुनिश्चित करती है कि transaction one unit की तरह complete हो, valid data rules preserve करे, concurrent work के साथ safely interact करे और failure recovery के बाद committed रहे।

ACID SQL पर magic label नहीं है। Reliable behavior InnoDB जैसे transactional engine, correct schema constraints, complete application transaction, suitable isolation और durable server configuration से आता है।

चार ACID Properties एक नज़र में

PropertyCore questionBank-transfer meaning
AtomicityAll work one unit में kept या cancelled?Debit और credit half-complete नहीं रह सकते
ConsistencyDeclared rules और invariants valid?No negative balance; account और ledger references valid
IsolationConcurrent work कैसे interact करता है?Another transfer validation में stale balance use नहीं करे
DurabilitySuccessful commit recovery के बाद बचे?Confirmed transfer restart/crash recovery के बाद रहे

Memorable sequence: all-or-nothing, valid-to-valid, concurrent-but-controlled, committed-and-recoverable

Verified Bank-Transfer Case Study

-- Opening balances
101 Asha  = 5000.00
102 Ravi  = 3000.00
103 Meera = 1200.00
Total     = 9200.00

START TRANSACTION;
SELECT account_id, balance
FROM bank_accounts
WHERE account_id IN (101, 102)
ORDER BY account_id
FOR UPDATE;

UPDATE bank_accounts
SET balance = balance - 1000.00
WHERE account_id = 101 AND balance >= 1000.00;

UPDATE bank_accounts
SET balance = balance + 1000.00
WHERE account_id = 102;

INSERT INTO transfer_ledger
VALUES (9001, 101, 102, 1000.00, 'COMMITTED', CURRENT_TIMESTAMP);

COMMIT;
Final: Asha=4000.00, Ravi=4000.00, Meera=1200.00 Combined total=9200.00; one ledger row exists

Example तभी ACID बनती है जब application every affected-row count check करे और any failure पर rollback करे। Decision logic के लिए full safe transfer workflow पढ़ें।

A — Atomicity: All or Nothing

Atomicity transaction को one indivisible logical unit मानती है। Debit के बाद credit fail हो तो ROLLBACK को uncommitted debit भी remove करनी चाहिए। InnoDB transaction और undo information से uncommitted changes reverse कर सकता है।

START TRANSACTION;
UPDATE bank_accounts SET balance = balance - 500
WHERE account_id = 101;
-- Next required operation fail मान लें।
ROLLBACK;

-- Account 101 pre-transaction value पर वापस।
  • Atomicity यह नहीं: every business task one enormous SQL statement में लिखना।
  • Atomicity requires: correct boundaries और full rollback path।
  • Important limitation: nontransactional table changes fully roll back नहीं होते।
  • DDL caution: अनेक schema statements implicit commit कराती हैं; business DML से बाहर रखें।
Exam phrase: Transaction की either every operation succeeds और commit होती है, या uncommitted effects cancel होते हैं।

C — Consistency: Valid State से Valid State

Consistency का अर्थ successful transaction database integrity rules और application business invariants respect करे। DBMS primary keys, foreign keys, unique rules, NOT NULL और CHECK constraints enforce कर सकता है। जो rules declared नहीं या complex process में फैले हों, application उन्हें enforce करे।

balance DECIMAL(12,2) NOT NULL
  CHECK (balance >= 0)

amount DECIMAL(12,2) NOT NULL
  CHECK (amount > 0)

FOREIGN KEY (from_account)
  REFERENCES bank_accounts(account_id)
RuleBest enforcement
Account ID uniquePRIMARY KEY
Ledger account existsFOREIGN KEY
Stored balance nonnegativeCHECK + transaction logic
Source और destination differentApplication validation या suitable constraint
Money total reconcileTransaction design, audit और reconciliation

Common misconception: ACID incorrect business logic को correct नहीं बनाती। Program wrong rule दे तो perfectly atomic transaction wrong amount commit कर सकती है।

I — Isolation: Controlled Concurrency

Isolation control करती है कि other transactions active होने पर one transaction क्या observe करती है। InnoDB multi-version concurrency control, isolation levels और locks combine करता है। MySQL InnoDB READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ और SERIALIZABLE support करता है; REPEATABLE READ default InnoDB level है।

LevelSimplified learning viewTrade-off
READ UNCOMMITTEDUncommitted changes दिख सकते हैंWeakest protection
READ COMMITTEDEach consistent read उस समय का committed dataOne transaction में changing views
REPEATABLE READConsistent reads one transaction snapshot shareMySQL InnoDB default
SERIALIZABLEMost restrictive standard isolation behaviorMore waiting, lower concurrency
START TRANSACTION;
SELECT account_id, balance
FROM bank_accounts
WHERE account_id IN (101, 102)
ORDER BY account_id
FOR UPDATE;
-- Matching records conflicting changes के लिए
-- COMMIT या ROLLBACK तक locked।

Isolation का अर्थ “no concurrency” नहीं। इसका अर्थ concurrent effects defined rules follow करें। Workload के लिए isolation level select करें और value read करके later update decision लेना हो तो locking reads use करें।

D — Durability: Committed Data Recovery में Survive

COMMIT succeed होने के बाद durability का अर्थ committed result server failure के बाद recoverable रहे। InnoDB redo log, doublewrite mechanism और crash recovery इस property में योगदान देते हैं। Replicated या point-in-time recovery के लिए binary-log और storage configuration भी matter करती है।

  • innodb_flush_log_at_trx_commit redo-log flush behavior affect करता है।
  • sync_binlog binary-log synchronization affect करता है।
  • Storage write caches, OS guarantees और power protection failure behavior influence करते हैं।
  • Backups और tested restores one transaction से आगे deletion, corruption, operational mistakes और disaster से protect करते हैं।
Oversimplify न करें: “COMMIT का मतलब data कभी lose नहीं हो सकता” configuration, hardware failure domains, backup policy और disaster recovery ignore करता है। Durability system property है, केवल SQL keyword नहीं।

Exam Answer, Misconceptions और अभ्यास

Five-mark model answer: ACID properties DBMS transactions reliable बनाती हैं। Atomicity transaction को all-or-nothing बनाती है। Consistency database को integrity rules satisfy करने वाली states के बीच move करती है। Isolation isolation level के अनुसार concurrent transactions का interference control करती है। Durability crash recovery में committed results preserve करती है। MySQL में InnoDB locking, MVCC, undo, redo और recovery जैसे transactional mechanisms देता है, जबकि schema और application को correct constraints तथा transaction boundaries define करनी होती हैं।

Incorrect statementCorrect understanding
Consistency = सभी replicas instantly identicalACID में consistency valid rules और invariants से जुड़ी है
Isolation = transactions never overlapControlled visibility और locking में overlap हो सकती हैं
Atomicity और durability sameAtomicity complete-unit outcome; durability commit preserve
ACID wrong application logic fix करती हैUnknown intent नहीं, declared rules और mechanics protect करती है

अभ्यास: ACID को school fee payment, online order और seat booking पर map करें। हर case में one invariant, one concurrent race, one rollback point और one durability requirement पहचानें।

Official संदर्भ

Definitions और MySQL implementation notes official MySQL 8.4 manual से check किए गए हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

DBMS में ACID properties क्या हैं?
ACID का अर्थ Atomicity, Consistency, Isolation और Durability है। ये principles errors, concurrency और failures के बीच reliable transaction behavior समझाती हैं।
Atomicity और consistency में क्या अंतर है?
Atomicity तय करती है transaction work one unit की तरह रखा जाएगा या नहीं। Consistency valid states के बीच declared constraints और business invariants preserve करने से जुड़ी है।
क्या ACID का अर्थ transactions हमेशा one-by-one चलती हैं?
नहीं। Transactions concurrently चल सकती हैं। Isolation levels, MVCC और locks तय करते हैं कि कौन-से intermediate effects visible हों और conflicts कब wait करें।
क्या केवल COMMIT हर hardware configuration में durability guarantee करता है?
COMMIT transaction boundary है, लेकिन practical durability InnoDB settings, binary-log settings, storage hardware, OS behavior, backups और power protection पर भी निर्भर करती है।
कौन-सा MySQL engine ACID transactions से closely associated है?
InnoDB MySQL का default general-purpose transactional engine है और transactions, row-level locking, crash recovery, redo तथा undo mechanisms देता है।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.