Database System
Database System
What is a Database?
A database is a structured collection of data that allows for easy storage, retrieval, and
management.
File-Based Systems
Database Architecture
What is Database Architecture?
Database architecture defines how data is stored, organized, and accessed in a database system.
It helps in designing the structure for managing data efficiently.
2. Conceptual Schema
3. External Schema
Data Independence
• Definition: The ability to change the data structure without affecting how applications use that
data.
• Types:
o Logical Independence: Changes to the logical structure (like adding fields) don’t
affect data access.
o Physical Independence: Changes in data storage methods (like using different servers)
don’t impact the logical structure.
• Definition: A way to organize data into tables (relations) that can relate to each other.
• Structure: Data is stored in rows and columns, making it easy to understand and manipulate.
Attributes
Schemas
Tuples
Domains
Relation Instances
Keys of Relations
• Definition: Attributes (or combinations of attributes) that uniquely identify each tuple (row) in
a relation.
• Types:
o Primary Key: The main key that uniquely identifies each record.
o Foreign Key: An attribute that creates a link between two tables.
Integrity Constraints
• Definition: Rules that ensure data accuracy and consistency in the database.
• Types:
o Domain Constraints: Ensure that values in a column fall within a specified range.
o Entity Integrity: Ensures that primary keys are unique and not null.
o Referential Integrity: Ensures that foreign keys match primary keys in related tables.
Relational Algebra
Selection
• Definition: An operation in relational algebra that retrieves rows from a relation that meet
specific criteria.
• Example: Selecting all students with an age greater than 18.
Normalization
• Definition: The process of organizing a database to reduce redundancy and improve data
integrity.
• Purpose: Helps eliminate data anomalies and ensures that the database structure is efficient.
Functional Dependencies
• Definition: A relationship between two attributes, typically between a key and a non-key
attribute, where one attribute's value determines another's.
• Example: If "StudentID" determines "StudentName," then knowing the StudentID allows you
to find the corresponding StudentName.
Normal Forms
Entity-Relationship Model
• Definition: A conceptual framework used to model the data and relationships within a
database. It helps visualize the structure and organization of data.
Entity Sets
• Definition: A collection of similar types of entities. Each entity is an object or thing in the real
world that can be distinctly identified.
• Example: An "Employee" entity set might include all employees in a company.
Attributes
Relationships
• Definition: Associations between two or more entity sets. They describe how entities interact
with each other.
• Example: An "Employee" might have a relationship with a "Department" entity, indicating
which department the employee belongs to.
What is SQL?
• Definition: SQL is a standard programming language used to manage and manipulate relational
databases.
• Purpose: It allows users to create, read, update, and delete (CRUD) data in a database.
Filtering Data
Sorting Data
Grouping Data
• GROUP BY: Used to group rows that have the same values in specified columns.
o Example: SELECT COUNT(*) FROM Students GROUP BY Age;
Joining Tables
• JOIN: Combines rows from two or more tables based on a related column.
o Example:
sql
SELECT Students.Name, Courses.CourseName
FROM Students
JOIN Enrollments ON Students.StudentID = Enrollments.StudentID;
sql
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
Joining Tables
Joins are used to combine rows from two or more tables based on a related column.
Types of Joins
1. INNER JOIN
o Definition: Returns only the rows that have matching values in both tables.
o Example:
sql
SELECT Students.Name, Courses.CourseName
FROM Students
INNER JOIN Enrollments ON Students.StudentID =
Enrollments.StudentID
INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
sql
SELECT Students.Name, Courses.CourseName
FROM Students
LEFT JOIN Enrollments ON Students.StudentID =
Enrollments.StudentID;
sql
SELECT Students.Name, Courses.CourseName
FROM Students
RIGHT JOIN Enrollments ON Students.StudentID =
Enrollments.StudentID;
sql
SELECT Students.Name, Courses.CourseName
FROM Students
FULL OUTER JOIN Enrollments ON Students.StudentID =
Enrollments.StudentID;
Subqueries
Subqueries are queries nested inside another SQL query. They can be used to provide data for
the outer query.
Types of Subqueries
1. Single-Row Subquery
o Definition: Returns one row and can be used with comparison operators.
o Example:
sql
SELECT Name
FROM Students
WHERE Age = (SELECT MAX(Age) FROM Students);
2. Multiple-Row Subquery
o Definition: Returns multiple rows and used with operators like IN, ANY, or ALL.
o Example:
sql
SELECT Name
FROM Students
WHERE StudentID IN (SELECT StudentID FROM Enrollments WHERE
CourseID = 1);
3. Correlated Subquery
o Definition: Refers to columns in the outer query and is executed for each row of the
outer query.
o Example:
sql
SELECT Name
FROM Students s
WHERE EXISTS (SELECT * FROM Enrollments e WHERE e.StudentID =
s.StudentID);
Grouping
• Definition: Grouping is used to combine rows that have the same values in specified columns.
• Command: GROUP BY
• Example:
sql
SELECT Age, COUNT(*)
FROM Students
GROUP BY Age;
o This counts how many students there are for each age.
Aggregation
Example of Aggregation
sql
SELECT Age, AVG(Grade)
FROM Students
GROUP BY Age;
Importance
• Prevents issues like lost updates and dirty reads when multiple users access the database at the
same time.
Key Concepts
Database Backup
• Definition: A backup is a copy of the database that can be used to restore data in case of loss or
corruption.
• Types:
1. Full Backup: A complete copy of the entire database.
2. Incremental Backup: Only the data that has changed since the last backup is saved.
3. Differential Backup: All changes made since the last full backup are saved.
Importance of Backups
• Protects against data loss from hardware failures, accidental deletions, or disasters.
• Ensures business continuity by allowing restoration of data.
Database Recovery
• Definition: The process of restoring the database from a backup after a failure.
• Methods:
1. Point-in-Time Recovery: Restores the database to a specific moment before an error
occurred.
2. Crash Recovery: Automatically restores the database to the last consistent state after a
failure.
Recovery Strategies
• Definition: An index is a data structure that improves the speed of data retrieval
operations on a database table.
• Purpose: Makes searching for specific rows faster, similar to an index in a book.
• Types:
1. Single-column Index: Created on a single column for faster searches.
2. Composite Index: Created on multiple columns to improve query performance
involving those columns.
3. Unique Index: Ensures that all values in the indexed column(s) are unique.
• Benefits:
o Faster query performance.
o Efficient data retrieval.
• Drawbacks:
o Increased storage space.
o Slower write operations, as the index must be updated with each insert, update, or
delete.
NoSQL Systems
• Definition: NoSQL (Not Only SQL) systems are databases designed for unstructured or
semi-structured data, allowing for flexible data models.
• Types:
1. Document Stores: Store data as documents (e.g., JSON), like MongoDB.
2. Key-Value Stores: Store data as key-value pairs, like Redis.
3. Column-family Stores: Organize data into columns rather than rows, like Cassandra.
4. Graph Databases: Use graph structures for data representation, like Neo4j.
• Benefits:
o Scalability: Can handle large volumes of data and traffic.
o Flexibility: Allows for varied data types and structures.
o High performance: Optimized for read and write operations.
• Use Cases:
o Big data applications.
o Real-time web applications.
o Content management systems.
PAST PAPERS
Question 2
b) Command Driven Interface: A user interface where users interact with the system by
entering commands.
Question 3
A relational system is a database management system (DBMS) that stores data in tables related
to each other through common fields.
Relational Systems:
Non-Relational Systems:
Question 4
Question 5
Anomalies in RDBMS: