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

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

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