0% found this document useful (0 votes)
11 views12 pages

DBMS Aryan Assignment 2

The document outlines a lab manual for a Database Management System assignment at the Indian Institute of Information Technology, Bhopal, detailing the creation of database schemas for Students, Courses, Enrollments, Professors, and Departments. It includes SQL commands for creating tables, inserting sample records, and implementing triggers for various functionalities such as preventing low CGPA enrollments and maintaining HOD integrity. Additionally, it provides test cases to validate the triggers and database operations.

Uploaded by

mrunknownsir10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

DBMS Aryan Assignment 2

The document outlines a lab manual for a Database Management System assignment at the Indian Institute of Information Technology, Bhopal, detailing the creation of database schemas for Students, Courses, Enrollments, Professors, and Departments. It includes SQL commands for creating tables, inserting sample records, and implementing triggers for various functionalities such as preventing low CGPA enrollments and maintaining HOD integrity. Additionally, it provides test cases to validate the triggers and database operations.

Uploaded by

mrunknownsir10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

INDIAN INSTITUTE OF INFORMATION

TECHNOLOGY, BHOPAL

Assignment 2

Lab Manual: DBMS

Branch: Information Technology Section-2


4th Semester

Submitted by: Submitted to:


Aryan Dixit Dr. Nikhil Kumar Singh
Scholar no.: 23U03116 Assistant Professor, IIIT Bhopal
Database Schema (CREATE TABLE)
CREATE TABLE Students (

StudentID INT PRIMARY KEY,

Name VARCHAR(100),

Email VARCHAR(100) UNIQUE,

Department VARCHAR(50),

CGPA DECIMAL(3,2) CHECK (CGPA BETWEEN 0.0 AND 10.0)

);

CREATE TABLE Courses (

CourseID INT PRIMARY KEY,

CourseName VARCHAR(100),

Credits INT CHECK (Credits > 0),

Department VARCHAR(50)

);

CREATE TABLE Enrollments (

EnrollID INT PRIMARY KEY,

StudentID INT,

CourseID INT,

EnrollmentDate DATE,

Grade VARCHAR(2),

FOREIGN KEY (StudentID) REFERENCES Students(StudentID),

FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)


);

CREATE TABLE Professors (

ProfessorID INT PRIMARY KEY,

Name VARCHAR(100),

Email VARCHAR(100) UNIQUE,

Department VARCHAR(50),

Salary DECIMAL(10,2) CHECK (Salary >= 0)

);

CREATE TABLE Departments (

DepartmentID INT PRIMARY KEY,

DepartmentName VARCHAR(100),

HOD_ID INT,

FOREIGN KEY (HOD_ID) REFERENCES Professors(ProfessorID)

);

Sample Records to Insert:


INSERT INTO Students (StudentID, Name, Email, Department, CGPA)
VALUES

(1, 'Aarav Mehta', '[email protected]', 'CSE', 3.5),

(2, 'Neha Sharma', '[email protected]', 'ECE', 1.8),

(3, 'Rohan Patel', '[email protected]', 'IT', 2.9),

(4, 'Ishita Verma', '[email protected]', 'ME', 2.0),

(5, 'Kunal Singh', '[email protected]', 'CSE', 3.9);


INSERT INTO Courses (CourseID, CourseName, Credits, Department)
VALUES

(101, 'Data Structures', 4, 'CSE'),

(102, 'Digital Electronics', 3, 'ECE'),

(103, 'Operating Systems', 4, 'IT'),

(104, 'Thermodynamics', 3, 'ME'),

(105, 'Computer Networks', 3, 'CSE');

INSERT INTO Enrollments (EnrollID, StudentID, CourseID, EnrollmentDate,


Grade) VALUES

(201, 1, 101, '2025-03-01', 9),

(202, 1, 105, '2025-03-03', 8),


(203, 3, 103, '2025-03-05', 7),

(204, 4, 104, '2025-03-07', 6),

(205, 5, 101, '2025-03-10', 9);

INSERT INTO Professors (ProfessorID, Name, Email, Department, Salary)


VALUES

(301, 'Dr. Anil Joshi', '[email protected]', 'CSE', 90000),

(302, 'Dr. Pooja Rathi', '[email protected]', 'ECE', 85000),

(303, 'Dr. Vikram Das', '[email protected]', 'IT', 87000);

INSERT INTO Departments (DepartmentID, DepartmentName, HOD_ID)


VALUES

(1, 'Computer Science', 301),

(2, 'Electronics', 302),

(3, 'Information Technology', 303);


Implementing Triggers:

Trigger 1: Prevent enrollment if CGPA < 2.0

CREATE OR REPLACE FUNCTION prevent_low_cgpa_enrollment_fn()

