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 (
|
2024-11-02 01:14:37 +05:30
|
|
|
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 (
|
2024-11-02 01:14:37 +05:30
|
|
|
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
|
2024-11-02 01:14:37 +05:30
|
|
|
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-11-02 01:14:37 +05:30
|
|
|
|
2024-09-26 22:36:44 +05:30
|
|
|
```sql
|
|
|
|
DECLARE
|
2024-11-02 01:14:37 +05:30
|
|
|
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
|
2024-11-02 01:14:37 +05:30
|
|
|
-- Check if roll number is valid
|
|
|
|
IF (p_roll <= 0) THEN
|
2024-09-29 23:00:38 +05:30
|
|
|
RAISE nodata;
|
|
|
|
END IF;
|
|
|
|
|
2024-11-02 01:14:37 +05:30
|
|
|
-- 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
|
|
|
|
2024-11-02 01:14:37 +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
|
|
|
|
2024-11-02 01:14:37 +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
|
2024-11-02 01:14:37 +05:30
|
|
|
fineAmt := 0;
|
2024-09-29 23:00:38 +05:30
|
|
|
END IF;
|
|
|
|
|
2024-11-02 01:14:37 +05:30
|
|
|
-- 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;
|
2024-11-02 01:14:37 +05:30
|
|
|
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
|
2024-11-02 01:14:37 +05:30
|
|
|
DBMS_OUTPUT.PUT_LINE('Roll number' || p_roll || ' not found.');
|
2024-09-29 23:00:38 +05:30
|
|
|
WHEN OTHERS THEN
|
2024-11-02 01:14:37 +05:30
|
|
|
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;
|
|
|
|
/
|
|
|
|
```
|