0% found this document useful (0 votes)
53 views41 pages

Student Mark Analysis with SQL Commands

The document outlines various exercises performed in a Database Management System lab, including student mark analysis using SQL commands, generating Fibonacci series, calculating factorials, reversing strings, summing natural numbers, creating triggers, and using cursors. Each exercise includes an aim, algorithm, source code, and results demonstrating successful execution. The exercises illustrate the application of DDL, DML, and TCL commands, as well as PL/SQL programming techniques.

Uploaded by

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

Student Mark Analysis with SQL Commands

The document outlines various exercises performed in a Database Management System lab, including student mark analysis using SQL commands, generating Fibonacci series, calculating factorials, reversing strings, summing natural numbers, creating triggers, and using cursors. Each exercise includes an aim, algorithm, source code, and results demonstrating successful execution. The exercises illustrate the application of DDL, DML, and TCL commands, as well as PL/SQL programming techniques.

Uploaded by

Computer science
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 1,2,3 STUDENT MARK ANALYSIS USING DDL, DML, AND TCL
COMMANDS.

AIM:

To perform student mark analysis by creating a table using DDL commands,


manipulating data using DML commands, and handling transactions using TCL
commands such as COMMIT and ROLLBACK.

ALGORITHM:

1. Start the program.


2. Create a table StudentMarks using the CREATE TABLE statement with the
fields:
o StudentID, StudentName, Subject1, Subject2, Subject3, Total, Average,
Grade.
3. Insert records into the table using INSERT INTO.
4. Calculate Total and Average marks using UPDATE statements.
5. Assign grades based on average using a CASE statement in UPDATE.
6. Use TCL Commands:
o Begin a transaction.
o Perform insert/update.
o Use ROLLBACK to undo incorrect operations.
o Use COMMIT to save correct transactions.
7. Use SELECT queries to display results.
8. End the program.

1 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

Source Code / SQL Commands:


🔹 DDL Commands
SQL>CREATE TABLE StudentMarks ( StudentID INT PRIMARY KEY,StudentName
VARCHAR(15), Subject1 INT,Subject2 INT, Subject3 INT,Total INT, Average FLOAT,
Grade CHAR(1));

Table created.

🔹 DML Commands
SQL>INSERT INTO StudentMarks (StudentID, StudentName, Subject1, Subject2,
Subject3)VALUES (1, 'Anita', 78, 85, 90);

1 row created.

SQL>INSERT INTO StudentMarks (StudentID, StudentName, Subject1, Subject2,


Subject3)VALUES (2, 'Bala', 65, 70, 75);

1 row created.

SQL>UPDATE StudentMarks SET Total = Subject1 + Subject2 + Subject3,Average =


(Subject1 + Subject2 + Subject3) / 3.0;

2 rows updated.

SQL>UPDATE StudentMarks SET Grade = CASE WHEN Average >= 90 THEN 'A'
WHEN Average >= 75 THEN 'B' WHEN Average >= 60 THEN 'C'WHEN Average >= 50
THEN 'D' ELSE 'F' END;

2 rows updated.

🔹 TCL Commands
SQL>UPDATE StudentMarks SET Subject1 = 0 WHERE StudentID = 2;

2 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

1 row updated.

-- Undo the change


SQL>ROLLBACK;
Rollback complete.

SQL>INSERT INTO StudentMarks (StudentID, StudentName, Subject1, Subject2, Subject3)


VALUES (3, 'Chitra', 88, 76, 92);
1 row created.

SQL>UPDATE StudentMarks SET Total = Subject1 + Subject2 + Subject3,Average =


(Subject1 + Subject2 + Subject3) / 3.0,Grade = CASE WHEN Average >= 90 THEN 'A'
WHEN Average >= 75 THEN 'B' WHEN Average >= 60 THEN 'C' WHEN Average >= 50
THEN 'D' ELSE 'F' END WHERE StudentID = 3;

1 row updated.

🔹 Verification Queries
SQL>SELECT * FROM StudentMarks;
OUTPUT:
Table: StudentMarks

StudentID StudentName Subject1 Subject2 Subject3 Total Average Grade

1 Anita 78 85 90 253 84.33 B

2 Bala 65 70 75 210 70.00 C

3 Chitra 88 76 92 256 85.33 B

3 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SQL>SELECT StudentName, Average, Grade FROM StudentMarks WHERE Average > 70;

Query Output: Students with Average > 70

StudentName Average Grade

Anita 84.33 B

Chitra 85.33 B