RETURNS TRIGGER AS $$

DECLARE

student_cgpa DECIMAL(3,2);

BEGIN

SELECT CGPA INTO student_cgpa FROM Students WHERE StudentID =


NEW.StudentID;

IF student_cgpa < 2.0 THEN

RAISE EXCEPTION 'Enrollment denied: CGPA too low';

END IF;

RETURN NEW;

END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER prevent_low_cgpa_enrollment

BEFORE INSERT ON Enrollments


FOR EACH ROW

EXECUTE FUNCTION prevent_low_cgpa_enrollment_fn();

Trigger 2: Auto-update CGPA after grade insertion

CREATE OR REPLACE FUNCTION update_cgpa_after_grade_insert()

RETURNS TRIGGER AS $$

DECLARE

rec RECORD;

total_points DECIMAL := 0;

total_credits INT := 0;

weighted_sum DECIMAL := 0;

BEGIN

-- Loop through all enrollments of the student to recalculate CGPA

FOR rec IN

SELECT e.Grade, c.Credits

FROM Enrollments e

JOIN Courses c ON e.CourseID = c.CourseID

WHERE e.StudentID = NEW.StudentID

LOOP

weighted_sum := weighted_sum + (rec.Grade * rec.Credits);

total_credits := total_credits + rec.Credits;

END LOOP;

IF total_credits > 0 THEN


total_points := weighted_sum / total_credits;

END IF;

-- Update the student's CGPA

UPDATE Students

SET CGPA = ROUND(total_points::numeric, 2)

WHERE StudentID = NEW.StudentID;

RETURN NEW;

END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER update_cgpa_after_grade_insert

AFTER INSERT OR UPDATE ON Enrollments

FOR EACH ROW

EXECUTE FUNCTION update_cgpa_after_grade_insert();assuming grades


A=10, B=8, C=6, D=4, F=0 and CGPA is average of grade points.

Trigger 3: Restrict salary reduction for professors

CREATE OR REPLACE FUNCTION prevent_salary_reduction()

RETURNS TRIGGER AS $$

BEGIN

IF NEW.Salary < OLD.Salary THEN

RAISE EXCEPTION 'Salary reduction is not allowed';

END IF;
RETURN NEW;

END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER prevent_salary_cut

BEFORE UPDATE ON Professors

FOR EACH ROW

EXECUTE FUNCTION prevent_salary_reduction();

Trigger 4: Maintain HOD integrity if deleted

CREATE OR REPLACE FUNCTION maintain_hod_integrity()

RETURNS TRIGGER AS $$

BEGIN

UPDATE Departments

SET HOD_ID = NULL

WHERE HOD_ID = OLD.ProfessorID;

RETURN OLD;

END;

$$ LANGUAGE plpgsql;

CREATE TRIGGER nullify_hod_on_delete

AFTER DELETE ON Professors

FOR EACH ROW

EXECUTE FUNCTION maintain_hod_integrity();


Test Cases:

Attempt to Enroll a Student with Low CGPA (Expect Rejection)

INSERT INTO Enrollments (EnrollID, StudentID, CourseID, EnrollmentDate,


Grade)

VALUES (301, 2, 102, '2025-04-06', 7);

ERROR: Enrollment denied: CGPA too low

Insert Grades for Students and Verify CGPA Auto-Update

-- Student 1 - Two courses

INSERT INTO Enrollments (EnrollID, StudentID, CourseID, EnrollmentDate,


Grade)

VALUES

(302, 1, 101, '2025-04-06', 8),

(303, 1, 103, '2025-04-06', 9);

-- Student 3 - One course

INSERT INTO Enrollments (EnrollID, StudentID, CourseID, EnrollmentDate,


Grade)
VALUES

(304, 3, 101, '2025-04-06', 6);

SELECT StudentID, CGPA FROM Students;

Expected Output:
Student 1's CGPA ≈ weighted average of (8×4 + 9×4) / (4+4) = 8.5
Student 3's CGPA = 6.0

Try to Reduce a Professor's Salary (Expect Rejection)

INSERT INTO Professors (ProfessorID, Name, Email, Department, Salary)

VALUES (501, 'Dr. Anil Joshi', '[email protected]', 'CSE', 90000);

UPDATE Professors

SET Salary = 80000

WHERE ProfessorID = 501;


Expected Output:
ERROR: Salary reduction is not allowed

Delete an HOD’s Record and Verify Department’s HOD_ID Set to NULL--

DELETE FROM Professors WHERE ProfessorID = 301;

SELECT * FROM Departments WHERE HOD_ID IS NULL;

Expected: HOD is deleted, and Departments.HOD_ID is now NULL.

You might also like