Improved queries for A4.
- Changed column names for better understanding - Changed dates in inserted data to show all fine cases - Added comments in procdure and changed variable names
This commit is contained in:
parent
ebe898535f
commit
25e849ccd0
96
Practical/Assignment-A4+A5/Queries-A4.md
Executable file → Normal file
96
Practical/Assignment-A4+A5/Queries-A4.md
Executable file → Normal file
@ -4,19 +4,19 @@
|
|||||||
|
|
||||||
```sql
|
```sql
|
||||||
CREATE TABLE Borrower (
|
CREATE TABLE Borrower (
|
||||||
Roll_no NUMBER(15),
|
roll_no INT,
|
||||||
Name VARCHAR(255),
|
issuer_name VARCHAR(255),
|
||||||
DateOfIssue DATE,
|
issue_date DATE,
|
||||||
NameOfBook VARCHAR(255),
|
book_name VARCHAR(255),
|
||||||
Status VARCHAR(255),
|
status VARCHAR(1),
|
||||||
PRIMARY KEY(Roll_no)
|
PRIMARY KEY (roll_no)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE Fine (
|
CREATE TABLE Fine (
|
||||||
Roll_no NUMBER(15),
|
roll_no INT,
|
||||||
DateOfReturn DATE,
|
return_date DATE,
|
||||||
Amount NUMBER(15),
|
amt INT,
|
||||||
FOREIGN KEY(Roll_no) REFERENCES Borrower(Roll_no)
|
FOREIGN KEY (roll_no) REFERENCES Borrower (roll_no)
|
||||||
);
|
);
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -24,71 +24,63 @@ CREATE TABLE Fine (
|
|||||||
## Inserting data
|
## Inserting data
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (1, 'Ayush Kalaskar', TO_DATE('2024-07-28','YYYY-MM-DD'), 'DBMS', 'I');
|
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-10-01', 'YYYY-MM-DD'), 'TOC', 'I');
|
||||||
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (2, 'Himanshu Patil', TO_DATE('2024-07-01','YYYY-MM-DD'),'IOT', 'I');
|
INSERT INTO Borrower VALUES (3, 'MEPA', TO_DATE('2024-10-25', 'YYYY-MM-DD'), 'IoT', 'I');
|
||||||
|
INSERT INTO Borrower VALUES (4, 'Kshitij', TO_DATE('2024-10-29', 'YYYY-MM-DD'), '1984', 'I');
|
||||||
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (3, 'Kshitij', TO_DATE('2024-08-05','YYYY-MM-DD'), 'TOC', 'I');
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Procedure
|
## Procedure
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
DECLARE
|
DECLARE
|
||||||
p_nameofbook VARCHAR(255);
|
p_roll NUMBER; -- specify roll number here since Live SQL cannot take input from user
|
||||||
p_rollno NUMBER(15) := 1;
|
-- Eg. p_roll NUMBER := 1 will take roll number 1 as input
|
||||||
p_dateofissue DATE;
|
p_book VARCHAR2(255); -- specify book name here since Live SQL cannot take input from user
|
||||||
currentdate DATE;
|
-- Eg. p_book VARCHAR2(255) := 'DBMS'
|
||||||
noofdays NUMBER(15);
|
p_issueDate DATE;
|
||||||
amount NUMBER(15);
|
totalDays NUMBER;
|
||||||
|
currentDate DATE;
|
||||||
|
fineAmt NUMBER;
|
||||||
nodata EXCEPTION;
|
nodata EXCEPTION;
|
||||||
|
|
||||||
BEGIN
|
BEGIN
|
||||||
p_nameofbook := '&p_nameofbook';
|
-- Check if roll number is valid
|
||||||
currentdate := SYSDATE;
|
IF (p_roll <= 0) THEN
|
||||||
|
|
||||||
IF p_rollno <= 0 THEN
|
|
||||||
RAISE nodata;
|
RAISE nodata;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
SELECT DateOfIssue
|
-- Storing values from table in variables
|
||||||
INTO p_dateofissue
|
SELECT issue_date INTO p_issueDate FROM Borrower WHERE roll_no = p_roll AND book_name = p_book;
|
||||||
FROM Borrower
|
|
||||||
WHERE Roll_no = p_rollno AND NameOfBook = p_nameofbook;
|
|
||||||
|
|
||||||
SELECT TRUNC(SYSDATE) - p_dateofissue
|
-- Getting the total days since book issue
|
||||||
INTO noofdays
|
SELECT TRUNC(SYSDATE) - p_issueDate INTO totalDays FROM dual;
|
||||||
FROM dual;
|
|
||||||
|
|
||||||
DBMS_OUTPUT.PUT_LINE('No of days: ' || noofdays);
|
-- Calculating fine
|
||||||
IF (noofdays > 30) THEN
|
IF (totalDays > 30) THEN
|
||||||
amount := noofdays * 50;
|
fineAmt := totalDays * 50; -- Rs. 50 per day for total days greater than 30
|
||||||
ELSIF (noofdays >= 15 AND noofdays <= 30) THEN
|
ELSIF (totalDays BETWEEN 15 AND 30) THEN
|
||||||
amount := noofdays * 5;
|
fineAmt := totalDays * 5; -- Rs. 5 per day for total days between 15 and 30
|
||||||
ELSE
|
ELSE
|
||||||
amount := 0;
|
fineAmt := 0;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
||||||
IF amount > 0 THEN
|
-- Inserting data into Fine table
|
||||||
INSERT INTO Fine (Roll_no, DateOfReturn, Amount)
|
IF fineAmt > 0 THEN
|
||||||
VALUES (p_rollno, SYSDATE, amount);
|
DBMS_OUTPUT.PUT_LINE('Roll no. ' || p_roll || ' has been fined Rs. ' || fineAmt || ' for being ' || totalDays || ' days late.');
|
||||||
END IF;
|
INSERT INTO Fine VALUES (p_roll, SYSDATE, fineAmt);
|
||||||
|
ELSE
|
||||||
IF amount > 0 THEN
|
DBMS_OUTPUT.PUT_LINE('Roll no. ' || p_roll || ' does not have to pay any fine.');
|
||||||
UPDATE Borrower
|
|
||||||
SET Status = 'R'
|
|
||||||
WHERE Roll_no = p_rollno;
|
|
||||||
END IF;
|
END IF;
|
||||||
|
UPDATE Borrower SET status = 'R' WHERE roll_no = p_roll AND book_name = p_book;
|
||||||
|
|
||||||
EXCEPTION
|
EXCEPTION
|
||||||
WHEN nodata THEN
|
WHEN nodata THEN
|
||||||
DBMS_OUTPUT.PUT_LINE('Roll number or book name not found.');
|
DBMS_OUTPUT.PUT_LINE('Roll number' || p_roll || ' not found.');
|
||||||
WHEN NO_DATA_FOUND THEN
|
|
||||||
DBMS_OUTPUT.PUT_LINE('No entry found for the provided roll number and book name.');
|
|
||||||
WHEN OTHERS THEN
|
WHEN OTHERS THEN
|
||||||
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
|
DBMS_OUTPUT.PUT_LINE('An error occured. Error: ' || SQLERRM);
|
||||||
|
|
||||||
END;
|
END;
|
||||||
/
|
/
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user