Database Management System (DBMS)
A Database Management System (DBMS) is software
designed to manage, organize, store, and retrieve data
efficiently in a structured format. It serves as an intermediary
between the user and the database, ensuring that data is
consistently maintained and accessible while abstracting the
complexities of data storage.
Purpose of a Database System
Data Storage and Retrieval: Provides a systematic and organized way to store data for
future retrieval.
Data Sharing: Facilitates sharing of data among multiple users and applications.
Data Integrity: Ensures accuracy and consistency of data through constraints and
validation mechanisms.
Data Security: Provides mechanisms to protect data from unauthorized access, misuse, and
corruption.
Data Redundancy Minimization: Reduces duplication by storing data in a single,
centralized location.
Concurrency Control: Manages simultaneous access by multiple users without conflicts.
Backup and Recovery: Ensures data is recoverable in case of failure or corruption.
Support for Queries: Enables users to retrieve data using query languages such as SQL.
Scalability: Supports large amounts of data and concurrent users efficiently.
Database Schema and Instances
1. Database Schema
A schema is the logical structure or blueprint of a database that defines its organization. It
describes how data is organized and what relationships exist between them.
Example:
A schema for an online shopping system might define tables like Customer, Order, and
Product, along with their attributes and relationships.
Types of Schemas:
Physical Schema: Describes the physical storage of data (e.g., files, indices).
Logical Schema: Defines the logical structure of the database (e.g., tables,
columns, relationships).
View Schema: Describes how users see the data through views.
Example:
Logical Schema: Customer(CustomerID, Name, Email, Phone)
View Schema: A subset of Customer data, like View_CustomerEmails.
2. Database Instance
An instance is the actual data stored in the database at a particular moment in time.
It is a snapshot of the database.
Difference Between Schema and Instance:
Schema is the design of the database, which remains static.
Instance is the content of the database, which changes over time.
Example:
Schema: The table Customer(CustomerID, Name, Email) .
Instance: Current rows in the table, e.g.:
CustomerID | Name | Email
-----------|--------------|-------------------
1 | John Doe |
[email protected]2 | Jane Smith |
[email protected] Views of Data
A view in DBMS represents an abstraction or simplified representation of data. It provides
different perspectives on the same database for different types of users or applications.
Levels of Data Abstraction
DBMS uses three levels of abstraction to separate how data is stored from how it is presented
to users:
Physical Level:
Describes how data is physically stored on disk (e.g., file structures, indices).
Low-level, detailed view meant for database administrators.
Logical Level:
Describes the structure of the database (e.g., tables, relationships).
Provides a logical view for developers and database designers.
View Level:
Provides an interface for end-users by showing only the relevant data.
Simplifies interaction with the database by hiding details.
Views of Data
Types of Database Languages
1. Data Definition Language (DDL)
2. Data Control Language (DCL)
3. Transaction Control Language (TCL)
4. Data Manipulation Language (DML)
Procedural Language (PL)
Structured Query Language (SQL)
1. Data Definition Language (DDL)
Purpose: Used to define the structure and schema of a database.
Key Operations:
CREATE: Creates database objects like tables, views, indexes, etc.
ALTER: Modifies the structure of existing database objects.
DROP: Deletes database objects permanently.
TRUNCATE: Removes the fractional part but retains its structure.
RENAME: Changes the name of a database object.
1. CREATE Command: To create new database objects like tables, views,
or indexes.
Example:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
OrderDate DATE,
CustomerID INT,
Amount DECIMAL(10, 2)
);
2. ALTER Command
Purpose: To modify the structure of an existing database object.
Examples:
Add a new column:
ALTER TABLE Orders ADD Status VARCHAR(20);
Adds a new column Status to the Orders table.
Modify a column:
ALTER TABLE Orders MODIFY Amount DECIMAL(12, 2);
Changes the Amount column to allow a larger precision.
.
2. ALTER Command
Purpose: To modify the structure of an existing database object.
Drop a column:
ALTER TABLE Orders DROP COLUMN Status;
Removes the Status column.
3. DROP Command
Purpose: To delete database objects permanently.
Examples:
Drop a table:
DROP TABLE Orders;
Deletes the Orders table permanently.
4. TRUNCATE Command
Purpose: To delete all records from a table but keep the table structure intact.
Example:
TRUNCATE TABLE Orders;
5. RENAME Command
Purpose: To change the name of a database object.
Examples:
Rename a table:
RENAME TABLE Orders TO SalesOrders;
Changes the name of the Orders table to SalesOrders.
Rename a column (specific to certain databases, e.g., MySQL):
ALTER TABLE SalesOrders RENAME COLUMN Amount TO TotalAmount;
2. Data Control Language (DCL)
Purpose: Used to control access to data and manage permissions.
Key Operations:
GRANT: This command is used to assign specific privileges to a user or role.
REVOKE: Removes previously granted privileges.
1. GRANT Command
Example 1: Granting All Privileges on a Database
GRANT ALL PRIVILEGES ON my_database.* TO 'username'@'localhost';
Example 2: Granting Specific Privileges on a Table Grant specific privileges (e.g.,
SELECT, INSERT, UPDATE) on the Orders table to a user:
GRANT SELECT, INSERT, UPDATE ON my_database.Orders TO
'username'@'localhost';
Example 3: Granting Privileges to All Tables in a Database
Grant SELECT and INSERT privileges to a user for all tables in a database:
GRANT SELECT, INSERT ON my_database.* TO 'username'@'localhost';
2. REVOKE Command
The REVOKE command is used to remove previously granted privileges from a user or role.
Example 1: Revoking Specific Privileges
Revoke SELECT and INSERT privileges from a user on the Orders table:
REVOKE SELECT, INSERT ON my_database.Orders FROM 'username'@'localhost';
Example 2: Revoking All Privileges
Revoke all privileges from a user on a specific database:
REVOKE ALL PRIVILEGES ON my_database.* FROM 'username'@'localhost';
Example 3: Revoking Administrative Privileges
Revoke the GRANT OPTION privilege from a user:
REVOKE GRANT OPTION ON my_database.* FROM 'admin_user'@'localhost';
Example 4: Revoking Privileges on All Databases
Revoke all privileges on all databases from a user:
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'username'@'localhost';
3. Transaction Control Language (TCL)
Purpose: Used to manage transactions in a database, ensuring data consistency and
integrity.
Key Operations:
COMMIT: Saves changes made in a transaction to the database.
ROLLBACK: Reverts changes made in a transaction.
SAVEPOINT: Creates a point within a transaction to which you can later roll back.
SET TRANSACTION: Sets properties for the transaction.
Examples:
Commit a transaction: COMMIT;
Rollback a transaction: ROLLBACK;
Savepoint usage: SAVEPOINT Save1;
4. Data Manipulation Language (DML)
Purpose: Used to perform operations on the data within the database.
Key Operations:
SELECT: Retrieves data from the database.
INSERT: Adds new records into a table.
UPDATE: Modifies existing records in a table.
DELETE: Removes records from a table.
Examples:
Insert a record:
INSERT INTO Orders (OrderID, OrderDate, CustomerID, Amount)VALUES (1, '2024-
01-01', 101, 250.50), (2, '2024-01-02', 102, 300.75), (3, '2024-01-03', 103, 150.00), (4,
'2024-01-04', 104, 500.25), (5, '2024-01-05', 105, 275.40);
4. Data Manipulation Language (DML)
SELECT: Retrieves data from the database.
Example 1: Retrieve All Records
SELECT * FROM Orders;
Example 2: Retrieve Specific Columns
SELECT OrderID, Amount FROM Orders;
Example 3: Apply a Condition
Retrieve orders with an amount greater than 300:
SELECT * FROM Orders WHERE Amount > 300;
Example 1: Update a Specific Record
Increase the Amount for the order with OrderID = 6:
UPDATE Orders SET Amount = 400.00 WHERE OrderID = 6;
Example 2: Update Multiple Records
Change the OrderDate for all orders with Amount > 300:
UPDATE Orders SET OrderDate = '2024-01-10' WHERE Amount > 300;
Query Language
Purpose: Primarily used to query and retrieve data from the database. The most common
query language is SQL (Structured Query Language).
Key Features:
Extracts specific data based on conditions.
Used extensively with DML commands like SELECT.
Example:
SELECT Name, Email FROM Customer WHERE CustomerID = 1;
Example 1: Delete a Specific Record
Remove the record with OrderID = 10:
DELETE FROM Orders WHERE OrderID = 10;
Example 2: Delete Records Based on a Condition
Remove all orders with Amount < 200:
DELETE FROM Orders WHERE Amount < 200;
Example 3: Delete All Records (Table Structure Remains)
DELETE FROM Orders;
Procedural Language (PL)
Purpose: Extends SQL capabilities by allowing procedural constructs like loops,
conditions, and exception handling.
Examples:
PL/SQL (Procedural Language for SQL - Oracle)
T-SQL (Transact-SQL - Microsoft SQL Server)
PL/SQL Example:
BEGIN
FOR rec IN (SELECT * FROM Customer) LOOP
DBMS_OUTPUT.PUT_LINE(rec.Name);
END LOOP;
END;
Database Language Purpose Key Commands
Define structure of database CREATE, ALTER, DROP,
DDL (Data Definition)
objects TRUNCATE, RENAME
Manage user permissions and
DCL (Data Control) GRANT, REVOKE
access
COMMIT, ROLLBACK,
TCL (Transaction
Manage database transactions SAVEPOINT, SET
Control)
TRANSACTION
SELECT, INSERT, UPDATE,
DML (Data Manipulation) Manipulate and manage data
DELETE
Retrieve and manipulate data
Query Language SELECT
using queries
Add procedural features like
Procedural Language PL/SQL, T-SQL
loops and conditions to SQL
Types of Database Architectures
Centralized Architecture:
All data resides in a single central location.
Users access the database via a network.
Advantages: Easy to manage and secure.
Disadvantages: Single point of failure, not scalable.
Distributed Architecture:
Data is spread across multiple locations (sites).
Can be homogeneous (same DBMS) or heterogeneous (different DBMS).
Advantages: Scalability, fault tolerance.
Disadvantages: Complexity, synchronization issues.
Types of Database Architectures
Client-Server Architecture:
Data resides on a server, and applications run on clients.
Components:
Client: User interface and query processing.
Server: Data storage, query execution, and transaction management.
Advantages: Better resource utilization.
Disadvantages: Network dependency.
Cloud-Based Architecture:
Data is hosted on cloud platforms (e.g., AWS, Azure).
Access via the internet.
Advantages: Cost-efficient, scalable.
Disadvantages: Dependency on third-party providers
DATABASE ARCHITECTURE
(CENTRALIZED/SHARED-MEMORY)
1. Query Processor
The query processor is responsible for interpreting user queries and executing them
efficiently. It includes:
Compiler and Linker: Converts application programs (written in programming
languages) into executable object code for interaction with the database.
DML Queries: Data Manipulation Language queries like SELECT, INSERT, UPDATE,
and DELETE are provided by users or applications to interact with data.
DDL Interpreter: Interprets Data Definition Language commands (e.g., CREATE
TABLE, ALTER TABLE) to define or modify the database schema.
DML Compiler and Organizer:
Converts high-level DML commands into low-level instructions (execution plans)
understandable by the database.
Ensures optimization of queries for efficient execution.
Query Evaluation Engine:
Executes the query execution plan generated by the DML compiler.
Interacts with the storage manager to retrieve or update data.
2. Storage Manager
The storage manager handles the storage and retrieval of data from the physical storage. It
ensures data integrity, security, and performance. Key components include:
Buffer Manager:
• Manages the data in memory buffers to minimize disk I/O.
• Keeps frequently accessed data in memory for faster access.
File Manager:
• Handles the physical storage of files on disk.
• Manages the allocation and deallocation of disk space.
Authorization and Integrity Manager:
Enforces security rules (e.g., access control) and data integrity constraints.
Transaction Manager:
• Ensures ACID (Atomicity, Consistency, Isolation, Durability) properties for all
database transactions.
• Manages concurrency control and rollback mechanisms.
3. Disk Storage
The disk storage is the physical layer where the data is stored persistently. Key
components include:
Data:
The actual data stored in tables and records.
Indices:
Data structures that improve the speed of data retrieval operations.
Data Dictionary:
Metadata repository that stores information about the database schema,
structure, and constraints.
Statistical Data:
Stores performance metrics and statistics for query optimization.
How It Works
User Queries:
Users or applications send DML queries or DDL commands to the DBMS.
Processing Queries:
The query processor interprets, optimizes, and evaluates queries.
Interaction with Storage Manager:
The query evaluation engine communicates with the storage manager to
retrieve or update data.
Accessing Disk Storage:
The storage manager retrieves data, metadata, or indices from disk storage
and provides it to the query processor.
Output Results:
The processed data is returned to the user or application.
CLIENT-SERVER ARCHITECTURE
(a) Two-Tier Architecture
Overview: The two-tier architecture consists of two layers:
Client Tier: The user interacts with an application (frontend) that connects
directly to the database.
Server Tier: The database system resides on the server, which manages and
stores the data.
Components:
User/Application:
The user interacts with the application through a user interface (e.g., GUI).
The application sends SQL queries to the database system.
Database System:
Processes queries, performs data operations, and sends the results back to
the client.
Characteristics:
The application runs directly on the client system.
The client and database server communicate over a network.
Advantages:
Simpler to implement.
Direct communication ensures faster query execution for small-scale
systems.
Disadvantages:
Poor scalability (all logic resides on the client, leading to performance
bottlenecks).
Limited separation of concerns (no middle layer for complex processing or
business logic).
(b) Three-Tier Architecture
Overview:,The three-tier architecture introduces an additional layer (Application Server)
between the client and the database server, creating three distinct tiers:
Client Tier: The user interacts with the application client (frontend).
Application Server Tier: Handles the business logic and processes requests.
Database System Tier: Manages and stores data.
Components:
User/Application Client:
• Provides the user interface.
• Sends requests to the application server.
Application Server:
• Processes the client requests.
• Contains the business logic (e.g., validating data, processing queries).
• Communicates with the database system for data retrieval and updates.
Database System:
Stores and manages data, returning results to the application server.
Characteristics:
Adds a middle layer for complex business logic.
Facilitates better separation of concerns and modularity.
Advantages:
Improved scalability (application logic is centralized on the application server).
Better security (direct database access is restricted).
Easier maintenance and updates to business logic without affecting clients.
Disadvantages:
More complex to implement.
Higher latency compared to two-tier due to additional communication between
layers.
Comparison
Aspect Two-Tier Architecture Three-Tier Architecture
Number of Layers 2 3
Directly dependent on Only interacts with the app
Client Dependency
DB server
Scalability Limited High
Harder to update
Maintenance Easier due to modularity
applications
Use Case Small-scale systems Large-scale, enterprise systems
DATABASE USERS AND ADMINISTRATORS
1. Naive Users
Description:
These are end-users who interact with the database through pre-defined application
interfaces.
They do not write queries directly but use the system for specific tasks (e.g., banking,
online shopping).
Examples:
Tellers in banks
Agents using reservation systems
Web users accessing online services
Tools Used:
Application interfaces (e.g., forms, GUIs)
2. Application Programmers
Description:
These users are responsible for developing application programs that interact with the
database.
They write code in programming languages and embed database queries (e.g., SQL)
within the applications.
Role:
Write programs that access the database.
Develop user-friendly interfaces for naive users.
Tools Used:
Application programs
Compiler and linker for generating executable code
3. Sophisticated Users
Description:
These users have in-depth knowledge of the database and use it to perform complex
analyses or queries.
They interact directly with the database using query languages like SQL.
Examples:
Data analysts
Scientists
Researchers
Tools Used:
Query tools (e.g., SQL editors, BI tools)
Direct interaction with the query processor (DML queries)
4. Database Administrators (DBAs)
Description: These are specialized users responsible for managing the overall functionality of
the database. Their responsibilities include schema definition, security enforcement, and
ensuring database performance.
Role:
Set up and maintain the database structure in routine.
Manage user access and permissions.
Perform backups and recovery.
Granting of authorization for data access
Ensuring that enough free disk space is available for normal operations, and upgrading
disk space as required
Monitoring jobs running on the database
Tools Used:
Administration tools (e.g., database management consoles)
Data Definition Language (DDL) interpreter for schema creation and updates.
Introduction to Relational Databases
A Relational Database is a type of database that stores and organizes data in a
structured format using tables. Each table, also called a relation, represents a
specific type of entity or object. Relational databases are the foundation of most
modern applications due to their reliability, scalability, and ability to handle large
volumes of data efficiently.
Key Concepts
1. Tables (Relations):
Data is stored in tables, which are made up of rows (records) and columns
(attributes).
Example
2. Rows (Records):
•Each row represents a single instance of the entity the table is modeling.
•Example: A row in the "Employee" table corresponds to one employee.
3. Columns (Attributes):
•Columns define the properties or characteristics of the data.
•Example: The "Name" column in the "Employee" table represents employee names.
4. Primary Key:
•A unique identifier for each row in a table.
•Example: EmployeeID in the "Employee" table.
5. Foreign Key:
•A column that links one table to another by referencing the primary key of another
table.
•Example: DepartmentID in an "Employee" table referencing the "Department"
table.
6. Relationships:
•Tables in a relational database are connected via relationships:
•One-to-One: Each row in Table A is linked to exactly one row in Table B.
•One-to-Many: A row in Table A can link to multiple rows in Table B.
•Many-to-Many: Rows in Table A can relate to multiple rows in Table B, and
vice versa (handled using a junction table).
7. Schema:
•The blueprint or structure of a database, including tables, columns, data types, and
relationships.
Features of Relational Databases
1.Structured Data:
•Data is stored in a highly organized and consistent manner.
2.SQL (Structured Query Language):
•A powerful language used to interact with and manage relational databases.
•Example Query: SELECT * FROM Employees WHERE Department = 'IT';
3.ACID Properties:
•Ensures data reliability and integrity:
•Atomicity: Transactions are all-or-nothing.
•Consistency: Database remains in a valid state before and after a transaction.
•Isolation: Concurrent transactions do not interfere with each other.
•Durability: Data remains persistent after a transaction is committed.
4.Data Integrity:
•Maintained through constraints like primary keys, foreign keys, and unique constraints.
5.Scalability:
•Suitable for handling large-scale data efficiently
Structure of Relational Databases
Relational databases are organized into a structured framework comprising tables, rows,
columns, and their associated relationships. This structure allows for efficient storage,
retrieval, and manipulation of data.
Core Components of Relational Database Structure
1. Tables (Relations):
•Definition: A table is the basic unit of storage in a relational database. It represents an entity
or object (e.g., employees, products, orders).
•Structure:
Rows (Records/Tuples): Represent individual instances of the entity.
Columns (Attributes): Define the properties or fields of the entity.
•Example Table: Employee
EmployeeID Name Department Salary
1 Alice HR 50000
2 Bob IT 60000
2. Schema:
•Definition: A schema is the blueprint or logical design of the database, describing how
tables are organized and the relationships between them.
•Key Elements:
Table names.
Column names and data types.
Relationships (e.g., primary and foreign keys).
Constraints (e.g., unique, not null, default values).
3. Primary Key:
•Definition: A column (or a set of columns) that uniquely identifies each row in a table.
•Purpose: Ensures data uniqueness and acts as a reference point for relationships.
•Example:
In the "Employee" table, EmployeeID is the primary key.
4. Foreign Key:
•Definition: A column in one table that refers to the primary key of another table.
•Purpose: Establishes relationships between tables.
•Example:
A DepartmentID column in the "Employee" table may refer to the primary key in the
"Department" table.
5. Relationships Between Tables:
•One-to-One (1:1):
Each row in Table A corresponds to exactly one row in Table B.
Example: A user and their unique profile.
•One-to-Many (1:N):
A single row in Table A corresponds to multiple rows in Table B.
Example: A department and its employees.
•Many-to-Many (M:N):
Rows in Table A relate to multiple rows in Table B and vice versa, typically
implemented with a junction table. Example: Students and courses.
6. Indexes:
•Definition: Data structures that improve the speed of data retrieval operations.
•Types:
Primary Index: Automatically created on the primary key.
Secondary Index: Created on non-primary key columns for faster access.
7. Constraints:
•Definition: Rules applied to data in a table to ensure data integrity.
•Types:
NOT NULL: Ensures a column cannot have a NULL value.
UNIQUE: Ensures all values in a column are distinct.
DEFAULT: Assigns a default value if no value is provided.
CHECK: Enforces a condition on column values.
FOREIGN KEY: Enforces referential integrity between tables.
8. Views:
•Definition: A virtual table based on a SELECT query.
•Purpose:
Simplify complex queries.
Provide a security layer by limiting access to specific columns or rows.
9. Stored Procedures and Functions:
•Stored Procedures: Predefined SQL scripts that perform specific tasks.
•Functions: Predefined computations that return a value.