Rollback was successful; Bala's Subject1 was not changed to 0.

SQL>commit;

Commit complete.

4 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:
The SQL program for Student Mark Analysis was successfully executed. The
student table was created using DDL, manipulated using DML, and transaction controls were
demonstrated using TCL commands

5 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 4 FIBONACCI SERIES

AIM:

To write a PL/SQL program to generate the Fibonacci series up to N terms.

ALGORITHM:

1. Start the program.


2. Read the number of terms (N).
3. Initialize first two terms as 0 and 1.
4. Use a loop to generate the next term by summing the previous two.
5. Print each term within the loop.
6. End the program.

6 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/ PL-SQL Commands:

SQL> SET SERVEROUTPUT ON;

SQL> DECLARE

2 a NUMBER := 0;

3 b NUMBER := 1;

4 c NUMBER;

5 n NUMBER := 10;

6 i NUMBER := 3;

7 BEGIN

8 DBMS_OUTPUT.PUT_LINE(a);

9 DBMS_OUTPUT.PUT_LINE(b);

10 WHILE i <= n LOOP

11 c := a + b;

12 DBMS_OUTPUT.PUT_LINE(c);

13 a := b;

14 b := c;

15 i := i + 1;

16 END LOOP;

17 END;

18 /

7 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

OUTPUT

13

21

34

PL/SQL procedure successfully completed.

8 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The PL/SQL program to generate the Fibonacci series was executed successfully and
the output was verified.

9 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 5
FACTORIAL

AIM:

To write a PL/SQL program to calculate the factorial of a given number.

ALGORITHM:

1. Start the program.


2. Read a number N.
3. Initialize result as 1.
4. Use a loop to multiply result by each number up to N.
5. Display the factorial value.
6. End the program.

10 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/ PL/SQL COMMANDS:

SQL> set serveroutput on;

SQL> DECLARE

2 n NUMBER := 5;

3 fact NUMBER := 1;

4 i NUMBER := 1;

5 BEGIN

6 WHILE i <= n LOOP

7 fact := fact * i;

8 i := i + 1;

9 END LOOP;

10 DBMS_OUTPUT.PUT_LINE('Factorial is: ' || fact);

11 END;

12 /

OUTPUT:

Factorial is: 120

PL/SQL procedure successfully completed.

11 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The PL/SQL program to calculate the factorial of a number was executed successfully
and the output was verified.

12 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 6
STRING REVERSE

AIM:

To write a PL/SQL program to reverse a given string.

ALGORITHM:

1. Start the program.


2. Read a string input.
3. Initialize reverse string as null.
4. Use a loop to concatenate characters in reverse order.
5. Display the reversed string.
6. End the program.

13 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/PL-SQL COMMANDS :

SQL> set serveroutput on;

SQL> DECLARE

2 str VARCHAR2(50) := 'PLSQL';

3 rev VARCHAR2(50) := '';

4 i NUMBER;

5 BEGIN

6 FOR i IN REVERSE 1..LENGTH(str) LOOP

7 rev := rev || SUBSTR(str, i, 1);

8 END LOOP;

9 DBMS_OUTPUT.PUT_LINE('Reversed string: ' || rev);

10 END;

11 /

OUTPUT:

Reversed string: LQSLP

PL/SQL procedure successfully completed.

14 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The PL/SQL program to reverse a string was executed successfully and the output
was verified.

15 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 7
SUM OF SERIES

AIM:

To write a PL/SQL program to find the sum of the first N natural numbers.

ALGORITHM:

1. Start the program.


2. Read the number N.
3. Initialize sum to 0.
4. Use a loop from 1 to N to add each number to sum.
5. Display the result.
6. End the program.

16 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/PL-SQL COMMAND:

SQL> SET SERVEROUTPUT ON;

SQL> DECLARE

2 n NUMBER := 10;

3 total NUMBER := 0; -- renamed variable

4 i NUMBER := 1;

5 BEGIN

6 WHILE i <= n LOOP

7 total := total + i;

8 i := i + 1;

9 END LOOP;

10 DBMS_OUTPUT.PUT_LINE('Sum is: ' || total);

11 END;

12 /

OUTPUT:

Sum is: 55

17 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The PL/SQL program to calculate the sum of a series was executed successfully and
the output was verified.

18 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 8
TRIGGER

AIM:

To create a trigger that logs insert operations performed on the Student table.

ALGORITHM:

1. Start by creating a log table to store activity.


