-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHotelManagementDB.sql
388 lines (332 loc) · 14.6 KB
/
HotelManagementDB.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
CREATE DATABASE HotelManagementSystem;
DROP DATABASE HotelManagementSystem;
USE HotelManagementSystem;
GO
CREATE SCHEMA Authentication;
GO
GO
CREATE SCHEMA Hotels;
GO
GO
CREATE SCHEMA Bookings;
GO
CREATE TABLE Authentication.Admin (
AdminId INT IDENTITY (1,1),
Username NVARCHAR (30) UNIQUE NOT NULL,
Password NVARCHAR (30) NOT NULL,
CONSTRAINT PK_AdminId PRIMARY KEY (AdminId)
);
INSERT INTO Authentication.Admin VALUES ('admin110', 'hotels123');
CREATE TABLE Hotels.Hotel (
HotelId INT IDENTITY (1,1),
Name NVARCHAR (50) NOT NULL UNIQUE,
ContactNumber NVARCHAR (15) NOT NULL UNIQUE,
Email NVARCHAR (50) NOT NULL UNIQUE,
Website NVARCHAR (50) UNIQUE,
Description NVARCHAR (500),
FloorCount INT NOT NULL,
TotalRooms INT NOT NULL,
AddressLine NVARCHAR(50) UNIQUE NOT NULL,
Street NVARCHAR(50) NOT NULL,
City NVARCHAR(20) NOT NULL,
Zip NVARCHAR(50) NOT NULL,
Country NVARCHAR(30) NOT NULL,
CONSTRAINT PK_HotelId PRIMARY KEY (HotelId)
);
CREATE TABLE Hotels.Departments (
DepartmentId INT IDENTITY(1,1),
DepartmentName NVARCHAR (50) NOT NULL,
DepartmentDescription NVARCHAR (50) NOT NULL,
InitialSalary INT NOT NULL,
HotelId INT,
Designation NVARCHAR (50) NOT NULL DEFAULT 'Intern',
CONSTRAINT PK_DepartmentId PRIMARY KEY (DepartmentId),
CONSTRAINT FK_HotelId_dep FOREIGN KEY (HotelId) REFERENCES Hotels.Hotel(HotelID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE Hotels.Employees(
EmployeeId INT IDENTITY (1,1),
EmployeeFirstName NVARCHAR (50) NOT NULL,
EmployeeLastName NVARCHAR (50) NOT NULL,
EmployeeDesignation NVARCHAR (50) NOT NULL,
EmployeeContactNumber NVARCHAR (15) NOT NULL,
EmployeeEmailAddress NVARCHAR(50) NOT NULL UNIQUE,
EmployeeJoiningDate DATE NOT NULL,
AddressLine NVARCHAR(50) NOT NULL,
Street NVARCHAR(50) NOT NULL,
City NVARCHAR(20) NOT NULL,
Zip NVARCHAR(50) NOT NULL,
DepartmentId INT,
HotelId INT NOT NULL,
CNIC NVARCHAR(20) NOT NULL UNIQUE,
CONSTRAINT PK_EmployeeId PRIMARY KEY (EmployeeId),
CONSTRAINT FK_DepartmentId_Employee FOREIGN KEY (DepartmentId)
REFERENCES Hotels.Departments (DepartmentId) ON DELETE SET NULL ON UPDATE NO ACTION,
CONSTRAINT FK_HotelId_Employee FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel(HotelId) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE Authentication.Login (
LoginId INT IDENTITY (1,1),
Username NVARCHAR (30) NOT NULL UNIQUE,
Password NVARCHAR (30) NOT NULL,
EmployeeId INT,
SecurityQuestion NVARCHAR(100),
Answer NVARCHAR(50),
HotelId INT NOT NULL,
NewUser CHAR(5) CHECK (NewUser IN ('Yes', 'No')) DEFAULT 'Yes',
CONSTRAINT PK_LoginId PRIMARY KEY (LoginId),
CONSTRAINT FK_EmployeeId_Login FOREIGN KEY (EmployeeId)
REFERENCES Hotels.Employees (EmployeeId) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT FK_HotelId_login FOREIGN KEY (HotelId) REFERENCES Hotels.Hotel(HotelId) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE Hotels.Guests(
GuestId INT IDENTITY (1,1),
GuestFirstName NVARCHAR (50) NOT NULL,
GuestLastName NVARCHAR (50) NOT NULL,
GuestEmailAddress NVARCHAR(50) NOT NULL ,
GuestContactNumber NVARCHAR (15) NOT NULL,
GuestPassportNumber NVARCHAR(20) NOT NULL,
GuestCnic NVARCHAR (30) NOT NULL,
AddressLine NVARCHAR(50) NOT NULL,
Street NVARCHAR(50) NOT NULL,
City NVARCHAR(20) NOT NULL,
Zip NVARCHAR(50) NOT NULL,
HotelId INT NOT NULL,
Status NVARCHAR(20) CHECK (Status IN ('Reserved', 'Not Reserved')) NOT NULL DEFAULT 'Not Reserved',
CONSTRAINT PK_GuestId PRIMARY KEY (GuestId),
CONSTRAINT FK_HotelID_Guests FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel(HotelId) ON DELETE CASCADE ON UPDATE NO ACTION,
);
GO
CREATE SCHEMA HotelService;
GO
CREATE TABLE HotelService.Services (
ServiceId INT IDENTITY(1,1),
ServiceName NVARCHAR (50) NOT NULL,
ServiceDescription NVARCHAR (50) NOT NULL,
ServiceCost INT NOT NULL,
HotelId INT NOT NULL,
CONSTRAINT PK_ServicesId PRIMARY KEY (ServiceId),
CONSTRAINT FK_HotelId_Services FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel (HotelId) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE Bookings.Discount(
DiscountId INT IDENTITY (1,1),
DiscountName NVARCHAR(100) NOT NULL,
DiscountDescription NVARCHAR(50) NOT NULL,
DiscountRate INT NOT NULL,
EmployeeId INT NOT NULL,
CONSTRAINT PK_DiscountID PRIMARY KEY (DiscountId),
CONSTRAINT FK_EmployeeId_Discount FOREIGN KEY (EmployeeId)
REFERENCES Hotels.Employees (EmployeeId) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE Bookings.Booking(
BookingId INT IDENTITY (1,1),
BookingDate DATE NOT NULL,
StayDuration INT NOT NULL,
CheckInDate DATE NOT NULL,
CheckOutDate DATE NOT NULL,
BookingAmount INT NOT NULL,
HotelId INT NOT NULL,
EmployeeId INT NOT NULL,
GuestId INT NOT NULL,
DiscountId INT,
Status NVARCHAR (10) NOT NULL CHECK (Status IN ('Checkin', 'Checkout')) DEFAULT 'Checkin',
CONSTRAINT PK_BookingId PRIMARY KEY (BookingId),
CONSTRAINT FK_HotelId_Booking FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel (HotelId) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT FK_EmployeeId_Booking FOREIGN KEY (EmployeeId)
REFERENCES Hotels.Employees (EmployeeId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_GuestId_Booking FOREIGN KEY (GuestId)
REFERENCES Hotels.Guests(GuestId) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_DiscountId_Booking FOREIGN KEY (DiscountId)
REFERENCES Bookings.Discount(DiscountId) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE HotelService.ServicesUsed (
ServicesUserId INT IDENTITY (1,1),
ServiceId INT,
BookingId INT NOT NULL,
CONSTRAINT FK_ServiceId_ServicesUsed FOREIGN KEY (ServiceId)
REFERENCES HotelService.Services(ServiceId) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT FK_BookingId_ServicesUsed FOREIGN KEY (BookingId)
REFERENCES Bookings.Booking (BookingId) ON DELETE NO ACTION ON UPDATE NO ACTION
);
GO
CREATE SCHEMA Rooms;
GO
CREATE TABLE Rooms.RoomType (
RoomTypeId INT IDENTITY (1,1),
Name NVARCHAR (50) NOT NULL,
Cost INT NOT NULL,
SmokeFriendly NVARCHAR (5) NOT NULL,
PetFriendly NVARCHAR (5) NOT NULL,
HotelId INT,
CONSTRAINT PK_RoomTypeId PRIMARY KEY (RoomTypeId),
CONSTRAINT FK_HotelId_type FOREIGN KEY (HotelId) REFERENCES Hotels.Hotel(HotelID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE Rooms.Room (
RoomId INT IDENTITY (1,1),
RoomNumber NVARCHAR (10) NOT NULL,
HotelId INT,
RoomTypeId INT,
Available NVARCHAR (5) NOT NULL CHECK (Available IN ('Yes', 'No')) DEFAULT 'Yes',
CONSTRAINT PK_RoomId PRIMARY KEY (RoomId),
CONSTRAINT FK_HotelID_Room FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel (HotelId) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT FK_RoomTypeID_Room FOREIGN KEY (RoomTypeId)
REFERENCES Rooms.RoomType (RoomTypeId) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE TABLE Rooms.RoomBooked (
RoomBookedId INT IDENTITY (1,1),
BookingId INT NOT NULL,
RoomId INT NOT NULL,
CONSTRAINT PK_RoomBookedId PRIMARY KEY (RoomBookedId),
CONSTRAINT FK_BookingId_RoomBooked FOREIGN KEY (BookingId)
REFERENCES Bookings.Booking (BookingId) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT FK_RoomId_RoomBooked FOREIGN KEY (RoomId)
REFERENCES Rooms.Room (RoomId) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE Bookings.Payments(
PaymentId INT IDENTITY (1,1),
PaymentStatus NVARCHAR (6) NOT NULL,
PaymentType NVARCHAR(50) NOT NULL,
PaymentAmount INT NOT NULL,
BookingId INT NOT NULL,
CONSTRAINT PK_PaymentID PRIMARY KEY (PaymentId),
CONSTRAINT FK_BookingId_Payments FOREIGN KEY(BookingId)
REFERENCES Bookings.Booking(BookingId) ON DELETE CASCADE ON UPDATE CASCADE
);
SELECT SUM(PaymentAmount) FROM Bookings.Payments WHERE BookingId IN (SELECT BookingId FROM Bookings.Booking WHERE HotelId = 3);
SELECT PaymentId, PaymentStatus, PaymentType, PaymentAmount, BookingId FROM Bookings.Payments WHERE BookingId = (SELECT BookingId FROM Bookings.Booking WHERE HotelId = 3);
DROP TABLE Bookings.Payments;
ALTER TABLE Bookings.Payments DROP CONSTRAINT FK_BookingId_Payments;
ALTER TABLE Bookings.Payments ADD CONSTRAINT FK_BookingId_Payments FOREIGN KEY(BookingId)
REFERENCES Bookings.Booking(BookingId) ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE Bookings.Payments ADD HotelId INT NOT NULL;
ALTER TABLE Bookings.Payments ADD CONSTRAINT FK_HotelId_Payments FOREIGN KEY (HotelId)
REFERENCES Hotels.Hotel(HotelId) ON DELETE NO ACTION ON UPDATE NO ACTION;
SELECT Bookings.Booking.BookingId, Bookings.Booking.BookingDate, Bookings.Booking.StayDuration, Bookings.Booking.CheckInDate, Bookings.Booking.CheckOutDate, Bookings.Booking.Status, Hotels.Guests.GuestId, Hotels.Guests.GuestFirstName, Hotels.Guests.GuestLastName, Hotels.Guests.GuestContactNumber, Hotels.Guests.GuestPassportNumber, Hotels.Guests.HotelId, Hotels.Employees.EmployeeId, Hotels.Employees.EmployeeFirstName, Hotels.Employees.EmployeeLastName, Hotels.Employees.EmployeeContactNumber, Bookings.Payments.PaymentId, Bookings.Payments.PaymentStatus,Bookings.Payments.PaymentAmount FROM Bookings.Booking INNER JOIN Hotels.Employees ON Bookings.Booking.EmployeeId = Hotels.Employees.EmployeeId FULL JOIN Hotels.Guests ON Bookings.Booking.GuestId = Hotels.Guests.GuestId FULL JOIN Bookings.Payments ON Bookings.Booking.BookingId = Bookings.Payments.BookingId WHERE Bookings.Booking.HotelId = 3;
-- QUERIES
-- How many distinct guest have made bookings for a particular month?
SELECT GuestFirstName, GuestLastName, GuestContactNumber
FROM Hotels.Guests
WHERE GuestId IN (
SELECT DISTINCT GuestId
FROM Bookings.Booking
WHERE MONTH(CheckInDate) = 5
);
SELECT * FROM Bookings.Booking;
-- How many books has a customer made in one year?
SELECT COUNT(*) AS 'Total Bookings'
FROM Bookings.Booking
WHERE YEAR(BookingDate) = 2022 AND GuestId = 14;
-- How many rooms are available for particular hotel?
SELECT COUNT(RoomId) AS 'Available Rooms'
FROM Rooms.Room
WHERE HotelId = 3 AND Available = 'Yes';
-- TRIGGER Hotels.Hotel
CREATE TABLE AuditHotels (
AuditId INT IDENTITY (1,1) PRIMARY KEY,
AuditData NVARCHAR (1000)
);
CREATE TRIGGER tr_HotelsInsert
ON Hotels.Hotel
FOR INSERT
AS
BEGIN
DECLARE @Id INT
SELECT @Id = HotelId FROM inserted
INSERT INTO AuditHotels VALUES ('New hotel with HotelId = ' + CAST(@Id AS NVARCHAR) + ' inserted on ' + CAST(GETDATE() AS NVARCHAR))
END
CREATE TRIGGER tr_HotelsDelete
ON Hotels.Hotel
FOR DELETE
AS
BEGIN
DECLARE @Id INT
SELECT @Id = HotelId FROM deleted
INSERT INTO AuditHotels VALUES ('Existing hotel with HotelId = ' + CAST(@Id AS NVARCHAR) + ' deleted on ' + CAST(GETDATE() AS NVARCHAR))
END
CREATE TRIGGER trgHotelForUpdate
ON Hotels.Hotel
FOR UPDATE
AS
BEGIN
DECLARE @HotelId INT, @AuditString NVARCHAR(1000)
DECLARE @OldName NVARCHAR(100),@NewName NVARCHAR(100)
DECLARE @OldContactNumber NVARCHAR (15),@NewContactNumber NVARCHAR (15)
DECLARE @OldEmail NVARCHAR (50),@NewEmail NVARCHAR (50)
DECLARE @OldWebsite NVARCHAR (50),@NewWebsite NVARCHAR (50)
DECLARE @OldDescription NVARCHAR (100),@NewDescription NVARCHAR (100)
DECLARE @OldFloorCount INT,@NewFloorCount INT
DECLARE @OldTotalRooms INT,@NewTotalRooms INT
DECLARE @OldAddressLine NVARCHAR(50),@NewAddressLine NVARCHAR(50)
DECLARE @OldStreet NVARCHAR(50),@NewStreet NVARCHAR(50)
DECLARE @OldCity NVARCHAR(20),@NewCity NVARCHAR(20)
DECLARE @OldZip NVARCHAR(50),@NewZip NVARCHAR(50)
DECLARE @OldCountry NVARCHAR(30),@NewCountry NVARCHAR(30)
SELECT * INTO #TempTable FROM Inserted
WHILE(EXISTS(SELECT HotelId FROM #TempTable))
BEGIN
SET @AuditString = ''
SELECT TOP 1 @HotelId = HotelId, @NewName = Name, @NewEmail = Email, @NewWebsite = Website,
@NewDescription = Description, @NewFloorCount= FloorCount, @NewTotalRooms = TotalRooms,@NewAddressLine = AddressLine,@NewStreet= Street,
@NewCity = City, @NewZip= Zip, @NewCountry = Country
FROM #TempTable
SELECT @HotelId = HotelId, @OldName = Name, @OldEmail = Email, @OldWebsite = Website,
@OldDescription = Description, @OldFloorCount= FloorCount, @OldTotalRooms = TotalRooms,@OldAddressLine = AddressLine,@OldStreet= Street,
@OldCity = City, @OldZip= Zip, @OldCountry = Country
FROM Deleted
WHERE HotelId =@HotelId
SET @AuditString = 'Hotel With Id ' + CAST(@HotelId AS NVARCHAR) + ' changed '
IF(@NewName<>@OldName)
SET @AuditString = @AuditString + ' Name from ' + CAST(@OldName AS NVARCHAR) + ' to ' +
CAST(@NewName AS NVARCHAR)
IF(@NewEmail<>@OldEmail)
SET @AuditString = @AuditString + ' Email from ' + CAST(@OldEmail AS NVARCHAR) + ' to ' +
CAST(@NewEmail AS NVARCHAR)
IF(@NewWebsite<>@OldWebsite)
SET @AuditString = @AuditString + ' Website from ' + CAST(@OldWebsite AS NVARCHAR) + ' to ' +
CAST(@NewWebsite AS NVARCHAR)
IF(@NewDescription<>@OldDescription)
SET @AuditString = @AuditString + ' Description from ' + CAST(@OldDescription AS NVARCHAR) + ' to ' +
CAST(@NewDescription AS NVARCHAR)
IF(@NewFloorCount<>@OldFloorCount)
SET @AuditString = @AuditString + ' Floor count from ' + CAST(@OldFloorCount AS NVARCHAR) + ' to ' +
CAST(@NewFloorCount AS NVARCHAR)
IF(@NewTotalRooms<>@OldTotalRooms)
SET @AuditString = @AuditString + ' Total from ' + CAST(@OldTotalRooms AS NVARCHAR) + ' to ' +
CAST(@NewTotalRooms AS NVARCHAR)
IF(@NewAddressLine<>@OldAddressLine)
SET @AuditString = @AuditString + ' Address line from ' + CAST(@OldAddressLine AS NVARCHAR) + ' to ' +
CAST(@NewAddressLine AS NVARCHAR)
IF(@NewStreet<>@OldStreet)
SET @AuditString = @AuditString + ' Street from ' + CAST(@OldStreet AS NVARCHAR) + ' to ' +
CAST(@NewStreet AS NVARCHAR)
IF(@NewCity<>@OldCity)
SET @AuditString = @AuditString + ' City from ' + CAST(@OldCity AS NVARCHAR) + ' to ' +
CAST(@NewCity AS NVARCHAR)
IF(@NewZip<>@OldZip)
SET @AuditString = @AuditString + ' ZIp from ' + CAST(@OldZip AS NVARCHAR) + ' to ' +
CAST(@NewZip AS NVARCHAR)
IF(@NewCountry<>@OldCountry)
SET @AuditString = @AuditString + ' COuntry from ' + CAST(@OldCountry AS NVARCHAR) + ' to ' +
CAST(@NewCountry AS NVARCHAR)
INSERT INTO AuditTable Values (@AuditString)
DELETE FROM #TempTable Where HotelId = @HotelId
END
END
SELECT * FROM AuditHotels;
SELECT * FROM Authentication.Admin;
SELECT * FROM Authentication.Login;
SELECT * FROM Bookings.Booking;
SELECT * FROM Bookings.Discount;
SELECT * FROM Bookings.Payments;
SELECT * FROM Hotels.Departments;
SELECT * FROM Hotels.Employees;
SELECT * FROM Hotels.Guests;
SELECT * FROM Hotels.Hotel;
SELECT * FROM HotelService.Services;
SELECT * FROM HotelService.ServicesUsed;
SELECT * FROM Rooms.Room;
SELECT * FROM Rooms.RoomBooked;
SELECT * FROM Rooms.RoomType;