Student Mark Analysis Using DDL
Student Mark Analysis Using DDL
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:
🔹 DDL Commands
CREATE TABLE StudentMarks ( StudentID INT PRIMARY KEY,StudentName VARCHAR(15),
Subject1 INT,Subject2 INT, Subject3 INT,Total INT, Average FLOAT, Grade
CHAR(1)
);
🔹 DML Commands
INSERT INTO StudentMarks (StudentID, StudentName, Subject1, Subject2,
Subject3)VALUES (1, 'Anita', 78, 85, 90);
UPDATE StudentMarks
SET
Total = Subject1 + Subject2 + Subject3,
Average = (Subject1 + Subject2 + Subject3) / 3.0;
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;
🔹 TCL Commands
UPDATE StudentMarks SET Subject1 = 0 WHERE StudentID = 2;
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;
COMMIT;
🔹 Verification Queries
SELECT * FROM StudentMarks;
📤 Output:
Table: StudentMarks
StudentID StudentName Subject1 Subject2 Subject3 Total Average Grade
Anita 84.33 B
Chitra 85.33 B
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.
II. PL/SQL
Here is your PL/SQL Lab Record for the following programs, formatted with clear sections:
Aim, Algorithm, Source Code, Output, and Result.
4. Fibonacci Series
Aim:
To write a PL/SQL program to generate the Fibonacci series up to N terms.
Algorithm:
Source Code:
DECLARE
a NUMBER := 0;
b NUMBER := 1;
c NUMBER;
n NUMBER := 10; -- Number of terms
i NUMBER := 3;
BEGIN
DBMS_OUTPUT.PUT_LINE(a);
DBMS_OUTPUT.PUT_LINE(b);
WHILE i <= n LOOP
c := a + b;
DBMS_OUTPUT.PUT_LINE(c);
a := b;
b := c;
i := i + 1;
END LOOP;
END;
Output:
0
1
1
2
3
5
8
13
21
34
Result:
The PL/SQL program to generate the Fibonacci series was executed successfully and the output
was verified.
5. Factorial
Aim:
To write a PL/SQL program to calculate the factorial of a given number.
Algorithm:
Source Code:
DECLARE
n NUMBER := 5;
fact NUMBER := 1;
i NUMBER := 1;
BEGIN
WHILE i <= n LOOP
fact := fact * i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial is: ' || fact);
END;
Output:
Result:
The PL/SQL program to calculate the factorial of a number was executed successfully and the
output was verified.
6. String Reverse
Aim:
To write a PL/SQL program to reverse a given string.
Algorithm:
Source Code:
DECLARE
str VARCHAR2(50) := 'PLSQL';
rev VARCHAR2(50) := '';
i NUMBER;
BEGIN
FOR i IN REVERSE 1..LENGTH(str) LOOP
rev := rev || SUBSTR(str, i, 1);
END LOOP;
DBMS_OUTPUT.PUT_LINE('Reversed string: ' || rev);
END;
Output:
7. Sum Of Series
Aim:
To write a PL/SQL program to find the sum of the first N natural numbers.
Algorithm:
Source Code:
DECLARE
n NUMBER := 10;
sum NUMBER := 0;
i NUMBER := 1;
BEGIN
WHILE i <= n LOOP
sum := sum + i;
i := i + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum is: ' || sum);
END;
Output:
Sum is: 55
Result:
The PL/SQL program to calculate the sum of a series was executed successfully and the output
was verified.
8. Trigger Example
Aim:
To create a trigger that logs insert operations performed on the Student table.
Algorithm:
Source Code:
-- Log Table
CREATE TABLE StudentLog (
LogID NUMBER GENERATED ALWAYS AS IDENTITY,
ActionType VARCHAR2(10),
LogDate DATE DEFAULT SYSDATE,
StudentName VARCHAR2(50)
);
-- Trigger Definition
CREATE OR REPLACE TRIGGER trg_log_student
BEFORE INSERT ON Student
FOR EACH ROW
BEGIN
INSERT INTO StudentLog (ActionType, StudentName)
VALUES ('INSERT', :NEW.StudentName);
END;
Output:
StudentLog table is updated when a new record is inserted into the Student
table.
Result:
The trigger on the Student table was successfully created and verified. Log entries are captured
during insertions.
III. CURSOR
Algorithm:
Source Code:
stu_rec student_cursor%ROWTYPE;
total NUMBER;
result VARCHAR2(10);
BEGIN
OPEN student_cursor;
LOOP
FETCH student_cursor INTO stu_rec;
EXIT WHEN student_cursor%NOTFOUND;
Output:
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.
APPLICATIONS
Certainly! I’ll expand both programs with more functions/features to make them more
comprehensive as a Lab Record. Here’s an enhanced version for Library Management System
and Student Mark Analysis with multiple functions.
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.
Source Code:
-- Step 1: Create Table
CREATE TABLE Library (
BookID NUMBER PRIMARY KEY,
Title VARCHAR2(100),
Author VARCHAR2(50),
Status VARCHAR2(10) DEFAULT 'Available'
);
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
Result:
The enhanced PL/SQL library management system successfully adds books, issues and returns
books, and lists available books with appropriate validations.
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.
Source Code:
-- Step 1: Create Table
CREATE TABLE StudentDetails (
RollNo NUMBER PRIMARY KEY,
Name VARCHAR2(50),
M1 NUMBER,
M2 NUMBER,
M3 NUMBER
);
Output:
Name: Alice, Total: 232, Average: 77.33, Grade: B, Result: Pass
Name: Bob, Total: 144, Average: 48.00, Grade: D, Result: Fail
Name: Charlie, Total: 273, Average: 91.00, Grade: A, Result: Pass
Result:
The enhanced student mark analysis application calculates total, average, grade, and pass/fail
status correctly for each student and displays the detailed report.