2. Create a trigger that activates BEFORE INSERT on the Student table.
3. In the trigger, insert the new student name into the log table.
4. Execute an insert on the Student table to test the trigger.
5. Display the log data to verify trigger execution.
6. End.

19 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/PL-SQL COMMANDS:

Create Student table

SQL>CREATE TABLE Student (StudentID NUMBER PRIMARY KEY,StudentName


VARCHAR2(50));

Table Created

Create StudentLog table

SQL>CREATE TABLE StudentLog ( LogID NUMBER PRIMARY KEY, ActionType


VARCHAR2(10), LogDate DATE DEFAULT SYSDATE, StudentName VARCHAR2(50));

Table created.

Create sequence for LogID

SQL>CREATE SEQUENCE studentlog_seq START WITH 1 INCREMENT BY 1


NOCACHE;

Sequence created.

Trigger to auto-fill LogID on StudentLog insert

SQL>CREATE OR REPLACE TRIGGER trg_studentlog_id BEFORE INSERT ON


StudentLog

FOR EACH ROW

BEGIN

SELECT studentlog_seq.NEXTVAL

INTO :[Link]

FROM dual;

END;

20 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

Trigger created.

Trigger to log inserts into Student

SQL>CREATE OR REPLACE TRIGGER trg_log_student

BEFORE INSERT ON Student

FOR EACH ROW

BEGIN

INSERT INTO StudentLog (ActionType, StudentName)

VALUES ('INSERT', :[Link]);

END;

Trigger created.

SQL>INSERT INTO Student (StudentID, StudentName) VALUES (1, 'Anita');

1 Row Updated

SQL>INSERT INTO Student (StudentID, StudentName) VALUES (2, 'Bala');

1 Row Updated

SQL>INSERT INTO Student (StudentID, StudentName) VALUES (3, 'Chitra');

1 Row Updated

Check the log table

SQL>SELECT * FROM StudentLog;

LogID ActionType LogDate StudentName

1 INSERT 22-SEP-25 Anita

2 INSERT 22-SEP-25 Bala

3 INSERT 22-SEP-25 Chitra

21 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The trigger on the Student table was successfully created and verified. Log entries are
captured during insertions.

22 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 9
STUDENT MARK ANALYSIS USING CURSOR

AIM:

To write a PL/SQL program using a cursor to perform student mark analysis and
display the total and result status for each student.

ALGORITHM:

1. Start the program.


2. Create a table named Student_Marks with fields: Roll_No, Name, Mark1, Mark2,
Mark3.
3. Insert sample records into the table.
4. Declare a cursor to retrieve student data row by row.
5. For each student record:
o Calculate the total marks.
o Determine pass/fail status (assume pass if all marks ≥ 40).
6. Display student name, total marks, and result.
7. Close the cursor.
8. End the program.

23 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE

SQL> CREATE TABLE Student_Marks (Roll_No NUMBER,Name


VARCHAR2(50),Mark1 NUMBER,Mark2 NUMBER,Mark3 NUMBER);

Table created.

SQL> INSERT INTO Student_Marks VALUES (1, 'John', 85, 67, 74);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (2, 'Mary', 55, 38, 49);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (3, 'Alex', 90, 92, 88);

1 row created.

SQL> INSERT INTO Student_Marks VALUES (4, 'Sara', 35, 40, 45);

1 row created.

SQL> SET SERVEROUTPUT ON

SQL> DECLARE

2 CURSOR student_cursor IS

3 SELECT * FROM Student_Marks;

4 stu_rec student_cursor%ROWTYPE;

5 total NUMBER;

6 result VARCHAR2(10);

7 BEGIN

24 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

8 OPEN student_cursor;

9 LOOP

10 FETCH student_cursor INTO stu_rec;

11 EXIT WHEN student_cursor%NOTFOUND;

12 total := stu_rec.Mark1 + stu_rec.Mark2 + stu_rec.Mark3;

13 IF stu_rec.Mark1 >= 40 AND stu_rec.Mark2 >= 40 AND stu_rec.Mark3 >= 40 THEN

14 result := 'Pass';

15 ELSE

16 result := 'Fail';

17 END IF;

