diff --git a/Practical/Practical Exam/PL-SQL/P1 - PL-SQL.md b/Practical/Practical Exam/PL-SQL/P1 - PL-SQL.md new file mode 100644 index 0000000..cde3529 --- /dev/null +++ b/Practical/Practical Exam/PL-SQL/P1 - PL-SQL.md @@ -0,0 +1,32 @@ +# P1 - PL-SQL code + +**Problem Statement:** Write a PL/SQL code block to calculate the area of a circle for a value of radius varying from 5 to 9. Store the radius and the corresponding values of calculated area in an empty table named areas, consisting of two columns, radius and area. + +--- + +## Procedure +```plsql +DECLARE + radius NUMBER; + area NUMBER; + nodata EXCEPTION; +BEGIN + DBMS_OUTPUT.PUT_LINE('Enter radius: '); + radius := &radius; -- for Live SQL, specify a value. Eg. radius := 5; + + IF NOT (radius BETWEEN 5 AND 9) THEN + raise nodata; + END IF; + + area := 3.14 * radius * radius; + + DBMS_OUTPUT.PUT_LINE('Area of circle with radius ' || radius || ' cm is ' || area || ' sq.cm.'); +EXCEPTION + WHEN nodata THEN + DBMS_OUTPUT.PUT_LINE('Radius should be between 5 and 9.'); +END; +/ + +``` + +--- diff --git a/Practical/Practical Exam/PL-SQL/P2 - Unnamed PL-SQL.md b/Practical/Practical Exam/PL-SQL/P2 - Unnamed PL-SQL.md new file mode 100644 index 0000000..72406fd --- /dev/null +++ b/Practical/Practical Exam/PL-SQL/P2 - Unnamed PL-SQL.md @@ -0,0 +1,102 @@ +# P2 - Unamed PL-SQL + +**Problem Statement:** +Write an Unnamed PL/SQL of code for the following requirements: - +Schema: +Borrower (Rollin, Name, DateofIssue, NameofBook, Status) +Fine (Roll_no,Date,Amt) +Accept roll_no & name of book from user. +Check the number of days (from date of issue). +1. If days are between 15 to 30 then fine amounts will be Rs 5 per +day. +2. If no. of days>30, per day fine will be Rs 50 per day & for days +less than 30, Rs. 5 per day. +3. After submitting the book, status will change from I to R. +4. If condition of fine is true, then details will be stored into +fine table. + +--- + +## Creating tables +```plsql +CREATE TABLE Borrower ( + rollin NUMBER, + Name VARCHAR2(255), + DateofIssue DATE, + NameofBook VARCHAR2(255), + Status VARCHAR(255), + PRIMARY KEY (rollin) +); + +CREATE TABLE Fine ( + Roll_no NUMBER, + DateofReturn DATE, + Amt NUMBER, + FOREIGN KEY (Roll_no) REFERENCES Borrower (rollin) +); + +``` + +> [!WARNING] +> Notice inconsistent naming for columns? We're just doing it by the books. Blame the one who made these [problem statements](https://git.kska.io/sppu-te-comp-content/DatabaseManagementSystems/src/branch/main/Practical/Practical%20Exam/DBMSL%20-%20Problem%20Statements%20for%20Practical%20Exam%20%28November%202024%29.pdf). + +## Inserting data +```plsql +INSERT INTO Borrower VALUES (1, 'Kalas', TO_DATE('2024-10-19', 'YYYY-MM-DD'), 'DBMS', 'I'); +INSERT INTO Borrower VALUES (2, 'Himanshu', TO_DATE('2024-11-01', 'YYYY-MM-DD'), 'IOT', 'I'); +INSERT INTO Borrower VALUES (3, 'Mepa', TO_DATE('2024-10-29', 'YYYY-MM-DD'), 'TOC', 'I'); +INSERT INTO Borrower VALUES (4, 'Jambo', TO_DATE('2024-10-20', 'YYYY-MM-DD'), 'CNS', 'I'); + +``` + +## Procedure +```plsql +DECLARE + p_roll NUMBER; + p_book VARCHAR2(255); + p_issueDate DATE; + totalDays INT; + fineAmt INT; + nodata EXCEPTION; +BEGIN + DBMS_OUTPUT.PUT_LINE('Enter roll number: '); + p_roll := &p_roll; -- specify value directly for Live SQL. Eg.: p_roll := 1; + DBMS_OUTPUT.PUT_LINE('Enter book name: '); + p_book := &p_book; -- specify value directly for Live SQL. Eg.: p_roll := 'DBMS'; + + IF (p_roll <= 0) THEN + RAISE nodata; + END IF; + + SELECT DateofIssue INTO p_issueDate FROM Borrower WHERE rollin = p_roll AND NameofBook = p_book; + SELECT TRUNC(SYSDATE) - p_issueDate INTO totalDays FROM dual; + + IF (totalDays > 30) THEN + fineAmt := (15 * 5) + ((totalDays - 15) * 50); -- Rs 5 for first 15 days, Rs 50 for remaining + DBMS_OUTPUT.PUT_LINE('Roll no. ' || p_roll || ' has been fined Rs. ' || fineAmt); + INSERT INTO Fine VALUES (p_roll, SYSDATE, fineAmt); + UPDATE Borrower SET Status = 'R' WHERE Rollin = p_roll AND NameofBook = p_book; + ELSIF (totalDays BETWEEN 15 AND 30) THEN + fineAmt := totalDays * 5; + DBMS_OUTPUT.PUT_LINE('Roll no. '|| p_roll || ' has been fined Rs. ' || fineAmt); + INSERT INTO Fine VALUES (p_roll, SYSDATE, fineAmt); + UPDATE Borrower SET Status = 'R' WHERE Rollin = p_roll AND NameofBook = p_book; + ELSE + fineAmt := 0; + DBMS_OUTPUT.PUT_LINE('No fine for roll no. '|| p_roll); + INSERT INTO Fine VALUES (p_roll, SYSDATE, fineAmt); + UPDATE Borrower SET Status = 'R' WHERE Rollin = p_roll AND NameofBook = p_book; + END IF; +EXCEPTION + WHEN nodata THEN + DBMS_OUTPUT.PUT_LINE('Please enter a valid roll number.'); + WHEN NO_DATA_FOUND THEN + DBMS_OUTPUT.PUT_LINE('Something went wrong. Please check input data.'); + WHEN OTHERS THEN + DBMS_OUTPUT.PUT_LINE('Error occured. Error: ' || SQLERRM); +END; +/ + +``` + +--- diff --git a/Practical/Practical Exam/PL-SQL/P3 - Cursor.md b/Practical/Practical Exam/PL-SQL/P3 - Cursor.md new file mode 100644 index 0000000..25eedcd --- /dev/null +++ b/Practical/Practical Exam/PL-SQL/P3 - Cursor.md @@ -0,0 +1,58 @@ +# P3 - Cursor + +**Problem Statement:** Write a PL/SQL block of code using Cursor that will merge the data available in the newly created table N_Roll Call with the data available in the table O_RollCall. If the data in the first table already exist in the second table, then that data should be skipped. + +--- + +## Creating tables +```plsql +CREATE TABLE O_RollCall ( + roll NUMBER NOT NULL, + name VARCHAR2(255), + class VARCHAR2(255) +); + +CREATE TABLE N_RollCall ( + roll NUMBER NOT NULL, + name VARCHAR2(255), + class VARCHAR2(255) +); + +``` + +## Inserting values +```plsql +INSERT INTO O_RollCall VALUES (2, 'Eddie', 'COMP-2'); +INSERT INTO O_RollCall VALUES (3, 'Foxy', 'COMP-1'); +INSERT INTO O_RollCall VALUES (5, 'Stomp', 'COMP-3'); + +INSERT INTO N_RollCall VALUES (1, 'Stewie', 'COMP-1'); +INSERT INTO N_RollCall VALUES (2, 'Eddie', 'COMP-2'); +INSERT INTO N_RollCall VALUES (3, 'Foxy', 'COMP-1'); +INSERT INTO N_RollCall VALUES (4, 'Lara', 'COMP-3'); +INSERT INTO N_RollCall VALUES (5, 'Stomp', 'COMP-3'); + +``` + +## Procedure +```plsql +DECLARE + p_roll NUMBER; + p_name VARCHAR2(255); + p_class VARCHAR2(255); + CURSOR c1 IS SELECT * FROM N_RollCall WHERE roll NOT IN (SELECT roll FROM O_RollCall); +BEGIN + OPEN c1; + LOOP + FETCH c1 INTO p_roll, p_name, p_class; + INSERT INTO O_RollCall VALUES (p_roll, p_name, p_class); + EXIT WHEN c1%NOTFOUND; + DBMS_OUTPUT.PUT_LINE('Inserted ' || p_name || ' having roll no. ' || p_roll || ' in class ' || p_class || '.'); + END LOOP; + CLOSE c1; +END; +/ + +``` + +---