DataBase Systems
DataBase Systems
1. Introduction to Databases
Definition:
A database is a structured collection of data stored electronically, allowing
efficient retrieval, insertion, updating, and management.
Explanation:
Databases store information systematically, making it easy to access, update, and
manage large amounts of data.
Uses:
Websites (e.g., storing user profiles, orders)
Banking (e.g., storing transactions)
Hospitals (e.g., patient records)
Example:
A university database stores student names, roll numbers, and marks.
2. Database Management System (DBMS)
Definition:
A DBMS (Database Management System) is software that allows users to create,
manage, and interact with databases.
Explanation:
A DBMS provides an interface for users to interact with databases, ensuring
security, consistency, and easy data retrieval.
Uses:
Stores and manages large datasets
Provides multi-user access
Ensures data security
Example:
Popular DBMS software:
MySQL
PostgreSQL
Microsoft SQL Server
3. Types of Databases
Definition & Explanation:
Different types of databases are designed for specific needs.
Explanation:
SQL allows users to perform CRUD operations:
Create (INSERT)
Read (SELECT)
Update (UPDATE)
Delete (DELETE)
Uses:
Used in backend development
Helps retrieve and modify data
Example:
sql
Copy
Edit
SELECT * FROM Students WHERE age > 18;
(Selects all students older than 18.)
5. Normalization
Definition:
Normalization is a process of organizing a database to reduce redundancy and
improve integrity.
Explanation:
It divides a large table into smaller ones and establishes relationships between
them.
Forms of Normalization:
1NF (Eliminates duplicate columns)
2NF (Eliminates partial dependencies)
3NF (Removes transitive dependencies)
Uses:
Prevents data duplication
Saves storage space
Example:
Before normalization:
Student_ID Name
1 Ali
2 Sara
Courses Table
Course_ID Course
101 Math
102 Science
6. ACID Properties
Definition:
ACID properties ensure database transactions are reliable.
Explanation:
Atomicity – All or nothing execution of a transaction.
Consistency – Ensures valid state before and after a transaction.
Isolation – Transactions don't interfere with each other.
Durability – Changes remain even after a system crash.
Uses:
Ensures data integrity in banking and e-commerce.
Example:
A banking transaction transferring money from one account to another must be atomic
—either fully complete or not executed at all.
7. Indexing
Definition:
Indexing is a technique that improves the speed of data retrieval.
Explanation:
An index creates a quick lookup mechanism for finding records faster.
Uses:
Used in search engines
Improves database performance
Example:
An index on the "name" column in a Students table allows fast searches.
sql
Copy
Edit
CREATE INDEX idx_name ON Students(name);
8. Transactions
Definition:
A transaction is a sequence of operations that is treated as a single unit.
Explanation:
A transaction ensures that multiple operations are completed together.
Uses:
Used in online payments, shopping carts.
Example:
If a user buys an item and the payment is deducted but the order is not placed, a
rollback should occur.
sql
Copy
Edit
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 100 WHERE user_id = 1;
UPDATE Orders SET status = 'Placed' WHERE order_id = 101;
COMMIT;
9. Triggers
Definition:
Triggers are special SQL procedures that execute automatically when certain
conditions are met.
Explanation:
Triggers help automate database tasks like logging changes.
Uses:
Used in auditing and security.
Example:
Automatically record when a new user is added.
sql
Copy
Edit
CREATE TRIGGER after_insert_user
AFTER INSERT ON Users
FOR EACH ROW
INSERT INTO AuditLog(user_id, action) VALUES (NEW.id, 'User Added');
10. Stored Procedures
Definition:
Stored procedures are precompiled SQL scripts stored in the database.
Explanation:
They improve security and efficiency by reducing repetitive query execution.
Uses:
Used in enterprise applications.
Example:
sql
Copy
Edit
CREATE PROCEDURE GetStudents()
AS
SELECT * FROM Students;
11. NoSQL Databases
Definition:
NoSQL databases store data in a non-relational format.
Explanation:
Unlike SQL databases, NoSQL databases store data in flexible formats like JSON,
key-value pairs, or graphs.
Uses:
Used in social media, real-time analytics.
Example:
MongoDB stores data in JSON format:
json
Copy
Edit
{ "name": "Ali", "age": 22, "city": "Lahore" }
12. Data Warehousing
Definition:
A data warehouse is a system used for reporting and data analysis.
Explanation:
It collects and stores large datasets for business intelligence.
Uses:
Used in business analytics, reporting.
Example:
Amazon stores customer purchase history in a data warehouse for recommendations.
Explanation:
It helps businesses find trends using algorithms.
Uses:
Used in fraud detection, recommendations.
Example:
Netflix suggests movies based on user preferences.