18 DBMS_OUTPUT.PUT_LINE('Name: ' || stu_rec.Name || ', Total: ' || total || ', Result:


' || result);

19 END LOOP;

20 CLOSE student_cursor;

21 END;

22 /

25 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

OUTPUT

Name: John, Total: 226, Result: Pass

Name: Mary, Total: 142, Result: Fail

Name: Alex, Total: 270, Result: Pass

Name: Sara, Total: 120, Result: Fail

PL/SQL procedure successfully completed.

26 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The PL/SQL program using a cursor to analyze student marks was successfully
executed. It correctly calculated total marks and determined the pass/fail status for each
student.

27 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 10
LIBRARY MANAGEMENT SYSTEM

AIM:

To develop a PL/SQL application to manage library books with functions for adding books,
issuing books, returning books, and viewing available books.

ALGORITHM:

1. Create a Library table with columns: BookID, Title, Author, and Status.
2. Write functions/procedures to:
o Add a new book.
o Issue a book (change status to 'Issued').
o Return a book (change status to 'Available').
o List all available books.
3. Perform operations via these functions.
4. Display appropriate messages after each operation.
5. End the program.

28 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/ SQL COMMANDS:


Create Table
SQL> CREATE TABLE Library (BookID NUMBER PRIMARY KEY,Title
VARCHAR2(100),Author VARCHAR2(50),Status VARCHAR2(10) DEFAULT
'Available');

Table created.

Insert Sample Data

SQL> INSERT INTO Library VALUES (101, 'Database Concepts', 'Elmasri','Available');

1 row created.

SQL> INSERT INTO Library VALUES (102, 'PL/SQL Programming', 'Ullman','Available');

1 row created.

SQL> INSERT INTO Library VALUES (103, 'Java for Beginners','Herbert','Available');

1 row created.

Procedure to Add Book

SQL> CREATE OR REPLACE PROCEDURE Add_Book(p_BookID IN NUMBER,p_Title


IN VARCHAR2,p_Author IN VARCHAR2) AS

2 BEGIN

3 INSERT INTO Library(BookID, Title, Author, Status)

4 VALUES (p_BookID, p_Title, p_Author, 'Available');

5 DBMS_OUTPUT.PUT_LINE('Book added successfully.');

6 END;

7 /

29 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

Procedure created.

Procedure to Issue Book

SQL> CREATE OR REPLACE PROCEDURE Issue_Book(p_BookID IN NUMBER) AS


v_status VARCHAR2(10);

2 BEGIN

3 SELECT Status INTO v_status FROM Library WHERE BookID = p_BookID;

5 IF v_status = 'Available' THEN

6 UPDATE Library SET Status = 'Issued' WHERE BookID = p_BookID;

7 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' issued successfully.');

8 ELSE

9 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' is already issued.');

10 END IF;

11 EXCEPTION

12 WHEN NO_DATA_FOUND THEN

13 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' does not exist.');

14 END;

15 /

Procedure created.

30 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

Procedure to Return Book

SQL> CREATE OR REPLACE PROCEDURE Return_Book(p_BookID IN NUMBER) AS


v_status VARCHAR2(10);

2 BEGIN

3 SELECT Status INTO v_status FROM Library WHERE BookID = p_BookID;

5 IF v_status = 'Issued' THEN

6 UPDATE Library SET Status = 'Available' WHERE BookID = p_BookID;

7 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' returned successfully.');

8 ELSE

9 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' is not issued.');

10 END IF;

11 EXCEPTION

12 WHEN NO_DATA_FOUND THEN

13 DBMS_OUTPUT.PUT_LINE('Book ID ' || p_BookID || ' does not exist.');

14 END;

15 /

Procedure created.

31 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

Procedure to List Available Books

SQL> CREATE OR REPLACE PROCEDURE List_Available_Books AS

2 BEGIN

3 FOR rec IN (SELECT * FROM Library WHERE Status = 'Available') LOOP

