Simplified and improved queries for A6.

- Separate sections for creating tables and inserting data
- Fixed IF control block in procedure
- Refined procedure and calling block
This commit is contained in:
K 2024-11-02 20:35:43 +05:30
parent 25e9652392
commit c5b8f8223e
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

103
Practical/Assignment-A6/Queries-A6.md Executable file → Normal file
View File

@ -1,78 +1,79 @@
# Database Queries for Assignment-A6
## Creating tables & inserting data
## Creating tables
Student marks:
```sql
CREATE TABLE Stud_Marks(
Roll NUMBER(14),
name VARCHAR(255),
total_marks NUMBER(14),
PRIMARY KEY(Roll)
CREATE TABLE Stud_Marks (
roll INT,
name VARCHAR(255),
total_marks INT,
PRIMARY KEY (roll)
);
INSERT INTO Stud_Marks VALUES (1, 'Arora', 1350);
INSERT INTO Stud_Marks VALUES (2, 'Shankar', 979);
INSERT INTO Stud_Marks VALUES (3, 'Lara', 834);
INSERT INTO Stud_Marks VALUES (4, 'Jambo', 667);
INSERT INTO Stud_Marks VALUES (5, 'Tinde', 1400);
CREATE TABLE Result (
roll INT,
name VARCHAR(255),
class VARCHAR(255),
FOREIGN KEY (roll) REFERENCES Stud_Marks (roll)
);
```
Result:
## Inserting data
```sql
CREATE TABLE Result(
Roll NUMBER(14),
name VARCHAR(255),
Class VARCHAR(255),
FOREIGN KEY (Roll) REFERENCES Stud_Marks (Roll)
);
INSERT INTO Stud_Marks VALUES (1, 'Kshitij', 1400);
INSERT INTO Stud_Marks VALUES (2, 'Kalas', 500);
INSERT INTO Stud_Marks VALUES (3, 'Himanshu', 995);
INSERT INTO Stud_Marks VALUES (4, 'MEPA', 850);
INSERT INTO Stud_Marks VALUES (5, 'Macho', 900);
```
## Procedure
```sql
CREATE OR REPLACE PROCEDURE proc_grade
(rollno in number,
p_roll_no out Stud_Marks.Roll%type,
p_name out Stud_Marks.name%type,
p_total out Stud_Marks.total_marks%type)
AS
CREATE OR REPLACE PROCEDURE proc_Grade (roll_no IN NUMBER) AS
-- declare section
p_roll Stud_Marks.roll%TYPE;
p_name Stud_Marks.name%TYPE;
p_total NUMBER;
BEGIN
SELECT Roll, name, total_marks into p_roll_no, p_name, p_total from Stud_Marks WHERE Roll = rollno;
IF p_total <= 1500 and p_total >= 990
THEN
INSERT INTO Result VALUES (p_roll_no, p_name, 'Distinction');
ELSE IF p_total <= 989 and p_total >= 900
THEN
INSERT INTO Result VALUES (p_roll_no, p_name, 'First Class');
ELSE IF p_total <= 899 and p_total >= 825
THEN
INSERT INTO Result VALUES (p_roll_no, p_name, 'Higher Second Class');
ELSE
INSERT INTO Result VALUES (p_roll_no, p_name, 'FAIL');
END IF;
END IF;
END IF;
SELECT roll, name, total_marks INTO p_roll, p_name, p_total FROM Stud_Marks WHERE roll = roll_no;
IF (p_total <= 1500 AND p_total >= 990) THEN
INSERT INTO Result VALUES (p_roll, p_name, 'Distinction');
DBMS_OUTPUT.PUT_LINE(p_name || ' (roll no. ' || p_roll || ') has been placed in the DISTINCTION category.');
ELSIF (p_total BETWEEN 900 AND 989) THEN
INSERT INTO Result VALUES (p_roll, p_name, 'First Class');
DBMS_OUTPUT.PUT_LINE(p_name || ' (roll no. ' || p_roll || ') has been placed in the FIRST CLASS.');
ELSIF (p_total BETWEEN 825 AND 899) THEN
INSERT INTO Result VALUES (p_roll, p_name, 'Higher Second Class');
DBMS_OUTPUT.PUT_LINE(p_name || ' (roll no. ' || p_roll || ') has been placed in the HIGHER SECOND CLASS.');
ELSE
INSERT INTO Result VALUES (p_roll, p_name, 'Fail');
DBMS_OUTPUT.PUT_LINE(p_name || ' (roll no. ' || p_roll || ') has FAILED.');
END IF;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Rollno ' || rollno || ' is not present!!');
END;
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No entry for this roll number in the Stud_Marks database.');
END proc_Grade;
/
```
## Calling Procedure:
## Calling procedure
```sql
DECLARE
rollno number(14);
p_roll_no Stud_Marks.Roll%type;
p_name Stud_Marks.name%type;
p_total_marks Stud_Marks.total_marks%type;
roll_no NUMBER;
BEGIN
rollno := &rollno;
proc_grade(rollno, p_roll_no, p_name, p_total_marks);
roll_no := &roll_no; -- replace &roll_no with a number for Live SQL since it does not support user input.
proc_Grade(roll_no);
END;
/