0% found this document useful (0 votes)
44 views4 pages

PL/SQL Stored Procedure for Student Grading

Uploaded by

hritik raj
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)
44 views4 pages

PL/SQL Stored Procedure for Student Grading

Uploaded by

hritik raj
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

NAME : - SARTHAK SAMPAT ZENDE

ROLL NO : – 552
DBMSL T – 18

Assignment No - 6
Problem Statement :
Write a Stored Procedure namely proc_Grade for the categorization of student. If marks scored
by students in examination is <=1500 and marks>=990 then student will be placed in distinction
category if marks scored are between 989 and900 category is first class, if marks 899 and 825
category is Higher Second Class Write a PL/SQL block for using procedure created with above
requirement.
Stud_Marks(name, total_marks)

mysql> create database marks;


Query OK, 1 row affected (0.12 sec)

mysql> use marks;


Database changed
mysql> create table studmarks(name varchar(20),total_marks integer);
Query OK, 0 rows affected (0.30 sec)

mysql> create table result(roll_no integer,name varchar(20),class varchar(25));


Query OK, 0 rows affected (0.28 sec)

mysql> DELIMITER $
mysql> create procedure proc_grade(in rollno tinyint,in name varchar(15),in marks int)
-> begin
-> declare class varchar(25);
-> if marks>=990 and marks<=1500 then set class="Distinction";
-> elseif marks<=989 and marks>=900 then set class="First Class";
-> elseif marks<=899 and marks>=825 then set class="Second Class";
-> elseif marks<=824 and marks>=700 then set class="Pass";
-> else
-> set class="Fail";
-> end if;
-> insert into studmarks values(name,marks);
-> insert into result values(rollno,name,class);
-> end$
Query OK, 0 rows affected (0.08 sec)

mysql> DELIMITER $
mysql> call proc_grade(1,"Aryan",850);
-> DELIMITER $
Query OK, 1 row affected (0.34 sec)

mysql> call proc_grade(2,"Peter",1000);


-> DELIMITER $
Query OK, 1 row affected (0.25 sec)

mysql> call proc_grade(3,"Smith",834);


-> DELIMITER $
Query OK, 1 row affected (0.09 sec)

mysql> call proc_grade(4,"Carol",750);


-> DELIMITER $
Query OK, 1 row affected (0.10 sec)

mysql> call proc_grade(5,"Bob",950);


-> DELIMITER $
Query OK, 1 row affected (0.07 sec)
mysql> select * from result;
-> DELIMITER $
+---------+-------+--------------+
| roll_no | name | class |
+---------+-------+--------------+
| 1 | Aryan | Second Class |
| 2 | Peter | Distinction |
| 3 | Smith | Second Class |
| 4 | Carol | Pass |
| 5 | Bob | First Class |
+---------+-------+--------------+
5 rows in set (0.04 sec)

mysql> select * from studmarks;


-> DELIMITER $
+-------+-------------+
| name | total_marks |
+-------+-------------+
| Aryan | 850 |
| Peter | 1000 |
| Smith | 834 |
| Carol | 750 |
| Bob | 950 |
+-------+-------------+
5 rows in set (0.00 sec)
mysql> DELIMITER $
mysql> create function tot_stud(classname varchar(25))
-> returns int
-> BEGIN
-> DECLARE TOTAL INT(20);
-> SELECT DISTINCT COUNT(*) INTO TOTAL FROM result where class=classname;
-> return TOTAL;
-> end $
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL
DATA in its declaration and binary logging is enabled (you *might* want to use the less safe
log_bin_trust_function_creators variable)
mysql> SET GLOBAL log_bin_trust_function_creators = 1;
-> DELIMITER $
Query OK, 0 rows affected (0.03 sec)

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1
mysql> DELIMITER $
mysql> create function tot_stud(classname varchar(25))
-> returns int
-> begin
-> declare total int(20);
-> select distinct count(*) into total from result where class=classname;
-> return total;
-> end $
Query OK, 0 rows affected, 1 warning (0.07 sec)

mysql> select tot_stud("Second Class");


-> DELIMITER $
+--------------------------+
| tot_stud("Second Class") |
+--------------------------+
| 2|
+--------------------------+
1 row in set (0.10 sec)

mysql> select tot_stud("Pass");


-> DELIMITER $
+------------------+
| tot_stud("Pass") |
+------------------+
| 1|
+------------------+
1 row in set (0.00 sec)
mysql> select tot_stud("First Class");
-> DELIMITER $
+-------------------------+
| tot_stud("First Class") |
+-------------------------+
| 1|
+-------------------------+

Common questions

Powered by AI

To determine the number of students in the 'Distinction' category, the query would be: `select tot_stud('Distinction');`. This query utilizes the `tot_stud` function to count the distinct entries in the `result` table where the class equals 'Distinction'.

Adjusting the `log_bin_trust_function_creators` variable is necessary when creating functions like `tot_stud` because MySQL requires additional security measures for functions affecting data that can alter binary logs if they are not marked as deterministic. Enabling this variable bypasses such restrictions.

While creating the function `tot_stud`, an error related to binary logging was encountered due to the absence of `DETERMINISTIC`, `NO SQL`, or `READS SQL DATA` in its declaration. This was resolved by setting the `log_bin_trust_function_creators` variable to 1 globally, allowing the non-safe function creation.

The `DELIMITER` command is used to change the statement delimiter from the default semicolon to another character or string temporarily. This permits programming in multi-line SQL statements or stored procedures until they are completed, at which point the delimiter is usually reset.

A developer may encounter errors due to MySQL's binary logging restrictions which prevent creating non-deterministic functions without specific flags. This is remedied by altering the `log_bin_trust_function_creators` setting, effectively allowing such operations in a more flexible environment.

Stored procedures like `proc_Grade` offer modularity, improved performance by reducing client-server communication, and maintainable logic encapsulation. However, they can lead to complexity in debugging, reduced portability across different database systems, and sometimes obscure performance optimizations.

The proc_Grade stored procedure classifies student marks into the following categories: 'Distinction' for marks between 990 and 1500, 'First Class' for marks between 900 and 989, 'Second Class' for marks between 825 and 899, 'Pass' for marks between 700 and 824, and 'Fail' for marks below 700.

MySQL error code 1064 indicates a syntax error usually caused by incorrect SQL statement structure or missing elements like the wrong DELIMITER setting for stored procedures. Prevention entails careful review of syntax rules and ensuring all statements, delimiters, and procedure steps are correctly declared.

The `proc_grade` stored procedure classifies a student with exactly 890 marks as 'First Class' since it checks conditions from the highest to the lowest. The marks are evaluated against 989 to 900, which includes 890, thereby falling within the 'First Class' bracket.

The `proc_Grade` procedure inserts records into the `studmarks` table with the student's name and total marks, and into the `result` table with the student's roll number, name, and classified class based on their marks. This integration updates both tables with new data of the student provided to the procedure.

You might also like