DBMS File
DBMS File
Objective:
• To understand the concepts of Database Management Systems (DBMS), Relational
Database Management Systems (RDBMS), and Structured Query Language (SQL).
• To differentiate between DBMS and RDBMS.
• To learn the basic functionalities of SQL for database operations.
Theory:
A Database Management System (DBMS) is software that enables users to store, organize,
retrieve, and manage data efficiently. It acts as an interface between users and a database,
ensuring that data is stored securely, remains consistent, and is easily accessible.
In modern applications, DBMS is essential for handling large volumes of structured and
unstructured data in various domains like banking, healthcare, e-commerce, education,
and social media.
What is a Database?
A database is an organized collection of related data that can be easily accessed, managed,
and updated. Data in a database is usually stored in tables (rows and columns), files, or
objects, depending on the type of database used.
Example:
A university database may store information about students, courses, teachers, and grades
in different tables.
3
3. Data Integrity:
o Ensures data is accurate and consistent through rules and constraints.
4. Concurrency Control:
o Allows multiple users to access and modify data at the same time without
conflicts.
5. Backup and Recovery:
o Protects data by allowing recovery in case of system failure.
6. Query Processing:
o Enables efficient data retrieval using SQL (Structured Query Language).
Types of DBMS
DBMS is categorized into different types based on how data is structured and managed:
1. Hierarchical DBMS
• Organizes data in a tree-like structure (parent-child relationship).
• Each parent can have multiple children, but each child has only one parent.
• Example: IBM’s IMS (Information Management System)
Use Case: Used in banking and telecommunication systems where hierarchical
relationships exist.
2. Network DBMS
• Uses a graph-like structure where multiple relationships can exist between records.
• Unlike hierarchical DBMS, a child record can have multiple parent records.
• Example: Integrated Data Store (IDS)
Use Case: Used in manufacturing and transport systems.
Advantages of DBMS
Reduces Data Redundancy: Avoids duplicate data storage.
Ensures Data Security: Restricts unauthorized access to sensitive data.
Provides Data Consistency: Ensures uniform and accurate data across all users.
Supports Multi-User Access: Multiple users can work on the same database
4
simultaneously.
Enables Complex Queries: Users can retrieve data efficiently using SQL.
Backup and Recovery: Automatic data backup in case of system failure.
Disadvantages of DBMS
High Cost: Requires powerful hardware and software.
Complex Setup: Setting up and managing a DBMS requires expertise.
Performance Overhead: Involves additional processing, which can slow down
performance in large
systems.
Applications of DBMS
Banking Systems: Storing customer accounts, transactions, and loan records.
E-commerce: Managing product catalogs, user information, and orders.
Healthcare: Storing patient records, hospital management, and prescriptions.
Education: Handling student information, courses, and exam results.
Social Media: Managing user profiles, posts, and interactions.
Introduction to RDBMS (Relational Database Management System)
A Relational Database Management System (RDBMS) is a type of DBMS (Database
Management System) that organizes data into tables with rows and columns. It follows the
relational model introduced by E.F. Codd in 1970, where data is stored in relations (tables)
and can be retrieved using SQL (Structured Query Language).
RDBMS is widely used in applications where data consistency, security, and structured
relationships are important, such as banking, e-commerce, healthcare, and enterprise
software.
5
Student_ID Name Age Course
101 John 20 B.Tech
102 Alice 22 MBA
103 Bob 21 B.Sc
Here,
• "Student_ID" is the unique identifier (Primary Key).
• Each row represents a student.
• Columns store different attributes (Student ID, Name, Age, Course).
Advantages of RDBMS
Eliminates Redundancy – Uses normalization to prevent duplicate data.
Ensures Data Security – Allows role-based access control.
Supports Transactions – Guarantees safe and reliable operations.
Allows Multiple Users – Supports concurrency control.
Easy Data Retrieval – Uses SQL for fast and efficient querying.
Disadvantages of RDBMS
Complex Structure – Requires expert knowledge to design and manage databases.
Performance Overhead – Handling large amounts of data can slow down operations.
Costly Implementation – Requires powerful servers and maintenance.
6
RDBMS Developer Usage
Microsoft SQL
Microsoft Business and corporate applications
Server
Mobile apps, lightweight
SQLite SQLite.org
applications
Normalization in RDBMS
Normalization is the process of reducing data redundancy and improving efficiency. It
divides large tables into smaller related tables.
Normalization Forms
1. 1NF (First Normal Form) – Ensures each column has atomic (indivisible) values.
2. 2NF (Second Normal Form) – Removes partial dependencies.
7
3. 3NF (Third Normal Form) – Eliminates transitive dependencies.
Applications of RDBMS
Banking Systems – Stores customer transactions, accounts, and loans.
E-commerce – Manages user profiles, products, and orders.
Healthcare – Stores patient records, doctor schedules, and prescriptions.
Education – Handles student data, courses, and grades.
Enterprise Software – Manages HR, sales, and inventory data.
Introduction to SQL (Structured Query Language)
SQL (Structured Query Language) is the standard programming language used to interact
with Relational Database Management Systems (RDBMS). It allows users to store,
retrieve, manipulate, and manage data in a structured and efficient manner.
SQL is widely used in applications such as banking, e-commerce, healthcare, education,
and enterprise systems to handle large amounts of data.
8
1. DDL (Data Definition Language) – Managing Database Structure
DDL commands are used to define and modify database schema (tables, indexes,
constraints, etc.).
CREATE TABLE – Creates a new table.
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Course VARCHAR(30)
);
ALTER TABLE – Modifies an existing table (adding/removing columns).
ALTER TABLE Students ADD Email VARCHAR(100);
DROP TABLE – Deletes a table permanently.
DROP TABLE Students;
TRUNCATE TABLE – Removes all records from a table but keeps the structure.
TRUNCATE TABLE Students;
9
FROM Students
INNER JOIN Courses ON Students.Course = Courses.Course_ID;
10
Join
Description Example
Type
SELECT * FROM Students INNER JOIN
INNER Returns matching records from
Courses ON Students.Course_ID =
JOIN both tables
Courses.Course_ID;
Returns all records from the left SELECT * FROM Students LEFT JOIN
LEFT
table, with matching records from Courses ON Students.Course_ID =
JOIN
the right Courses.Course_ID;
Returns all records from the right SELECT * FROM Students RIGHT JOIN
RIGHT
table, with matching records from Courses ON Students.Course_ID =
JOIN
the left Courses.Course_ID;
Returns all records from both SELECT * FROM Students FULL JOIN
FULL
tables, with NULL for unmatched Courses ON Students.Course_ID =
JOIN
rows Courses.Course_ID;
11
Experiment-2
Title : Experiments based on DDL commands – CREATE, ALTER, DROP and TRUNCATE.
Objective:
• To understand and perform DDL (Data Definition Language) commands in
SQL.
• To learn how to create, modify, delete, and manage database structures
using CREATE, ALTER, DROP, and TRUNCATE commands.
• To analyze the impact of these commands on database schemas and data
storage.
• To understand the differences between DROP and TRUNCATE in terms of
data removal and table structure persistence.
Theory:
What is DDL (Data Definition Language)?
DDL (Data Definition Language) is a category of SQL commands used to define, modify,
and delete database structures such as tables, schemas, indexes, and constraints. DDL
commands do not modify actual data inside tables; instead, they affect the structure of the
database.
List of DDL Commands
Command Description
CREATE Creates new database objects (tables, views, indexes, etc.).
Modifies existing database objects (adding/removing columns, constraints,
ALTER
etc.).
Deletes an existing database object (removes table, view, or index
DROP
permanently).
TRUNCATE Deletes all records from a table but retains its structure.
COMMENT Adds comments to database objects for documentation.
RENAME Renames database objects (table, column, etc.).
12
Student_ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT CHECK (Age >= 18),
Email VARCHAR(100) UNIQUE,
Course VARCHAR(50) DEFAULT 'Undeclared'
);
1.3 Creating an Index (for Faster Searching)
CREATE INDEX idx_student_name ON Students(Name);
This command creates an index on the Name column for faster searching.
13
4. TRUNCATE Command – Deleting All Records from a Table
The TRUNCATE command removes all rows from a table but keeps the table structure
TRUNCATE TABLE Students;
This deletes all student records but keeps the table for future use.
Difference Between DELETE and TRUNCATE
Feature DELETE TRUNCATE
Removes Data? Yes, based on condition Yes, all records
Keeps Table
Yes Yes
Structure?
Can Be Rolled Yes (if inside a
No (cannot be undone)
Back? transaction)
Faster No (slow for large Yes (fast because it does not log
Performance? tables) individual row deletions)
14
Lab Assignment 1
Q.1 Create a table student which contain attributes like roll no.,dob,gender,class,college,city
and marks.
15
Q.5 Display rollno and class of Noida students
Q.10 Delete the records of students where marks is less than 80.
16
EXPERIMENT 3
Title: Apply the integrity constraints like Primary Key, Foreign key, Check, NOT NULL,
etc. to the tables.
Objective:
• To understand and implement integrity constraints in SQL to ensure data accuracy
and consistency.
• To apply Primary Key constraint to uniquely identify records in a table.
• To enforce Foreign Key constraints for maintaining referential integrity between
tables.
• To use NOT NULL constraint to prevent null values in essential fields.
• To apply the CHECK constraint to enforce specific conditions on column values.
• To implement the DEFAULT constraint to assign a default value when no value is
provided.
• To gain practical experience in designing robust and reliable relational databases.
Theory:
Integrity constraints are rules enforced on a database to maintain accuracy, consistency, and
reliability of data. These constraints ensure that only valid data is stored in the database by
defining specific conditions that must be met by the data. The most commonly used integrity
constraints in SQL are:
17
dept_name VARCHAR(50),
emp_id INT,
FOREIGN KEY (emp_id) REFERENCES Employee(emp_id)
);
4. CHECK Constraint
• The CHECK constraint ensures that column values meet a specified condition.
• It is used to enforce domain integrity (e.g., age should be above 18).
Example:
ALTER TABLE Student
ADD CONSTRAINT chk_age CHECK (age >= 18);
5. DEFAULT Constraint
• The DEFAULT constraint assigns a default value to a column when no value is
provided during insertion.
Example:
ALTER TABLE Student
ADD COLUMN city VARCHAR(50) DEFAULT 'Delhi';
18
Lab assignment 2
Q1:Create table emp having primary key as, emp(emp_id primary
key,emp_name,emp_salary)
Q2: Create table dept with foreign key (dept_id primary key,dept_name,emp_id foreign
key(emp_id) references emp(emp_id))
Q.3 Create table emp having one unique column as(emp_id unique,emp_name)
Q5: Apply check constraint in student table Q6: Apply default constraint in student table
19
EXPERIMENT 4
Title: Experiments based on basic DML commands – SELECT, INSERT, UPDATE and
DELETE
Objective:
To understand and perform basic Data Manipulation Language (DML) operations, including
SELECT, INSERT, UPDATE, and DELETE commands in SQL. These commands help in
retrieving, modifying, and managing data within a database table.
Theory:
Data Manipulation Language (DML) is a subset of SQL used for managing data within
database tables. The key DML commands include:
1. SELECT – Retrieves data from one or more tables.
SELECT * FROM employees;
SELECT name, age FROM students WHERE age > 18;
2. INSERT – Adds new records into a table.
INSERT INTO employees (id, name, salary) VALUES (1, 'John Doe', 50000);
3. UPDATE – Modifies existing records in a table.
UPDATE employees SET salary = 60000 WHERE id = 1;
4. DELETE – Removes records from a table.
DELETE FROM employees WHERE id = 1;
These operations are essential for maintaining and manipulating the data stored in relational
databases efficiently.
20
Lab Assignment 3
Q.1 Create a table employee(emp no., emp name, job, hire_date, salary, commission,
department no., age ).
21
Q.6 Display max, min, average salary of department no.=30
Q.8 What is the difference between maximum and minimum salary of employee in
organization ?
22
EXPERIMENT 5
Title: Write the queries for implementing Built-in functions, GROUP BY, HAVING and
ORDER BY.
Objective:
To learn and implement SQL queries using Built-in functions, GROUP BY, HAVING, and
ORDER BY to perform data aggregation, filtering, and sorting operations in a relational
database.
Theory:
SQL provides various functionalities to manipulate and analyze data efficiently. Some of the
key concepts include:
1. Built-in Functions:
SQL provides several built-in functions that help in performing operations on data:
1. Aggregate Functions: SUM(), AVG(), MAX(), MIN(), COUNT()
2. String Functions: UPPER(), LOWER(), LENGTH(), CONCAT()
3. Date Functions: NOW(), CURDATE(), YEAR(), MONTH()
SELECT MAX(salary) FROM employee;
SELECT UPPER(emp_name) FROM employee;
2. GROUP BY Clause:
The GROUP BY clause is used to group rows that have the same values in specified
columns and perform aggregate functions on them.
SELECT department_no, AVG(salary)
FROM employee
GROUP BY department_no;
3. HAVING Clause:
The HAVING clause is used to filter groups after aggregation. It works with GROUP
BY, unlike WHERE, which filters individual rows.
SELECT department_no, COUNT(*)
FROM employee
GROUP BY department_no
HAVING COUNT(*) > 5;
4. ORDER BY Clause:
The ORDER BY clause is used to sort the result set in ascending (ASC) or
descending (DESC) order.
SELECT emp_name, salary
FROM employee
ORDER BY salary DESC;
23
Lab Assignment 4
Question 1: Create a table with the following structure:
SaleID (Product , Category , Amount , SaleDate)
(1 , Laptop , Electronics , 50000 , 2024-01-15)
(2 , TV , Electronics , 40000 , 2024-01-18)
(3 , Shirt , Clothing , 2000 , 2024-01-20)
(4 , Laptop , Electronics , 55000 , 2024-02-05)
(5 , Shirt , Clothing , 2500 , 2024-02-10 )
Question 2: Find products that have been sold more than once.
Question 3: Find the average sale amount per category, but only for categories with
more than one sale.
24