4 DBMS_OUTPUT.PUT_LINE('BookID: ' || [Link] || ', Title: ' || [Link] || ',


Author: ' || [Link]);

5 END LOOP;

6 END;

7 /

Procedure created.

Testing the procedures

SQL> BEGIN

2 Add_Book(104, 'Python Basics', 'Guido');

3 Issue_Book(102);

4 Return_Book(103);

5 List_Available_Books;

6 END;

7 /

32 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

OUTPUT

Book added successfully.

Book ID 102 issued successfully.

Book ID 103 is not issued.

BookID: 101, Title: Database Concepts, Author: Elmasri

BookID: 103, Title: Java for Beginners, Author: Herbert

BookID: 104, Title: Python Basics, Author: Guido

PL/SQL procedure successfully completed.

33 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The enhanced PL/SQL library management system successfully adds books, issues
and returns books, and lists available books with appropriate validations.

34 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

[Link]: 11
STUDENT MARK ANALYSIS

AIM:

To write a PL/SQL application with functions to analyze student marks, calculate


total, average, grade, and determine pass/fail status.

ALGORITHM:

1. Create a StudentDetails table with columns: RollNo, Name, M1, M2, M3.
2. Write functions to:
o Calculate total marks.
o Calculate average marks.
o Determine grade based on average.
o Determine pass/fail status (minimum 40 in each subject to pass).
3. Use a cursor to process each student record.
4. Display student details including total, average, grade, and result.
5. End the program.

35 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

SOURCE CODE/ SQL COMMANDS:

SQL> CREATE TABLE StudentDetails (RollNo NUMBER PRIMARY KEY, Name


VARCHAR2(50),M1 NUMBER,M2 NUMBER,M3 NUMBER);

Table created.

SQL> INSERT INTO StudentDetails VALUES (1, 'Alice', 78, 69, 85);

1 row created.

SQL> INSERT INTO StudentDetails VALUES (2, 'Bhuvana', 45, 39, 60);

1 row created.

SQL> INSERT INTO StudentDetails VALUES (3, 'Charlie', 90, 95, 88);

1 row created.

SQL> SELECT * FROM StudentDetails;

ROLL NO NAME M1 M2 M3
1 Alice 78 69 85
2 Charlie 45 39 60
3 Bhuvana 90 95 88

SQL> CREATE OR REPLACE FUNCTION Calc_Total(m1 NUMBER, m2 NUMBER, m3


NUMBER) RETURN NUMBER AS

2 BEGIN

3 RETURN m1 + m2 + m3;

4 END;

36 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

5 /

Function created.

SQL> CREATE OR REPLACE FUNCTION Calc_Average(total NUMBER) RETURN


NUMBER AS

2 BEGIN

3 RETURN total / 3;

4 END;

5 /

Function created.

SQL> CREATE OR REPLACE FUNCTION Get_Grade(avg_marks NUMBER) RETURN


VARCHAR2 AS

2 BEGIN

3 IF avg_marks >= 85 THEN

4 RETURN 'A';

5 ELSIF avg_marks >= 70 THEN

6 RETURN 'B';

7 ELSIF avg_marks >= 50 THEN

8 RETURN 'C';

9 ELSE

37 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

10 RETURN 'D';

11 END IF;

12 END;

13 /

Function created.

SQL> CREATE OR REPLACE FUNCTION Get_Result(m1 NUMBER, m2 NUMBER, m3


NUMBER) RETURN VARCHAR2 AS

2 BEGIN

3 IF m1 >= 40 AND m2 >= 40 AND m3 >= 40 THEN

4 RETURN 'Pass';

5 ELSE

6 RETURN 'Fail';

7 END IF;

8 END;

9 /

Function created.

SQL> DECLARE

2 CURSOR stud_cur IS SELECT * FROM StudentDetails;

38 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

3 rec stud_cur%ROWTYPE;

4 total NUMBER;

5 avg_marks NUMBER;

6 grade VARCHAR2(2);

7 result VARCHAR2(10);

8 BEGIN

9 OPEN stud_cur;

10 LOOP

11 FETCH stud_cur INTO rec;

12 EXIT WHEN stud_cur%NOTFOUND;

13 total := Calc_Total(rec.M1, rec.M2, rec.M3);

14 avg_marks := Calc_Average(total);

15 grade := Get_Grade(avg_marks);

16 result := Get_Result(rec.M1, rec.M2, rec.M3);

17 DBMS_OUTPUT.PUT_LINE('Name: ' || [Link] ||

18 ', Total: ' || total ||

19 ', Average: ' || ROUND(avg_marks, 2) ||

20 ', Grade: ' || grade ||

21 ', Result: ' || result);

22 END LOOP;

39 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

23 CLOSE stud_cur;

24 END;

25 /

OUTPUT

Name: Alice, Total: 232, Average: 77.33, Grade: B, Result: Pass

Name: Charlie, Total: 144, Average: 48, Grade: D, Result: Fail

Name: Bhuvana, Total: 273, Average: 91, Grade: A, Result: Pass

PL/SQL procedure successfully completed.

40 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]


IDHAYA COLLEGE OF ARTS AND SCIENCE FOR WOMEN-PUDUPALAYAM

RESULT:

The enhanced student mark analysis application calculates total, average, grade, and
pass/fail status correctly for each student and displays the detailed report

41 DATABASE MANAGEMENT SYSTEM LAB [23UPCS56]

You might also like