0% found this document useful (0 votes)
4 views5 pages

Task 1

The document outlines the creation of a student attendance and grading system using SQL Server Management Studio (SSMS) with four main tables: Students, Subjects, Grades, and Attendance. It includes a stored procedure to insert grades with automatic remarks based on the grade value and a trigger to limit attendance records to a maximum of 10 per student per subject. Additionally, it provides test cases for inserting data and validating the functionality of the stored procedure and trigger.

Uploaded by

s.snjbarsubia
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)
4 views5 pages

Task 1

The document outlines the creation of a student attendance and grading system using SQL Server Management Studio (SSMS) with four main tables: Students, Subjects, Grades, and Attendance. It includes a stored procedure to insert grades with automatic remarks based on the grade value and a trigger to limit attendance records to a maximum of 10 per student per subject. Additionally, it provides test cases for inserting data and validating the functionality of the stored procedure and trigger.

Uploaded by

s.snjbarsubia
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/ 5

Task 1: Create database for student attendance and grading system.

Using SSMS SQL 20

Students Table
StudentID
FullName
Course

Subjects Table
SubjectCode
Description

Grades Table
GradeID
StudentID
SubjectCode
Grade
Remarks

Attendance Table
StudentID
SubjectCode
DateAttended

Task 2:

Sp_InsertGrade

The procedure must Accept Inputs: @StudentID, @SubjectCode, @Grade


Automatically compute the “Remarks” based on Grade:
“Passed” if Grade >= 75
“Failed” if Grade < 75
Insert record into the Grades table
-Meaning the stored procedure must be the one that decides the remarks

Task 3:
Create a trigger
Tr_AttendaceLimit

Create a BEFORE INSERT Trigger on the Attendance table that will:


Prevent a student from attending more than 10 sessions for the same subject.
If they already have 10 entries for a subject, raise an error and prevent the insert.

Criteria:

All four tables (Student,Subjects,Grades and Attendance) are created correctly with appropriate data
types and constraints.

A working stored procedure sp_InsertGrade exists, accepts parameters, inserts data, and computes
correct remarks.

A working trigger Tr_AttendanceLimit prevents more than 10 attendance records per student per
subject. Shows an appropriate error message.

Inserts at least 2 students, 2 subjects, and corresponding test data for grades and attendance.
Includes a test case showing that the trigger blocks the 11th attendance record.
Stored procedure assigns “PASSED” for grades > or = 75 and “FAILED” for grades < 75 accurately.

SQL is properly formatted, uses meaningful identifiers, and includes helpful comments.

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
FullName NVARCHAR(100) NOT NULL,
Course NVARCHAR(50) NOT NULL
);

CREATE TABLE Subjects (


SubjectCode NVARCHAR(20) PRIMARY KEY,
Description NVARCHAR(100) NOT NULL
);

CREATE TABLE Subjects (


SubjectCode NVARCHAR(20) PRIMARY KEY,
Description NVARCHAR(100) NOT NULL
);

CREATE TABLE Attendance (


AttendanceID INT IDENTITY(1,1) PRIMARY KEY,
StudentID INT NOT NULL,
SubjectCode NVARCHAR(20) NOT NULL,
DateAttended DATE NOT NULL DEFAULT GETDATE(),
CONSTRAINT FK_Attendance_Students FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
CONSTRAINT FK_Attendance_Subjects FOREIGN KEY (SubjectCode) REFERENCES
Subjects(SubjectCode)
);

CREATE PROCEDURE sp_InsertGrade


@StudentID INT,
@SubjectCode NVARCHAR(20),
@Grade DECIMAL(5,2)
AS
BEGIN
-- Validate grade range
IF @Grade < 0 OR @Grade > 100
BEGIN
RAISERROR('Grade must be between 0 and 100', 16, 1);
RETURN;
END

-- Check if student exists


IF NOT EXISTS (SELECT 1 FROM Students WHERE StudentID = @StudentID)
BEGIN
RAISERROR('Student does not exist', 16, 1);
RETURN;
END

-- Check if subject exists


IF NOT EXISTS (SELECT 1 FROM Subjects WHERE SubjectCode = @SubjectCode)
BEGIN
RAISERROR('Subject does not exist', 16, 1);
RETURN;
END

-- Determine remarks based on grade


DECLARE @Remarks NVARCHAR(20);
SET @Remarks = CASE WHEN @Grade >= 75 THEN 'Passed' ELSE 'Failed' END;

-- Insert the grade record


INSERT INTO Grades (StudentID, SubjectCode, Grade, Remarks)
VALUES (@StudentID, @SubjectCode, @Grade, @Remarks);

PRINT 'Grade record inserted successfully.';


END;

CREATE TRIGGER Tr_AttendanceLimit


