139 Db Notes2
139 Db Notes2
In a database system, most data is stored on secondary storage, like hard drives or
SSDs (referred to here as magnetic disks).Why secondary storage? Because it can
hold large amounts of data, unlike main memory (RAM), which is smaller and
temporary.
However:To do any useful operation (e.g., searching or updating data), the data must
first be loaded into main memory (RAM), because the CPU cannot directly work
with data on disk.
Terminologies to Know:
Blocks:
A block refers to the smallest unit of data that can be read from or written to the
disk. A block corresponds to a fixed-size chunk of data on the storage device
(magnetic disk), typically ranging from 512 bytes to a few kilobytes.
Buffers:
A buffer is a designated area in main memory (RAM) where data from disk blocks is
temporarily stored before processing.
Buffer Manager:
Storage Manager:
It is the job of the storage manager to control the placement of data on disk and its
movement between disk and main memory.
Explanation:
The buffer manager is responsible for dividing the available main memory into
buffers, which are page-sized regions into which disk blocks can be transferred.
Thus, all DBMS components that need information from the disk will interact with
the buffers and the buffer manager, either directly or through the execution
engine. The kinds of information that various components may need include:
2. Metadata: the database schema that describes the structure of, and constraints
on, the database.
3. Log Records: information about recent changes to the database; these support
durability of the database.
4. Statistics: information gathered and stored by the DBMS about data properties
such as the sizes of, and values in. various relations or other components of the
database.
Atomicity:
How It Works:
o A transaction can involve several operations, such as adding, updating, or
deleting data.
o If an operation fails at any point, the entire transaction is rolled back to its
original state. This ensures the database is never left in an inconsistent or
partial state.
o Example:
If the first operation succeeds (deducting from Account A), but the
second operation fails (adding to Account B), the database will
rollback the entire transaction, and no money will be deducted from
Account A or added to Account B. This ensures the atomicity of the
transaction.
Consistency:
Consistency refers to the property that ensures the database transitions from one
valid state to another after a transaction is executed. In other words, after a
transaction, the database must be in a state that adheres to all defined rules,
constraints, and business logic.
Transaction Should Not Violate Rules: During the transaction, the changes
made to the database must not violate the rules or constraints of the database
schema. These could include:
o Data type constraints: For example, ensuring that a column that should only
hold integers doesn't get a string.
o Primary key constraints: Ensuring no two rows have the same primary key.
o Business logic: Ensuring that the data remains meaningful. For example,
ensuring an account doesn't go into a negative balance during a transfer.
Eg:
Before Transaction:
Transaction:
After Transaction:
Account B cannot have more money than allowed (let’s say the maximum limit is
$500).
If there was a rule violation, for example, if the transaction tried to take out
more money from Account A than it had (e.g., $600 instead of $100), the
transaction would violate consistency, and the DBMS would reject the transaction.
Isolation:
Definition: Transactions are isolated from each other. One transaction should not
affect the execution of another transaction, even if they occur concurrently.in
DBMS,isolation is implemented through locking.
How It Works:
Without Lock:
Without Isolation (First Diagram):
WITH LOCK:
2. With Isolation (Second Diagram):
DURABILITY:
Durability ensures that once a transaction is committed, its changes are permanent,
even in the face of failures like power outages or crashes.
Changes are written to non-volatile storage (e.g., disk or SSD) before the
transaction completes.
Example:
Transaction Processing:
Transaction manager is software component of DBMS.It performs following tasks:
1.Logging:
In order to assure durability, every change in the database is logged separately on disk.
The log manager follows one of several policies designed to assure that no matter when
a system failure or "crash" occurs, a recovery manager will be able to examine the log of
changes and restore the database to some consistent state. The log manager initially
writes the log in buffers and negotiates with the buffer manager to make sure that
buffers are written to disk (where data can survive a crash) at appropriate times.
1. The database system uses the log to identify what operations were performed.
o Redo completed transactions: Ensure their effects are fully applied to the
database.
2.Concurrency control:
Transactions must appear to execute in isolation. But in most systems, there will in
truth be many transactions executing simultaneously.So,locks are maintained on
database by scheduler so that there will be no conflict among transactions. The
scheduler affects the execution of queries and other database operations by
forbidding the execution engine from accessing locked parts of the database
3.Deadlock Resolution:
Situations can occur where two transactions have locked resources like transaction
T1 has locked resource R1 and transaction T2 has locked resource R2.Then,T1 also
wants to access R2 which is locked by T2.And at the same time ,T2 while having locked
R2 wants to access R1 which is locked by T1, Both T1 and T2 want each other resources
while don’t want to release the resources they have.This situation is called deadlock
which is solved by the transaction manager which has the responsibility to intervene
and cancel ("rollback" or "abort") one or more transactions to let the others proceed.