-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
169 lines (148 loc) · 5.25 KB
/
queries.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- not needed for Microsoft SQL
-- CREATE DATABASE LIBRARY ;
-- USE LIBRARY ;
/*Creating table to store book details */
CREATE TABLE Book (
bookName VARCHAR(500) NOT NULL,
edition VARCHAR(50),
language CHAR(50),
year INT,
bookid VARCHAR(30) PRIMARY KEY ,
rating DECIMAL(3,1),
genre VARCHAR(50),
author VARCHAR (50),
quantity INT CHECK (quantity > 1)
);
/*inserting data on book table */
INSERT INTO Book(bookName, edition, language, year, bookid, rating, genre,author,quantity)
VALUES
('The Hobbit', 'First Edition', 'English', 1937, '9780007497904', 4.2, 'Fantasy', 'J.R.R.Tolkien',5),
('Harry Potter and the Philosopher''s Stone', 'First Edition', 'English', 1997, '9780747532699', 4.5, 'Fantasy','J.K. Rowling',15),
('Alice in Wonderland', 'Revised Edition', 'English', 1865, '9781503290283', 4.1, 'Fantasy','Lewis Carroll',18),
('The Chronicles of Narnia', 'Complete Collection', 'English', 1950, '9780064404990', 4.6, 'Fantasy','C.S. Lewis',10),
('Pride and Prejudice', 'Revised Edition', 'English', 1813, '9780141439518', 4.7, 'Classic','Jane Austen',15),
('To Kill a Mockingbird', 'First Edition', 'English', 1960, '9780062420701', 4.5, 'Fiction','Harper Lee',16),
('The Great Gatsby', 'First Edition', 'English', 1925, '9780743273565', 4.2, 'Classic','F. Scott Fitzgerald',18),
('1984', 'Revised Edition', 'English', 1949, '9780451524935', 4.3, 'Dystopian','George Orwell',20),
('The Catcher in the Rye', 'First Edition', 'English', 1951, '9780316769488', 4.0, 'Fiction','J.D. Salinger',20),
('Moby-Dick', 'Revised Edition', 'English', 1851, '9781503280789', 4.4, 'Adventure',' Herman Melville',20);
/*Creating table to store person details */
CREATE TABLE Person
(personid INT PRIMARY KEY,
email CHAR(200));
/*Inserting Records in pErson table*/
INSERT INTO Person (personid,email)
VALUES
(1, '[email protected]'),
(2, '[email protected]'),
(3, '[email protected]'),
(4, '[email protected]'),
(5, '[email protected]'),
(6, '[email protected]'),
(7, '[email protected]'),
(8, '[email protected]'),
(9, '[email protected]'),
(10,'[email protected]');
/*Userinfo table*/
CREATE TABLE Userinfo
(email CHAR(200) PRIMARY KEY,
Personname CHAR(50) NOT NULL );
/*Inserting Records*/
INSERT INTO Userinfo ( email, personname)
VALUES
('[email protected]', 'John Doe'),
('[email protected]', 'Jane Smith'),
('[email protected]', 'Michael Johnson'),
('[email protected]', 'Emily Davis'),
('[email protected]', 'David Wilson'),
('[email protected]', 'Sarah Brown'),
('[email protected]', 'Christopher Lee'),
('[email protected]', 'Olivia Taylor'),
('[email protected]', 'Daniel Anderson'),
('[email protected]', 'Sophia Martinez');
/*Fine table*/
CREATE table Fine
(transactionid INT PRIMARY KEY,
fineamt DECIMAL);
/* insert Recors for fine table */
insert into fine (transactionid,fineamt)
values
(1,5),
(2,5),
(3,5),
(4,3),
(5,4),
(6,0),
(7,1),
(8,0),
(9,2),
(10,0);
/*Action table */
CREATE TABLE Action
(transactionid INT PRIMARY KEY IDENTITY(1,1),
bookid VARCHAR(30),
personid INT,
borrowdate DATE,
returndate DATE,
FOREIGN KEY (Bookid) REFERENCES Book(Bookid) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (personid) REFERENCES Person(personid) ON DELETE CASCADE ON UPDATE CASCADE) ;
/* inserting records */
insert into Action (bookid , personid,borrowdate,returndate)
VALUES ('9780062420701' ,1,'2023-05-25',NULL),
('9780064404990',1,'2023-05-25',NULL),
('9780062420701',1,'2023-05-25',NULL),
('9780062420701',1,'2023-05-25',NULL),
('9780062420701',4,'2023-05-25',NULL),
('9780062420701',1,'2023-05-25',NULL),
('9780062420701',1,'2023-05-25','2023-05-25'),
('9780062420701',3,'2023-05-25',NULL),
('9780062420701',1,'2023-05-25','2023-05-30'),
('9780062420701',2,'2023-05-25',NULL);
/*function to check books availability */
CREATE FUNCTION BookExists(@bookID VARCHAR(30))
RETURNS BIT
AS
BEGIN
DECLARE @exists BIT;
-- Check if the user ID exists in the Users table
IF EXISTS (SELECT 1 FROM Book WHERE bookid = @bookID and quantity > 0 )
SET @exists = 1;
ELSE
SET @exists = 0;
RETURN @exists;
END;
GO
/*function to authorise user */
CREATE FUNCTION UserExists(@userID INT)
RETURNS BIT
AS
BEGIN
DECLARE @exists BIT;
-- Check if the user ID exists in the Users table
IF EXISTS (SELECT 1 FROM Person WHERE personid = @userID)
SET @exists = 1;
ELSE
SET @exists = 0;
RETURN @exists;
END;
GO
/*Trigers when book returned past 30 days of borrowdate */
CREATE TRIGGER CalculateFineTrigger
ON Action
AFTER UPDATE
AS
BEGIN
-- Check if the returndate is updated
IF UPDATE(returndate)
BEGIN
-- Insert into the Fine table for each updated row that meets the condition
INSERT INTO Fine (transactionid, fineamt)
SELECT i.transactionid,
CASE WHEN DATEDIFF(day, i.borrowdate, i.returndate) > 30
THEN DATEDIFF(day, i.borrowdate, i.returndate) - 30
ELSE 0
END AS fineamt
FROM inserted i
INNER JOIN Action a ON i.transactionid = a.transactionid;
END;
END;