DBMS
DBMS
Data:
Definition: Raw facts and figures that are processed to generate information.
Characteristics:
o Unorganized and unprocessed.
o Forms the basis for generating meaningful insights.
Examples: Numbers (e.g., 100, 200), text (e.g., "John", "Sales"), images, etc.
Database:
Definition: A software tool that facilitates the creation, manipulation, and management
of databases.
Features:
o Data Storage: Efficient storage of large volumes of data.
o Data Retrieval: Quick access to required data using queries.
o Data Security: Prevents unauthorized access.
o Backup and Recovery: Ensures data safety during failures.
o Concurrency Control: Supports multiple users accessing data simultaneously.
Banking:
o Manages customer accounts, balances, and transactions.
o Provides ATMs with real-time updates.
Airlines:
o Handles flight scheduling, ticket booking, and customer records.
o Monitors seat availability dynamically.
Education:
o Maintains student records, grades, and attendance.
o Facilitates online examinations and course management.
Healthcare:
o Manages patient records, prescriptions, and appointment schedules.
o Tracks hospital inventory like medicines and equipment.
Data Abstraction:
Definition: Process of hiding implementation details and showing only essential data.
Levels:
o Physical Level: Describes how data is stored on hardware.
o Logical Level: Represents the structure and organization of data in the database.
o View Level: Shows the data relevant to specific users, hiding other details.
Data Independence:
Definition: Ability to change the schema at one level without altering the schema at
another level.
Types:
o Physical Data Independence: Changes in the physical storage structure do not
affect the logical schema.
o Logical Data Independence: Changes in the logical schema do not affect the
user views.
Database Schema:
Definition: The blueprint of the database that defines how data is organized.
Types:
o Physical Schema: Specifies the physical storage of data.
o Logical Schema: Describes the logical organization of data.
o View Schema: Defines user-specific data representations.
Codd’s Rules:
Components:
o Database Engine: Core service managing storage, processing, and security of
data.
o Query Processor: Interprets and executes user queries efficiently.
o Transaction Manager: Ensures consistency, isolation, and durability of
transactions (ACID properties).
o Storage Manager: Handles data storage, indexing, and access.
o User Interfaces: Allows users to interact with the system.
1.2 Architecture
Two-Tier Architecture:
Definition: Client-Server model with direct interaction between client and database.
Components:
o Client: User interface to send queries and view results.
o Server: Processes the queries and returns data.
Advantages:
o Simple to implement.
o Efficient for small systems.
Disadvantages:
o Limited scalability.
o Security risks due to direct database access.
Three-Tier Architecture:
Definition: Organizes data into tables (relations) with rows (tuples) and columns
(attributes).
Features:
o Uses primary keys to identify records uniquely.
o Enforces constraints for data integrity (e.g., foreign keys).
o SQL (Structured Query Language) is used for querying.
Advantages:
o Simplicity and ease of use.
o Supports complex queries and operations.
o High flexibility in managing data relationships.
Disadvantages:
o Performance overhead for large datasets.
o Requires normalization to reduce redundancy.
Example: Employee table with attributes like ID, Name, Department, and Salary.
Unit - II: Relational Data Model
2.1 Relational Structure
Tables (Relations):
Definition: A table, also known as a relation, is the primary structure used to store and
organize data in a relational database. It is a collection of rows and columns, where each
row represents a record and each column represents an attribute of the data.
Characteristics:
o Each table has a unique name to identify it within the database.
o Tables are used to store data about entities, where each column has a specific data
type.
Example: A "Students" table may have columns for StudentID, Name, Age, and Grade.
Rows (Tuples):
Definition: Rows, also known as tuples, are the individual entries in a table that contain
data about a specific instance of an entity.
Example: A single row in a "Students" table may contain the following data: (101, John
Doe, 21, A).
Key Point: Each row is unique, representing one record in the table.
Domains:
Definition: A domain is the set of all possible values that a column or attribute can take.
It ensures data integrity by restricting values within a predefined range or set.
Example: The domain for an "Age" column might be integers between 0 and 120.
Key Point: Domains help enforce consistency and validation in the database.
Attributes:
Entities:
Definition: Entities are real-world objects or concepts that can have data stored about
them in a database. An entity is represented as a table in the relational model.
Example: Examples of entities include "Student," "Employee," and "Product."
Key Point: Entities have attributes that describe their characteristics.
2.2 Keys
Super Keys:
Definition: A super key is a set of one or more attributes that can uniquely identify a row
in a table.
Characteristics:
o A table can have multiple super keys.
o A super key may contain extra attributes beyond what is necessary for unique
identification.
Example: In a "Students" table, {StudentID}, {StudentID, Name}, and {StudentID,
Name, Age} are all super keys.
Candidate Key:
Definition: A candidate key is a minimal super key, meaning it contains the smallest
number of attributes needed to uniquely identify a row.
Characteristics:
o A table can have multiple candidate keys.
o All attributes in a candidate key are necessary for uniqueness.
Example: In a "Students" table, {StudentID} and {Email} might be candidate keys.
Primary Key:
Definition: A primary key is a candidate key chosen to uniquely identify rows in a table.
Characteristics:
o The primary key must contain unique values for each row.
o It cannot contain NULL values.
Example: In a "Students" table, StudentID is typically chosen as the primary key.
Foreign Key:
Definition: A foreign key is an attribute in one table that establishes a link to the primary
key in another table.
Purpose: Maintains referential integrity between related tables.
Example: In an "Employees" table, DepartmentID can be a foreign key referencing the
primary key in a "Departments" table.
2.3 Data Constraints
Domain Constraints:
Definition: Referential integrity constraints ensure that foreign key values in one table
correspond to primary key values in another table.
Characteristics:
o Prevents invalid references between tables.
o Ensures that related data remains consistent.
Example: If an "Orders" table has a foreign key referencing the "Products" table,
deleting a product must also handle its related orders.
Definition: A strong entity set is an entity that has a primary key and can exist
independently in the database.
Characteristics:
o Each instance of a strong entity set is uniquely identifiable.
o Does not rely on other entities for identification.
Example: An "Employee" entity with EmployeeID as its primary key.
Definition: A weak entity set depends on a strong entity set for its identification. It uses a
foreign key in combination with its attributes to create a composite key.
Characteristics:
o Cannot exist without a related strong entity.
o Requires a "discriminator" attribute to distinguish instances.
Example: A "Dependent" entity related to an "Employee" entity.
Types of Attributes:
ER Diagrams:
Functional Dependencies:
Normal Forms:
Data-types:
Definition: Data-types define the kind of data that can be stored in a column.
Common Data-types:
o Numeric: INTEGER, FLOAT, DECIMAL. Example: The "Age" column in a
"Students" table could use the INTEGER data type.
o Character: CHAR, VARCHAR. Example: The "Name" column in the "Students"
table could use VARCHAR to store text.
o Date and Time: DATE, TIME, TIMESTAMP. Example: A "DateOfBirth"
column could use the DATE type.
o Boolean: TRUE, FALSE. Example: An "IsActive" column could use BOOLEAN
to indicate whether a student is active.
SQL Sub-languages:
Clauses:
2. GROUP BY: Groups rows that have the same values in specified columns.
o Example:
SELECT Age, COUNT(*) FROM Students GROUP BY Age HAVING COUNT(*) >
1;
Joins:
Definition: Combine rows from two or more tables based on related columns.
2. Left Join: Returns all rows from the left table and matching rows from the right table.
o Example:
o SELECT Students.Name, Courses.CourseName
o FROM Students
LEFT JOIN Courses ON Students.CourseID = Courses.CourseID;
3. Right Join: Returns all rows from the right table and matching rows from the left table.
o Example:
o SELECT Courses.CourseName, Students.Name
o FROM Courses
RIGHT JOIN Students ON Courses.CourseID = Students.CourseID;
4. Full Join: Returns all rows when there is a match in either table.
o Example:
o SELECT Students.Name, Courses.CourseName
o FROM Students
FULL OUTER JOIN Courses ON Students.CourseID = Courses.CourseID;
5. Nested Queries: Queries inside another query for complex data retrieval.
o Example:
3.3 Operators
SELECT * FROM Students WHERE Age > 18 AND Age < 25;
Numeric Functions:
String Functions:
Aggregate Functions:
Views:
o Update:
o Drop:
Example: A "StudentView" that shows only students above the age of 18:
CREATE VIEW StudentView AS SELECT Name, Age FROM Students WHERE Age >
18;
Sequences:
o Alter:
o Drop:
o Use:
Indexes:
Advantages of PL/SQL:
Tight Integration with SQL: Combines procedural capabilities with SQL for robust
applications.
Improved Performance: Reduces network traffic by sending entire blocks instead of
multiple queries.
Portability: Supported on various database systems.
Code Reusability: Procedures, functions, and triggers enable reusable code blocks.
Error Handling: Advanced exception handling mechanisms improve reliability.
Anonymous Block:
DECLARE
-- Declarations
BEGIN
-- Executable Statements
EXCEPTION
-- Exception Handling
END;
Components:
o Declare: Defines variables, constants, and cursors.
o Begin: Contains the executable code.
o Exception: Handles runtime errors.
o End: Marks the end of the block.
Variable Declaration:
DECLARE
v_name VARCHAR2(50);
BEGIN
v_name := 'John';
END;
Constant Declaration:
DECLARE
c_tax_rate CONSTANT NUMBER := 0.05;
BEGIN
DBMS_OUTPUT.PUT_LINE(c_tax_rate);
END;
Conditional Control:
IF Statement:
IF condition THEN
-- Code
ELSIF another_condition THEN
-- Code
ELSE
-- Code
END IF;
Iterative Control:
LOOP Statement:
LOOP
-- Code
EXIT WHEN condition;
END LOOP;
FOR LOOP:
FOR i IN 1..10 LOOP
-- Code
END LOOP;
Sequential Control:
GOTO Statement:
GOTO label;
<<label>>
NULL;
4.3 Exception Handling
Predefined Exceptions:
User-Defined Exceptions:
4.4 Cursors
Implicit Cursors:
Explicit Cursors:
Example:
FOR v_student IN cur_students LOOP
DBMS_OUTPUT.PUT_LINE(v_student.name);
END LOOP;
Parameterized Cursors:
Example:
4.5 Procedures
Advantages:
Modular programming.
Reusability of code.
Reduced network traffic.
Create:
CREATE PROCEDURE AddStudent(p_name VARCHAR2, p_age NUMBER) AS
BEGIN
INSERT INTO Students (Name, Age) VALUES (p_name, p_age);
END;
Execute:
Delete:
4.6 Functions
Advantages:
Returns a value.
Can be used in SQL statements.
Enhances reusability and readability.
Create:
CREATE FUNCTION GetStudentAge(p_id NUMBER) RETURN NUMBER AS
v_age NUMBER;
BEGIN
SELECT Age INTO v_age FROM Students WHERE StudentID = p_id;
RETURN v_age;
END;
Execute:
Delete:
Use of Triggers:
Types of Triggers:
Create:
CREATE TRIGGER BeforeInsertStudents
BEFORE INSERT ON Students
FOR EACH ROW
BEGIN
:NEW.CreatedAt := SYSDATE;
END;
Delete:
Create User:
Delete User:
Grant Privileges:
Revoke Privileges:
5.2 Transactions
Concept of Transaction:
States of a Transaction:
Types of Failures:
Causes of Failure:
Power outages
Hardware malfunctions
Software bugs
Human errors
Database Backup:
1. Physical Backup:
o Copies of physical files (e.g., datafiles, control files).
o Suitable for disaster recovery.
2. Logical Backup:
o Copies of database structures and content (e.g., tables, schemas).
o Exported using tools like EXPORT or mysqldump.
5.4 Data Recovery
Recovery Concepts:
Recovery Techniques:
1. Roll Forward:
o Applies redo logs to update the database to a later point in time.
o Example: Recovering committed transactions.
2. Rollback:
o Reverts uncommitted transactions to maintain consistency.
o Example:
ROLLBACK;
Data Warehouse:
A centralized repository for storing large volumes of structured data from multiple
sources.
Supports analysis and decision-making.
Data Lakes:
Data Mining:
The process of extracting meaningful patterns and insights from large datasets.
Techniques include clustering, classification, and association.
Big Data:
Refers to extremely large datasets that cannot be processed using traditional methods.
Technologies: Hadoop, Spark.
MongoDB: