DBMS Report
DBMS Report
LAB REPORT
CSE312: Database Management System Lab
Submitted To
Ms. Tamanna Sultana
Lecturer
Department of CSE, Daffodil International University
Submitted By
Student ID: 222-15-6188
Section: 62_A1
Student Name: Maria Tasnim
2
Lab 1,2 Lab Setup and DDL (Create, Alter, Drop, Truncate) CO1
Lab 3,4,5 DML (Select, Insert, Update, Delete Operation) and CO2
Keys
Lab 10 DCL (Grant, Revoke) and TCL (Commit, Savepoint and CO2
Rollback)
LAB REPORT
01
Topic: DDL & DML
CO Mapping: CO1, CO2
Experiment Details:
Objective:
The tasks in this experiment are designed to demonstrate various SQL operations, such as CRUD
operations (Create, Read, Update, Delete), Alter operations, and Basic SQL Queries to manage
and retrieve data effectively.
Database Schema:
The following tables will be created:
1. Students Table:
○ student_id (INT, Primary Key, Auto Increment): Unique identifier for each student.
○ first_name (VARCHAR(50)): First name of the student.
○ last_name (VARCHAR(50)): Last name of the student.
○ email (VARCHAR(100), Unique): Email address of the student.
○ date_of_birth (DATE): Date of birth of the student.
2. Courses Table:
○ course_id (INT, Primary Key, Auto Increment): Unique identifier for each course.
○ course_name (VARCHAR(100), Unique): Name of the course.
○ course_description (TEXT): Description of the course.
3. Enrollments Table:
○ enrollment_id (INT, Primary Key, Auto Increment): Unique identifier for each
enrollment record.
○ student_id (INT, Foreign Key referencing Students.student_id): References the
student_id from the Students table.
○ course_id (INT, Foreign Key referencing Courses.course_id): References the
course_id from the Courses table.
○ enrollment_date (DATE): The date when the student enrolled in the course.
1. Create Operations:
○ Insert 5 records in the Students table with appropriate values for each field.
○ Insert 4 records in the Courses table with relevant course details.
2. Read Operations:
○ Retrieve the list of all students, displaying all student details.
5
○ Retrieve the names of students who have enrolled in a specific course (e.g.,
"Mathematics").
3. Update Operation:
○ Update the email address of a student based on their student_id.
4. Delete Operation:
○ Delete a student from the Students table who has not enrolled in any course.
1. Add a new column phone_number (VARCHAR(15)) to the Students table. This will allow
the system to store students' contact numbers.
2. Change the datatype of the course_description column in the Courses table from TEXT
to VARCHAR(255) to accommodate course descriptions with a smaller and more defined
length.
a student_id that does not exist in the Students table will result in a constraint
violation.
3. Retrieve all enrollments with student names and course names:
○ Query to retrieve a list of all enrollments, including the student names and
corresponding course names.
Step-by-Step Procedure:
Database Schema
Obtained Output:
7
Course Table:
YES
Enrollment Table:
Obtained Output:
YES
Courses Table:
02. Read:
o Retrieve the list of all students.
SELECT * FROM Student;
Obtained Output:
YES
);
Obtained Output:
Desired
Output?
YES
UPDATE Student
SET email = 'spamemail@example.com'
WHERE student_id = 3;
Obtained Output:
Desired
Output?
YES
04. Delete: Delete a student who has not enrolled in any course from the Student table.
Obtained Output:
Desired
Output
?
YES
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
11
SELECT *
FROM Student
WHERE date_of_birth > '2000-01-01';
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
Obtained Output:
12
Desired
Output?
YES
04. Retrieve the names of students enrolled in both "Mathematics" and "Science" courses
using a set operation:
Obtained Output:
Desired
Output
?
YES
05. Rename student_id as ID and first_name as Name when retrieving student data:
Obtained Output:
13
Desired
Output
?
YES
06. For each course, display the student names enrolled in that course:
Obtained Output:
Desired
Output
?
YES
02. Demonstrate how you would enforce a foreign key constraint when inserting data
into the Enrollments table:
To enforce a foreign key constraint when inserting data into the Enrollments table, you can
use the following SQL command:
SELECT c.course_name, s.first_name, INSERT INTO Enrollments
(enrollment_id, student_id, course_id)
VALUES (1, 123, 456);
14
Before executing this command, the database checks that student_id 123 exists in the
Student table. If it does not, the insert operation will fail, maintaining the integrity of the
relationship.
03. Retrieve all enrollments along with student names and course names:
We can retrieve all enrollments along with student names and course names using the
following SQL query:
Desired
Output
?
YES
Observation:
In this lab, we successfully designed and managed a relational database for a Student Management
System. The CRUD operations—Create, Read, Update, and Delete—enabled smooth data
manipulation, allowing us to easily insert, retrieve, modify, and remove records. We demonstrated
the adaptability of the schema by adding a phone_number column and modifying column types to
meet evolving requirements. SQL queries were utilized to gain valuable insights from the data,
such as filtering students based on birthdate, calculating averages, and performing complex joints
to retrieve related information. The use of Foreign Keys ensured data integrity by establishing
referential relationships between tables, while constraints helped maintain valid data entry.
Overall, this exercise emphasized the importance of structured database design and the efficient
use of queries for effective database management.
15
LAB REPORT
02
Topic: Advanced SQL Techniques for Efficient Data Retrieval, Aggregation,
and Conditional Manipulation.
CO Mapping: CO2
Experiment Details:
Exploring Nested Queries and the LIMIT Keyword in SQL for Conditional Data Extraction and
Ranking
Schema
Faculty (Id, Name, Designation, Salary)
Student_b_i(Id, Name, Email, Phone)
Course_b_i(CID, CName, Credit)
Employee (ID, Name, Age, Salary)
Enrollment (SID, CID)
Takes (FID, CID)
1. Find out the Faculty who gets the second highest salary.
2. Find faculty/faculty members who take DBMS courses.
3. Find out those students who attend course/s by MMI.
4. Find out the teacher’s basic info on whom Rahim attends class/es.
Step-by-Step Procedure:
Database Schema
● Find out the Faculty who gets the second highest salary.
Obtained Output:
Desired
Output?
YES
SELECT Name
FROM Faculty
WHERE ID IN (
SELECT FID
FROM Takes
JOIN Course_b_i ON Takes.CID = Course_b_i.CID
19
Obtained Output:
Desired
Output?
YES
SELECT S.Name
FROM Faculty F
JOIN Takes T ON F.ID = T.FID
JOIN Course_b_i C ON T.CID = C.CID
JOIN Enrollment E ON C.CID = E.CID
JOIN Student_b_i S ON E.SID = S.Id
WHERE F.Name = 'MMI';
Obtained Output:
Desired
Output?
YES
● Find out the teacher’s basic info whom Rahim attends class/es.
Obtained Output:
Desired
Output?
YES
Observation:
In this experiment, we executed various SQL queries on a university database to retrieve specific
data. The tasks included identifying the faculty member with the second-highest salary using a
subquery, locating faculty members teaching the "DBMS" course through a join between the
Faculty, Takes, and Course_b_i tables, and fetching students who are enrolled in courses taught
by "MMI". Additionally, we retrieved information about faculty members teaching courses
attended by the student "Rahim". These queries demonstrated the effective use of subqueries, joins,
and filtering across interconnected tables to efficiently extract and manipulate data within a
relational database.
21
Experiment No: 02 Experiment Name: Distinct, Group By, Having, And, Or, not.
Experiment Details:
To examine the application of nested queries and the LIMIT keyword in SQL for retrieving targeted data
based on specific conditions and for ranking purposes.
Schema
Faculty (Id, Name, Designation, Salary)
Student_b_i(Id, Name, Email, Phone)
Course_b_i(CID, CName, Credit)
Employee (ID, Name, Age, Salary)
Enrollment (SID, CID)
Takes (FID, CID)
Tasks to Perform
1. Find out the average salary of Senior Lecturers.
2. Show the details of the highest paid Senior Lecturer.
3. Find out the courses taught by the teacher who gets a salary ranging from 500,000 to
1,000,000.
4. Find out the faculty information having salary 500,000 and designation "SL".
5. Find out those students who took courses DBMS or CN.
6. Find out the students who did not enroll in any course having 3 credits.
Step-by-Step Procedure:
Database Schema
(1, 2),
(2, 3),
(3, 1);
Obtained Output:
Desired
Output?
YES
SELECT *
FROM Faculty
WHERE Designation = 'Senior Lecturer'
ORDER BY Salary DESC
LIMIT 1;
Obtained Output:
Desired
Output?
YES
3. Find out the courses taught by the teacher who gets a salary ranging from 500,000 to
1,000,000.
24
SELECT c.CName
FROM Takes t
JOIN Faculty f ON t.FID = f.Id
JOIN Course_b_i c ON t.CID = c.CID
WHERE f.Salary BETWEEN 500000 AND 1000000;
Obtained Output:
Desired
Output?
YES
4. Find out the faculty information having salary 500,000 and designation "SL".
SELECT *
FROM Faculty
WHERE Salary = 500000 AND Designation = 'Senior Lecturer';
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
6. Find out the students who did not enroll in any course having 3 credits.
SELECT s.Name
FROM Student_b_i s
WHERE NOT EXISTS (
SELECT 1
FROM Enrollment e
JOIN Course_b_i c ON e.CID = c.CID
WHERE e.SID = s.Id AND c.Credit = 3
);
Desired
Output?
YES
Observation:
In this experiment, we employed a range of SQL queries to accomplish tasks such as calculating
average salaries, filtering faculty by specific salary ranges, and retrieving data from multiple tables
using joins. These queries made use of subqueries, aggregation functions, and various filtering
techniques to extract detailed insights. Additionally, the tasks involved joining related tables like
Faculty, Student_b_i, Course_b_i, and Enrollment to retrieve specific records, demonstrating how
relational databases allow for complex data retrieval based on certain conditions. This exercise
reinforced the importance of mastering SQL for efficiently managing and querying data within
relational databases.
26
LAB REPORT
03
Topic: SQL Join Operation
CO Mapping: CO2
Experiment No: 03 Experiment Name: Exploring SQL Joins for Effective Student
and Course Data Retrieval
Experiment Details:
To investigate and apply various SQL join techniques (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
JOIN) for retrieving and managing data related to student enrollments in CSE courses, with an emphasis
on the relationships between students and courses.
Schema:
• Students (Student_ID, Name, Age, Class)
• Courses (Course_ID, Course_Name, Credits)
• Enrollments (Enrollment_ID, Student_ID, Course_ID, ENrollment_Date)
Step-by-Step Procedure:
Database Schema
1. Student_ID, Name, Course_Name, and Enrollment_Date for all students who are enrolled in
any course. (INNER JOIN)
Obtained Output:
Desired
Output?
YES
2. Student_ID, Name, Course_Name, and Enrollment_Date for all students, including those who
are not enrolled in any course. (LEFT JOIN)
Obtained Output:
Desired
Output?
YES
FROM Courses c
RIGHT JOIN Enrollments e ON c.Course_ID = e.Course_ID
RIGHT JOIN Students s ON e.Student_ID = s.Student_ID;
Obtained Output:
Desired
Output?
YES
4. Student_ID, Name, Course_Name, and Enrollment_Date for all students and courses, including
unmatched entries from both sides.
UNION
Obtained Output:
Desired
Output?
YES
31
Observation:
In this experiment, we examined various SQL join techniques (INNER JOIN, LEFT JOIN, RIGHT
JOIN, and FULL JOIN) to manage and retrieve student-course enrollment information. The
INNER JOIN allowed us to fetch data only for students who were enrolled in courses, while the
LEFT JOIN ensured that all students were included, regardless of their enrollment status. The
RIGHT JOIN highlighted all courses, even those without any enrolled students, and the FULL
JOIN merged both students and courses, presenting all records, including those without matches
from either side. These join operations are crucial for effectively working with related data, each
serving different purposes depending on the need to include or exclude unmatched entries.
32
LAB REPORT
04
Topic: COMMIT/ROLLBACK in Database Transactions
CO Mapping: CO2
Experiment Details:
The goal of this experiment is to investigate and apply the COMMIT and ROLLBACK operations
within SQL transactions.
Step-by-Step Procedure:
Database Schema
Obtained Output:
Desired
Output?
YES
3. Begin a transaction:
START TRANSACTION;
35
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
Obtained Output:
Desired
Output?
YES
ROLLBACK TO Save1;
37
Obtained Output:
Desired
Output?
YES
Observation:
In this experiment, we examined the application of transactions to maintain data integrity within
the database. By utilizing the COMMIT command, we effectively saved the changes made during
a transaction, while the ROLLBACK command allowed us to reverse any undesired modifications.
Additionally, the SAVEPOINT feature provided a way to set intermediate checkpoints within a
transaction, enabling partial rollbacks if necessary. These techniques demonstrate how transactions
can provide both flexibility and reliability in managing database operations.
38
LAB REPORT
05
Topic: Trigger, View, Procedure
CO Mapping: CO3
Experiment Details:
This experiment demonstrates the use of stored procedures, triggers, and views in SQL for
effective data management. It covers the creation of a stored procedure to update employee
salaries, a trigger to automatically log salary modifications into an audit table, and a view to present
employee and department details. These tasks showcase how SQL can be leveraged for
streamlined data handling, automated tracking, and enhanced reporting.
Schema
Employees (Emp_ID, Name, Department_ID, Salary)
Departments (Dept_ID, Dept_Name, Location)
Salary_Audit (Audit_ID, Emp_ID, Old_Salary, New_Salary)
Tasks to Perform
Task 1: Create and run a stored procedure to do the following:
1. Accept an employee ID (Emp_ID) as an IN parameter.
2. Get the employee's name as an OUT parameter and return it.
3. Accept a percentage as an INOUT parameter, then increase the employee's salary by that
percentage and return the updated salary.
Task 2: Create a trigger that does the following:
1. When an update to the Salary column in the Employees table occurs, automatically log the
change to a new table called Salary_Audit.
2. The Salary_Audit table should include: [Audit_ID (primary key), Emp_ID, Old_Salary,
New_Salary].
Task 3: Design a view that shows the following employee data:
Emp_ID, Name, Department, Salary, and Dept_Location.
Step-by-Step Procedure:
Database Schema
Task 1:
1. Accept an employee ID (Emp_ID) as an IN parameter.
2. Get the employee's name as an OUT parameter and return it.
3. Accept a percentage as an INOUT parameter, then increase the employee's salary by that
percentage and return the updated salary.
DELIMITER //
DELIMITER;
41
Obtained Output:
Desired
Output?
YES
Task 2:
1. When an update to the Salary column in the Employees table occurs, automatically log the
change to a new table called Salary_Audit.
2. The Salary_Audit table should include: [Audit_ID (primary key), Emp_ID, Old_Salary,
New_Salary].
DELIMITER //
DELIMITER;
Obtained Output:
Desired
Output?
42
YES
Task 3:
1. Design a view that shows the following employee data:
Obtained Output:
Desired
Output?
YES
Observation:
In this experiment, we focused on the implementation of stored procedures, triggers, and views to
enhance data management and integrity. We created a stored procedure that facilitates employee
salary updates and returns the updated salary. Additionally, a trigger was set up to automatically
capture any salary changes and log them in an audit table, ensuring transparency and
accountability. We also developed a view to consolidate employee and department information,
streamlining data access. This experiment underscored the value of stored procedures for handling
dynamic data operations, triggers for maintaining data integrity and audit trails, and views for
efficient data retrieval and presentation.
43
01 CO1,CO2 Yes These lab activities cover CO1 and CO2 by creating databases
with interconnected tables using primary and foreign keys, and
performing CRUD operations. The tasks involve using SQL
commands like CREATE, ALTER, DROP, INSERT, and
SELECT, while optimizing query performance in MySQL.
02 CO2 Yes These lab activities focus on CO2 by exploring advanced SQL
queries, including nested queries, LIMIT, and ranking
techniques, with a focus on query optimization and execution
time analysis in MySQL. They enhance skills in writing
efficient, complex queries for accurate data retrieval.
03 CO2 Yes These lab activities achieve CO2 by applying various SQL
joins (INNER, LEFT, RIGHT, FULL) to retrieve data from
related tables, emphasizing table relationships and efficient
data retrieval strategies in relational databases.
05 CO2,CO3 Yes These lab activities cover CO2 and CO3 by using triggers,
stored procedures, and views to address database challenges.
Triggers ensure data integrity, stored procedures handle
complex operations, and views simplify data access and
improve security. These activities enhance skills in
maintaining consistency, security, and performance in
databases.
44
01 EP1: Depth of Yes CO1, The labs require both K2, K3, K4 Lab Report
Knowledge CO2 basic and advanced SQL 01
Required knowledge, including (Page: 3-14)
database design,
optimization, indexing,
and query execution.
02 EP2: Range of Yes CO2 Optimizing queries while K3, K4, K6 Lab Report
Conflicting ensuring data integrity 02
Requirements across tables, with a (Page: 15-25)
focus on execution time
and efficient data
management.