RDBMS
RDBMS
Types of Failures
1. Transaction Failure: Occurs when a transaction cannot be completed due to logical errors,
system issues, or user errors.
2. System Crash: Results from hardware or software failures, causing the system to halt
unexpectedly.
3. Disk Failure: Includes disk corruption or crashes, leading to loss of data stored on the disk.
Recovery Techniques
There are several techniques to recover a database depending on the type of failure. These
techniques include:
1. Log-Based Recovery
Transaction Logs: Every change made to the database is recorded in a log. The log contains
details such as the transaction ID, operation type, affected data, and the before-and-after
values.
Undo and Redo Operations:
Undo: Reverses uncommitted transactions to ensure atomicity.
Redo: Reapplies committed transactions to ensure durability.
Write-Ahead Logging (WAL): Ensures that log entries are written to stable storage before
the corresponding data changes are made to the database.
2. Checkpoints
A checkpoint is a snapshot of the database's state at a particular point in time.
During recovery, the system starts from the most recent checkpoint, avoiding the need to
process older transactions, thus reducing recovery time.
3. Shadow Paging
In this technique, changes are made to a shadow copy of the database instead of the actual
database.
If a transaction commits successfully, the shadow copy replaces the original database.
Advantages: No need for logs or checkpoints.
Disadvantages: High overhead due to copying large data blocks.
4. Deferred Update
In this method, changes are not written to the database until a transaction commits.
If a failure occurs before the commit, no changes are made, simplifying recovery.
However, this can lead to slower performance for large transactions.
5. Immediate Update
Changes are written to the database as soon as they occur, even before the transaction
commits.
Recovery involves undoing uncommitted changes and redoing committed changes.
Fundamental Operations
The basic operations of relational algebra include selection, projection, union, set difference,
Cartesian product, and renaming. These form the building blocks for more complex queries.
1. Selection (σ)
Filters rows in a relation based on a specified condition.
Syntax: σcondition(R)\sigma_{\text{condition}}(R)
Example: σage>25(Employee)\sigma_{\text{age} > 25}(Employee) selects
employees older than 25.
2. Projection (π)
Extracts specific columns (attributes) from a relation.
Syntax: πattribute1, attribute2, ...(R)\pi_{\text{attribute1, attribute2, ...}}(R)
Example: πname, age(Employee)\pi_{\text{name, age}}(Employee) retrieves only
the name and age of employees.
3. Union (∪)
Combines tuples from two relations and removes duplicates.
Syntax: R∪SR \cup S
Example: Combining the records of two departments in an organization.
Note: Relations must be union-compatible (same attributes and domains).
4. Set Difference (−)
Retrieves tuples present in one relation but not in another.
Syntax: R−SR - S
Example: Employees in RR but not in SS.
5. Cartesian Product (×)
Combines every tuple of one relation with every tuple of another relation.
Syntax: R×SR \times S
Example: Joining two relations where no common attribute exists.
6. Rename (ρ)
Assigns a new name to a relation or its attributes.
Syntax: ρnewName(R)\rho_{\text{newName}}(R)
Example: ρDept(Department)\rho_{\text{Dept}}(Department) renames the
"Department" relation to "Dept".
Derived Operations
Derived operations are built using fundamental operations and simplify query expression.
1. Intersection (∩)
Retrieves common tuples between two relations.
Syntax: R∩SR \cap S
2. Join (⨝)
Combines related tuples from two relations based on a condition.
Types:
Theta Join (⨝θ): Combines tuples matching a condition.
Equi-Join: A special case of theta join where the condition is equality.
Natural Join: Automatically joins relations based on common attributes.
3. Division (÷)
Retrieves tuples from one relation that are related to all tuples in another relation.
Example: Find students who have completed all courses in a given list.
4. Outer Join
Includes tuples that do not have matching pairs in the other relation.
Types: Left Outer Join, Right Outer Join, Full Outer Join.
Example Query
Suppose we have two relations:
Student: {RollNo, Name, Age}\{ \text{RollNo, Name, Age} \}
Marks: {RollNo, Subject, Marks}\{ \text{RollNo, Subject, Marks} \}
Query: Retrieve names of students with marks in "Math".
Conclusion
Relational Algebra is a powerful query language that serves as the theoretical foundation for SQL.
By using its operations, users can construct complex queries to manipulate and retrieve data
efficiently from relational databases.
Constraints
Constraints are rules applied to table columns to maintain data validity and integrity. They prevent
invalid data entry and enforce database consistency.
Types of Constraints
1. Primary Key
Ensures that each row in a table is unique and identifiable.
Example: A "StudentID" column in a "Student" table.
2. Foreign Key
Maintains referential integrity between two tables.
Example: A "CourseID" in an "Enrollment" table references the "Course" table.
3. Unique
Ensures that all values in a column are distinct.
Example: A "Username" column in a "Users" table.
4. Not Null
Ensures that a column cannot have null (empty) values.
Example: A "Name" column in an "Employee" table.
5. Check
Ensures that values meet a specific condition.
Example: Check (Age > 18)\text{Check (Age > 18)}.
6. Default
Assigns a default value to a column if no value is provided.
Example: A "Status" column defaults to "Active".
Triggers
Triggers are database objects that execute automatically in response to certain events such as
INSERT, UPDATE, or DELETE operations.
Features of Triggers
Automated Execution: Triggers run without manual intervention.
Event-Driven: Activated by specific database events.
Enforce Rules: Useful for enforcing complex business logic.
Types of Triggers
1. Before Triggers
Executed before the specified event.
Example: Validate data before insertion.
2. After Triggers
Executed after the specified event.
Example: Log changes to a table.
3. Instead Of Triggers
Used for views to intercept operations like INSERT, UPDATE, or DELETE.
Example: Redirect an INSERT to another table.
Example of a Trigger
Scenario: Automatically log updates to an "Employee" table.
CREATE TRIGGER log_update
AFTER UPDATE ON Employee
FOR EACH ROW
INSERT INTO Employee_Log (EmpID, OldSalary, NewSalary, UpdateTime)
VALUES (OLD.EmpID, OLD.Salary, NEW.Salary, CURRENT_TIMESTAMP);
Comparison
Constraints: Enforce rules passively, ensuring data integrity.
Triggers: Perform active actions based on events, enabling automation.
Conclusion
Constraints and triggers are essential tools for maintaining data integrity and automating actions in
relational databases. Constraints provide a foundation for data validity, while triggers add flexibility
and automation to database management. Together, they ensure robust and consistent database
operations.
Techniques of Denormalization
1. Combining Tables
Merges two or more normalized tables into a single table.
Example: Combining "Customer" and "Order" tables into one.
2. Adding Redundant Columns
Stores additional attributes in a table to avoid joins.
Example: Adding "CustomerName" to the "Order" table.
3. Precomputing Aggregates
Stores calculated values, such as totals or averages, in a table.
Example: Adding "TotalSales" to a "Sales" table.
4. Storing Derived Data
Saves derived values to avoid recalculations.
Example: Storing "Age" instead of recalculating it from the "DateOfBirth".
Advantages of Denormalization
1. Faster query performance.
2. Reduced query complexity.
3. Improved reporting and analytics.
Disadvantages of Denormalization
1. Increased Storage: Redundancy leads to higher storage requirements.
2. Data Inconsistency: Changes in one location might not propagate correctly to redundant
data.
3. Maintenance Challenges: Updates and deletions become more complex due to duplication.
Example
Consider a normalized database with:
Conclusion
Denormalization improves query performance in specific scenarios at the cost of redundancy and
potential inconsistencies. It is a strategic decision based on the system's requirements, balancing
performance with data integrity.
Must be in 1NF.
Non-prime attributes must depend on the entire primary key.
Example:
Order Table:
OrderID OrderDate
OrderDetails Table:
Must be in 2NF.
No transitive dependencies (non-prime attributes shouldn't depend on other non-prime
attributes).
Example:
EmpID DeptID
Department Table:
DeptID DeptName
Must be in 3NF.
Every functional dependency's left side must be a superkey.
Example:
CourseID Instructor
BCNF Tables:
Instructor Table:
Instructor Department
Course Table:
CourseID Instructor
StudentID Course
Student-Hobby Table:
StudentID Hobby
Must be in 4NF.
No join dependency that is not implied by candidate keys.
Example:
| ProjectID | EmployeeID | Role |
5NF Tables:
Project-Employee Table:
ProjectID EmployeeID
Project-Role Table:
ProjectID Role
Conclusion
Normalization reduces redundancy and ensures data integrity. Higher normal forms, like 4NF and
5NF, handle complex dependencies but may increase query complexity.