DBMS Project
DBMS Project
ER diagram :
DATA BASE DESIGN FOR STUDENT MANAGEMENT
SYSTEM:-
For a Student Management System in a Database Management System
(DBMS), we need several tables to manage students, courses, enrollments,
instructors, and possibly grades and departments. Here’s a basic structure
for the necessary tables:
1. Students Table.
2. Courses Table.
3. Enrollments Table.
1.CREATING TABLES FOR STUDENT MANAGEMENT
SYSTEM:-
CREATE DATABASE StudentManagementSystem;
USE StudentManagementSystem;
//STUDENT TABLE
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
Gender VARCHAR(10),
AdmissionDate DATE
);
//COURSES TABLE
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
Credits INT
);
//ENROLLMENTS TABLE
CREATE TABLE Enrollments (
EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE,
Grade CHAR(1),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
2.INSERTING VALUES IN TABLE:-
//Inserting values into Students table
INSERT INTO Students (StudentID, FirstName, LastName, BirthDate,
Gender, AdmissionDate) VALUES
(1, 'Tejaswini', 'Beri', '2000-01-15', 'Female', '2018-09-01'),
(2, 'Suresh', 'Azmeera', '1999-05-20', 'Male', '2017-09-01'),
(3, 'Eswar', 'Bandaru', '2001-03-25', 'Male', '2019-09-01'),
(4, 'Ankkosh', 'GiriGosai', '2000-07-30', 'Male', '2018-09-01'),
(5, 'snehitha', 'Bandaru', '1998-12-12', 'Female', '2016-09-01'),
(6, 'preethi', 'satya', '2002-02-14', 'Female', '2020-09-01'),
(7, 'sam', 'tata', '2001-06-18', 'Female', '2019-09-01'),
(8, 'Olivia', 'Taylor', '1999-09-10', 'Female', '2017-09-01'),
(9, 'Alexander', 'Anderson', '2000-11-22', 'Male', '2018-09-01'),
(10, 'Sophia', 'Thomas', '2002-05-05', 'Female', '2020-09-01');
// Inserting values into Courses table
INSERT INTO Courses (CourseID, CourseName, Credits) VALUES
(1, 'Mathematics', 4),
(2, 'Physics', 4),
(3, 'Chemistry', 4),
(4, 'Biology', 4),
(5, 'Computer Science', 4),
(6, 'History', 3),
(7, 'Geography', 3),
(8, 'English', 3),
(9, 'Economics', 3),
(10, 'Philosophy', 3);
// Inserting values into Enrollments table
INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID,
EnrollmentDate, Grade) VALUES
(1, 1, 1, '2018-09-05', 'A'),
(2, 2, 2, '2017-09-10', 'B'),
(3, 3, 3, '2019-09-12', 'A'),
(4, 4, 4, '2018-09-15', 'C'),
(5, 5, 5, '2016-09-20', 'B'),
(6, 6, 6, '2020-09-25', 'A'),
(7, 7, 7, '2019-09-28', 'B'),
(8, 8, 8, '2017-10-01', 'A'),
(9, 9, 9, '2018-10-05', 'C'),
(10, 10, 10, '2020-10-10', 'B');
3.creating views:-
CREATE VIEW StudentEnrollments AS
SELECT s.StudentID, s.FirstName, s.LastName, c.CourseName,
e.EnrollmentDate, e.Grade
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
JOIN Courses c ON e.CourseID = c.CourseID;
Querying the View:-
//Querying the StudentEnrollments view
SELECT * FROM StudentEnrollments;
OUTPUT:-
StudentID | FirstName | LastName | CourseName | EnrollmentDate |
Grade
----------------------------------------------------------------------------
1 | Tejaswini | Beri | Mathematics | 2018-09-05 |A
2 | Suresh | Azmeera | Physics | 2017-09-10 |B
3 | Eswar | Bandaru | Chemistry | 2019-09-12 |A
4 | Ankkosh | GiriGosai | Biology | 2018-09-15 |C
5 | snehitha | Bandaru | Computer Science | 2016-09-20 |B
6 | preethi | satya | History | 2020-09-25 |A
7 | sam | tata | Geography | 2019-09-28 |B
8 | Olivia | Taylor | English | 2017-10-01 |A
9 | Alexander | Anderson | Economics | 2018-10-05 |C
10 | Sophia | Thomas | Philosophy | 2020-10-10 |B
4.TRIGGERS:-
CREATE TRIGGER UpdateEnrollmentDate
BEFORE INSERT ON Enrollments
FOR EACH ROW
SET NEW.EnrollmentDate = NOW();
Querying the Trigger:-
INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID,
Grade) VALUES (11, 1, 2, 'A');
OUTPUT:-
EnrollmentID | StudentID | CourseID | EnrollmentDate | Grade
---------------------------------------------------------------------------------
11 |1 |2 | 2024-07-12 15:30:00 |A
5.AGGREGATION FUNCTIONS:-
//Minimum grade in each course
SELECT CourseID, MIN(Grade) AS MinGrade
FROM Enrollments
GROUP BY CourseID;
OUTPUT:-
CourseID | MinGrade
-------------------
1 |A
2 |B
3 |A
4 |C
5 |B
6 |A
7 |B
8 |A
9 |C
10 |B
//Maximum credits of courses
SELECT MAX(Credits) AS MaxCredits
FROM Courses;
OUTPUT:-
MaxCredits
----------
4
// Average age of students
SELECT AVG(YEAR(CURRENT_DATE) - YEAR(BirthDate)) AS
AvgAge
FROM Students;
OUTPUT:-
AvgAge
------
22.7
//Total number of enrollments
SELECT COUNT(*) AS TotalEnrollments
FROM Enrollments;
OUTPUT:-
TotalEnrollments
----------------
10
ALL:-
//Students enrolled in all courses
SELECT * FROM Students WHERE StudentID NOT IN (SELECT
StudentID FROM Enrollments WHERE CourseID NOT IN (SELECT
CourseID FROM Courses));
OUTPUT:-
No rows found.
IN:-
//Students enrolled in specific courses (IN clause)
SELECT * FROM Students WHERE StudentID IN (SELECT
StudentID FROM Enrollments WHERE CourseID IN (1, 2, 3));
OUTPUT:-
StudentID | FirstName | LastName | BirthDate | Gender |
AdmissionDate
-----------------------------------------------------------------------
11 | Tejaswini | Beri | 2000-01-15 | Female | 2018-09-01
2 | Suresh | Azmeera | 1999-05-20 | Male | 2017-09-01
3 | Eswar | Bandaru | 2001-03-25 | Male | 2019-09-01
5 | snehitha | Bandaru | 1998-12-12 | Female | 2016-09-01
7 | sam | tata | 2001-06-18 | Female | 2019-09-01
10 | Sophia | Thomas | 2002-05-05 | Female | 2020-09-01
UNION:-
// Using UNION to combine results of two queries
SELECT FirstName, LastName FROM Students WHERE Gender =
'Male'
UNION
SELECT FirstName, LastName FROM Students WHERE BirthDate >
'2000-01-01';
OUTPUT:-
FirstName | LastName
---------------------
Alexander | Anderson
snehitha | Bandaru
Tejaswini | Beri
sam | tata
Eswar | Bandaru
Tejaswini | Beri
Alexander | Anderson
snehitha | Bandaru
INTERSECT:-
// Using INTERSECT (not supported in MySQL, simulated with JOIN)
SELECT s.FirstName, s.LastName
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID = 1
AND s.StudentID IN (SELECT StudentID FROM Enrollments WHERE
CourseID = 2);
OUTPUT:-
FirstName | LastName
---------------------
Tejaswini | Beri
snehitha | Bandaru
CONSTRAINTS:-
//Adding constraints
ALTER TABLE Students ADD CONSTRAINT chk_gender CHECK
(Gender IN ('Male', 'Female'));
ALTER TABLE Courses ADD CONSTRAINT chk_credits CHECK
(Credits >= 1 AND Credits <= 5);
GROUP BY:-
SELECT CourseID, COUNT(*) AS EnrollmentCount
FROM Enrollments
GROUP BY CourseID;
OUTPUT:-
CourseID | EnrollmentCount
--------------------------
1 |1
2 |1
3 |1
4 |1
5 |1
6 |1
7 |1
8 |1
9 |1
10 |1
ORDER BY:-
SELECT *
FROM Courses
ORDER BY CourseName ASC;
OUTPUT:-
CourseID | CourseName | Credits
--------------------------------------
10 | Philosophy |3
9 | Economics |3
8 | English |3
7 | Geography |3
6 | History |3
5 | Computer Science | 4
4 | Biology |4
3 | Chemistry |4
2 | Physics |4
1 | Mathematics |4
HAVING:-
SELECT CourseID, AVG(CASE WHEN Grade = 'A' THEN 4
WHEN Grade = 'B' THEN 3
WHEN Grade = 'C' THEN 2
ELSE 1 END) AS AvgGrade
FROM Enrollments
GROUP BY CourseID
HAVING AVG(CASE WHEN Grade = 'A' THEN 4
WHEN Grade = 'B' THEN 3
WHEN Grade = 'C' THEN 2
ELSE 1 END) > 3;
OUTPUT:-
CourseID | AvgGrade
-------------------
1 | 4.0000
2 | 3.0000
3 | 4.0000
4 | 2.0000
5 | 3.0000
6 | 4.0000
7 | 3.0000
8 | 4.0000
9 | 2.0000
10 | 3.0000
7.PROCEDURES:-
//Delimiter change for creating procedure
DELIMITER $$
//Create procedure
CREATE PROCEDURE GetStudentsByCourseID (IN courseId INT)
BEGIN
SELECT s.StudentID, s.FirstName, s.LastName, s.BirthDate,
s.Gender, s.AdmissionDate
FROM Students s
JOIN Enrollments e ON s.StudentID = e.StudentID
WHERE e.CourseID = courseId;
END$$
Calling the Procedure and Output
// Call the procedure for CourseID = 1
CALL GetStudentsByCourseID(1);
OUTPUT:-
StudentID | FirstName | LastName | BirthDate | Gender |
AdmissionDate
----------------------------------------------------------------------
1 | Tejaswini | Beri |2000-01-15 | Male | 2018-09-01
5 | snehitha | Bandaru | 1998-12-12 | Male | 2016-09-01
8.CURSOR:-
//Delimiter change for creating cursor
DELIMITER $$
//Create procedure with cursor
CREATE PROCEDURE GetStudentDetails()
BEGIN
// Declare variables to hold cursor data
DECLARE studentId INT;
DECLARE firstName VARCHAR(50);
DECLARE lastName VARCHAR(50);
DECLARE done INT DEFAULT FALSE;
//Declare cursor
DECLARE studentCursor CURSOR FOR
SELECT StudentID, FirstName, LastName
FROM Students;
// Declare handler for cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done =
TRUE;
-- Open cursor
OPEN studentCursor;