DatabaseManagementSystems/Practical/Assignment-A4+A5/Queries-A4.md

87 lines
2.4 KiB
Markdown
Raw Normal View History

2024-10-28 13:34:55 +05:30
# Database Queries for Assignment-A4
2024-09-29 23:00:38 +05:30
## Creating tables
2024-09-26 22:36:44 +05:30
```sql
2024-09-29 23:00:38 +05:30
CREATE TABLE Borrower (
roll_no INT,
issuer_name VARCHAR(255),
issue_date DATE,
book_name VARCHAR(255),
status VARCHAR(1),
PRIMARY KEY (roll_no)
2024-09-26 22:36:44 +05:30
);
2024-09-29 23:00:38 +05:30
CREATE TABLE Fine (
roll_no INT,
return_date DATE,
amt INT,
FOREIGN KEY (roll_no) REFERENCES Borrower (roll_no)
2024-09-29 23:00:38 +05:30
);
2024-09-26 22:36:44 +05:30
```
2024-09-29 23:00:38 +05:30
## Inserting data
2024-09-26 22:36:44 +05:30
```sql
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 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');
2024-09-26 22:36:44 +05:30
```
2024-09-29 23:00:38 +05:30
## Procedure
2024-09-26 22:36:44 +05:30
```sql
DECLARE
p_roll NUMBER; -- specify roll number here since Live SQL cannot take input from user
-- Eg. p_roll NUMBER := 1; will take roll number 1 as input
p_book VARCHAR2(255); -- specify book name here since Live SQL cannot take input from user
-- Eg. p_book VARCHAR2(255) := 'DBMS';
p_issueDate DATE;
totalDays NUMBER;
currentDate DATE;
fineAmt NUMBER;
2024-09-29 23:00:38 +05:30
nodata EXCEPTION;
2024-09-26 22:36:44 +05:30
BEGIN
-- Check if roll number is valid
IF (p_roll <= 0) THEN
2024-09-29 23:00:38 +05:30
RAISE nodata;
END IF;
-- Storing values from table in variables
SELECT issue_date INTO p_issueDate FROM Borrower WHERE roll_no = p_roll AND book_name = p_book;
2024-09-29 23:00:38 +05:30
-- Getting the total days since book issue
SELECT TRUNC(SYSDATE) - p_issueDate INTO totalDays FROM dual;
2024-09-29 23:00:38 +05:30
-- Calculating fine
IF (totalDays > 30) THEN
fineAmt := totalDays * 50; -- Rs. 50 per day for total days greater than 30
ELSIF (totalDays BETWEEN 15 AND 30) THEN
fineAmt := totalDays * 5; -- Rs. 5 per day for total days between 15 and 30
2024-09-29 23:00:38 +05:30
ELSE
fineAmt := 0;
2024-09-29 23:00:38 +05:30
END IF;
-- Inserting data into Fine table
IF fineAmt > 0 THEN
DBMS_OUTPUT.PUT_LINE('Roll no. ' || p_roll || ' has been fined Rs. ' || fineAmt || ' for being ' || totalDays || ' days late.');
INSERT INTO Fine VALUES (p_roll, SYSDATE, fineAmt);
ELSE
DBMS_OUTPUT.PUT_LINE('Roll no. ' || p_roll || ' does not have to pay any fine.');
2024-09-29 23:00:38 +05:30
END IF;
UPDATE Borrower SET status = 'R' WHERE roll_no = p_roll AND book_name = p_book;
2024-09-29 23:00:38 +05:30
EXCEPTION
WHEN nodata THEN
DBMS_OUTPUT.PUT_LINE('Roll number' || p_roll || ' not found.');
2024-09-29 23:00:38 +05:30
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occured. Error: ' || SQLERRM);
2024-09-29 23:00:38 +05:30
2024-09-26 22:36:44 +05:30
END;
/
```