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

95 lines
2.0 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 NUMBER(15),
Name VARCHAR(255),
DateOfIssue DATE,
NameOfBook VARCHAR(255),
Status VARCHAR(255),
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 NUMBER(15),
DateOfReturn DATE,
Amount NUMBER(15),
FOREIGN KEY(Roll_no) REFERENCES Borrower(Roll_no)
);
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-09-29 23:00:38 +05:30
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (1, 'Ayush Kalaskar', TO_DATE('2024-07-28','YYYY-MM-DD'), 'DBMS', 'I');
2024-09-26 22:36:44 +05:30
2024-09-29 23:00:38 +05:30
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (2, 'Himanshu Patil', TO_DATE('2024-07-01','YYYY-MM-DD'),'IOT', 'I');
2024-09-26 22:36:44 +05:30
2024-09-29 23:00:38 +05:30
INSERT INTO Borrower (Roll_no, Name, DateOfIssue, NameOfBook, Status) values (3, 'Kshitij', TO_DATE('2024-08-05','YYYY-MM-DD'), 'TOC', '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
2024-09-29 23:00:38 +05:30
p_nameofbook VARCHAR(255);
p_rollno NUMBER(15) := 1;
p_dateofissue DATE;
currentdate DATE;
noofdays NUMBER(15);
amount NUMBER(15);
nodata EXCEPTION;
2024-09-26 22:36:44 +05:30
BEGIN
2024-09-29 23:00:38 +05:30
p_nameofbook := '&p_nameofbook';
currentdate := SYSDATE;
IF p_rollno <= 0 THEN
RAISE nodata;
END IF;
SELECT DateOfIssue
INTO p_dateofissue
FROM Borrower
WHERE Roll_no = p_rollno AND NameOfBook = p_nameofbook;
SELECT TRUNC(SYSDATE) - p_dateofissue
INTO noofdays
FROM dual;
DBMS_OUTPUT.PUT_LINE('No of days: ' || noofdays);
IF (noofdays > 30) THEN
amount := noofdays * 50;
ELSIF (noofdays >= 15 AND noofdays <= 30) THEN
amount := noofdays * 5;
ELSE
amount := 0;
END IF;
IF amount > 0 THEN
INSERT INTO Fine (Roll_no, DateOfReturn, Amount)
VALUES (p_rollno, SYSDATE, amount);
END IF;
IF amount > 0 THEN
UPDATE Borrower
SET Status = 'R'
WHERE Roll_no = p_rollno;
END IF;
EXCEPTION
WHEN nodata THEN
DBMS_OUTPUT.PUT_LINE('Roll number or book name 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
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
2024-09-26 22:36:44 +05:30
END;
/
```