ON Attendance
INSTEAD OF INSERT
AS
BEGIN
-- Check each record being inserted
DECLARE @StudentID INT, @SubjectCode NVARCHAR(20), @DateAttended DATE;

DECLARE attendance_cursor CURSOR FOR


SELECT StudentID, SubjectCode, DateAttended FROM inserted;

OPEN attendance_cursor;
FETCH NEXT FROM attendance_cursor INTO @StudentID, @SubjectCode, @DateAttended;

WHILE @@FETCH_STATUS = 0
BEGIN
-- Check if student already has 10 attendance records for this subject
DECLARE @AttendanceCount INT;
SELECT @AttendanceCount = COUNT(*)
FROM Attendance
WHERE StudentID = @StudentID AND SubjectCode = @SubjectCode;

IF @AttendanceCount >= 10
BEGIN
RAISERROR('Student %d has already attended the maximum 10 sessions for subject %s', 16, 1,
@StudentID, @SubjectCode);
END
ELSE
BEGIN
-- Insert the record if under limit
INSERT INTO Attendance (StudentID, SubjectCode, DateAttended)
VALUES (@StudentID, @SubjectCode, @DateAttended);
END

FETCH NEXT FROM attendance_cursor INTO @StudentID, @SubjectCode, @DateAttended;


END

CLOSE attendance_cursor;
DEALLOCATE attendance_cursor;
END;
-- Insert test data
INSERT INTO Students (StudentID, FullName, Course) VALUES
(1, 'Sarah Barsubia', 'DBMS'),
(2, 'Heart Necesito', 'HCI');

INSERT INTO Subjects (SubjectCode, Description) VALUES


('INFOSYS01', 'Computer Programming I'),
('INFOSYS02', 'Database Systems');

-- Test the stored procedure


EXEC sp_InsertGrade 1, 'INFOSYS01', 85.5; -- Should show "Passed"
EXEC sp_InsertGrade 1, 'INFOSYS02', 72.0; -- Should show "Failed"

-- Test the trigger


-- First insert 10 attendance records
INSERT INTO Attendance (StudentID, SubjectCode, DateAttended) VALUES
(1, 'INFOSYS01', '2023-01-01'),
--CREATE 9 MORE RECORDS
-- Then attempt an 11th record
INSERT INTO Attendance (StudentID, SubjectCode, DateAttended) VALUES
(1, 'INFOSYS01', '2023-01-11'); -- This should fail

-- First, insert 9 sessions for student 1 in CS101


INSERT INTO Attendance (StudentID, SubjectCode, DateAttended)
SELECT 1, 'CS101', DATEADD(DAY, number, '2023-01-01')
FROM (SELECT TOP 9 ROW_NUMBER() OVER (ORDER BY object_id) - 1 AS number
FROM sys.objects) AS numbers;

-- Try to insert 2 more sessions (would make 11 total)


BEGIN TRY
INSERT INTO Attendance (StudentID, SubjectCode, DateAttended)
VALUES
(1, 'CS101', '2023-01-10'),
(1, 'CS101', '2023-01-11');

PRINT 'Test failed - records were inserted';


END TRY
BEGIN CATCH
PRINT 'Test passed: ' + ERROR_MESSAGE();
END CATCH;

-- Verify only 9 records exist


SELECT COUNT(*) AS CurrentSessions
FROM Attendance
WHERE StudentID = 1 AND SubjectCode = 'CS101';

Task Test work:


A. Insert sample students and subjects
B. Try inserting more than 10 attendance records for one subject
C. Insert sample grades using the procedure and check if the remarks column is correct
-- B. Test attendance limit triggerPRINT '=== B. Testing attendance limit (10 sessions max) ===';
-- First insert 10 valid attendance records for Alice in CS101PRINT 'Inserting 10 attendance records for
Alice in CS101...';INSERT INTO Attendance (StudentID, SubjectCode, DateAttended)SELECT 101,
'CS101', DATEADD(DAY, number, '2023-09-01')FROM (SELECT TOP 10 ROW_NUMBER() OVER (ORDER
BY object_id) - 1 AS number
FROM sys.objects) AS numbers;
-- Verify the 10 records were insertedSELECT COUNT(*) AS AliceCS101Count FROM Attendance
WHERE StudentID = 101 AND SubjectCode = 'CS101';
-- Attempt to insert an 11th record (should fail)PRINT 'Attempting to insert 11th attendance
record...';BEGIN TRY
INSERT INTO Attendance (StudentID, SubjectCode, DateAttended)
VALUES (101, 'CS101', '2023-09-11');
PRINT 'TEST FAILED: 11th record was inserted';END TRYBEGIN CATCH
PRINT 'TEST PASSED: ' + ERROR_MESSAGE();END CATCH;
GO

You might also like