forked from 7ravis/hotel-reservation-database
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotel_reservation_system.sql
More file actions
355 lines (319 loc) · 10.7 KB
/
hotel_reservation_system.sql
File metadata and controls
355 lines (319 loc) · 10.7 KB
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
drop database if exists HotelReservation;
create database HotelReservation;
use HotelReservation;
create table Brand (
BrandID smallint(10) unsigned not null auto_increment,
BrandName varchar(45) not null,
primary key (BrandID)
);
create table Hotel (
HotelID mediumint(10) unsigned not null auto_increment,
Brand_BrandID smallint(10) unsigned not null,
primary key (HotelID),
foreign key (Brand_BrandID)
references Brand (BrandID)
);
create table Employee (
EmployeeID bigint(20) unsigned not null auto_increment,
FirstName varchar(45) not null,
LastName varchar(45) not null,
primary key (EmployeeID)
);
create table HotelEmployee (
Hotel_HotelID mediumint(10) unsigned not null,
Employee_EmployeeID bigint(20) unsigned not null,
primary key (Hotel_HotelID, Employee_EmployeeID),
foreign key (Hotel_HotelID)
references Hotel (HotelID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID)
);
create table RoomType (
RoomTypeID smallint(10) unsigned not null auto_increment,
RoomType varchar(20) not null,
primary key (RoomTypeID)
);
create table RoomRate (
RoomRateID bigint(20) unsigned not null auto_increment,
Rate decimal(12,2) unsigned not null,
StartDate date not null,
EndDate date,
RoomType_RoomTypeID smallint(10) unsigned not null,
primary key (RoomRateID),
foreign key (RoomType_RoomTypeID)
references RoomType (RoomTypeID)
);
create table Room (
RoomID int(10) unsigned not null auto_increment,
RoomNumber smallint(10) unsigned not null,
Floor smallint(10) not null,
OccupancyLimit smallint(10) unsigned not null,
RoomType_RoomTypeID smallint(10) unsigned not null,
Hotel_HotelID mediumint(10) unsigned not null,
primary key (RoomID),
foreign key (RoomType_RoomTypeID)
references RoomType (RoomTypeID),
foreign key (Hotel_HotelID)
references Hotel (HotelID)
);
create table Amenity (
AmenityID smallint(10) unsigned not null auto_increment,
AmenityName varchar(45),
primary key (AmenityID)
);
create table RoomAmenity (
Room_RoomID int(10) unsigned not null,
Amenity_AmenityID smallint(10) unsigned not null,
AmenityQuantity tinyint(5) unsigned not null,
primary key (Room_RoomID, Amenity_AmenityID),
foreign key (Room_RoomID)
references Room (RoomID),
foreign key (Amenity_AmenityID)
references Amenity (AmenityID)
);
create table AmenityRate (
AmenityRateID bigint(20) unsigned not null auto_increment,
Rate decimal(12,2) unsigned not null,
StartDate date not null,
EndDate date,
Amenity_AmenityID smallint(10) unsigned not null,
primary key (AmenityRateID),
foreign key (Amenity_AmenityID)
references Amenity (AmenityID)
);
create table Guest (
GuestID bigint(20) unsigned not null auto_increment,
FirstName varchar(45) not null,
LastName varchar(45) not null,
BirthDate date,
Employee_EmployeeID bigint(20) unsigned,
primary key (GuestID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID)
);
create table EmailType (
EmailTypeID tinyint(10) unsigned not null auto_increment,
Type varchar(30) not null,
primary key (EmailTypeID)
);
create table Email (
EmailID bigint(20) unsigned not null auto_increment,
EmailAddress varchar(100) not null,
EmailType_EmailTypeID tinyint(10) unsigned,
Guest_GuestID bigint(20) unsigned,
Hotel_HotelID mediumint(10) unsigned,
Employee_EmployeeID bigint(20) unsigned,
primary key (EmailID),
foreign key (Guest_GuestID)
references Guest (GuestID),
foreign key (Hotel_HotelID)
references Hotel (HotelID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID),
foreign key (EmailType_EmailTypeID)
references EmailType (EmailTypeID)
);
create table PhoneType (
PhoneTypeID tinyint(10) unsigned not null auto_increment,
Type varchar(30) not null,
primary key (PhoneTypeID)
);
create table Phone (
PhoneID bigint(20) unsigned not null auto_increment,
PhoneType_PhoneTypeID tinyint(10) unsigned,
PhoneNumber varchar(20) not null,
Guest_GuestID bigint(20) unsigned,
Hotel_HotelID mediumint(10) unsigned,
Employee_EmployeeID bigint(20) unsigned,
primary key (PhoneID),
foreign key (Guest_GuestID)
references Guest (GuestID),
foreign key (Hotel_HotelID)
references Hotel (HotelID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID),
foreign key (PhoneType_PhoneTypeID)
references PhoneType (PhoneTypeID)
);
create table CityZip (
CityZipID mediumint(20) unsigned not null auto_increment,
City varchar(45) not null,
State char(2) not null,
Zipcode varchar(10) not null,
primary key (City, State, Zipcode),
unique key (CityZipID)
);
create table AddressType (
AddressTypeID tinyint(10) unsigned not null auto_increment,
Type varchar(30) not null,
primary key (AddressTypeID)
);
create table Address (
AddressID bigint(20) unsigned not null auto_increment,
AddressType_AddressTypeID tinyint(10) unsigned,
StreetAddress varchar(45) not null,
CityZip_CityZipID mediumint(20) unsigned not null,
Guest_GuestID bigint(20) unsigned,
Hotel_HotelID mediumint(10) unsigned,
Employee_EmployeeID bigint(20) unsigned,
primary key (AddressID),
foreign key (Guest_GuestID)
references Guest (GuestID),
foreign key (Hotel_HotelID)
references Hotel (HotelID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID),
foreign key (CityZip_CityZipID)
references CityZip (CityZipID),
foreign key (AddressType_AddressTypeID)
references AddressType (AddressTypeID)
);
create table Reservation (
ReservationID bigint(20) unsigned not null auto_increment,
Guest_GuestID bigint(20) unsigned not null,
TotalBeforeTax decimal(12,2) unsigned,
Tax decimal(12,2) unsigned,
primary key (ReservationID),
foreign key (Guest_GuestID)
references Guest (GuestID)
);
create table RoomReservation (
RoomReservationID bigint(20) unsigned not null auto_increment,
Room_RoomID int(10) unsigned not null,
Reservation_ReservationID bigint(20) unsigned not null,
StartDate date not null,
EndDate date,
primary key (RoomReservationID), -- this bridge table uses a single primary key for simplicity since it is used as a foreign key in various tables
foreign key (Room_RoomID)
references Room (RoomID),
foreign key (Reservation_ReservationID)
references Reservation (ReservationID)
);
create table RoomReservationGuest (
Guest_GuestID bigint(20) unsigned not null,
RoomReservation_RoomReservationID bigint(20) unsigned not null,
primary key (Guest_GuestID, RoomReservation_RoomReservationID),
foreign key (Guest_GuestID)
references Guest (GuestID),
foreign key (RoomReservation_RoomReservationID)
references RoomReservation (RoomReservationID)
);
create table AddOn (
AddOnID smallint(20) unsigned not null auto_increment,
AddOnType varchar(30) not null,
primary key (AddOnID)
);
create table AddOnRate (
AddOnRateID bigint(20) unsigned not null auto_increment,
Rate decimal(12,2) unsigned not null,
StartDate date not null,
EndDate date,
AddOn_AddOnID smallint(20) unsigned not null,
primary key (AddOnRateID),
foreign key (AddOn_AddOnID)
references AddOn (AddOnID)
);
create table RoomReservationAddOn (
RoomReservationAddOnID bigint(20) unsigned not null auto_increment, -- this bridge table does not have a composite key because it allows multiple add-ons of the same type for the same room
RoomReservation_RoomReservationID bigint(20) unsigned not null,
AddOn_AddOnID smallint(20) unsigned not null,
primary key (RoomReservationAddOnID),
foreign key (RoomReservation_RoomReservationID)
references RoomReservation (RoomReservationID),
foreign key (AddOn_AddOnID)
references AddOn (AddOnID)
);
create table Promo (
PromoID bigint(20) unsigned not null auto_increment,
PromoCode varchar(20) not null,
Description varchar(45) not null,
DollarDiscount smallint(10) unsigned,
PercentDiscount tinyint(10) unsigned,
StartDate date not null,
EndDate date,
RoomType_RoomTypeID smallint(10) unsigned,
Amenity_AmenityID smallint(10) unsigned,
AddOn_AddOnID smallint(20) unsigned,
primary key (PromoID),
foreign key (RoomType_RoomTypeID)
references RoomType (RoomTypeID),
foreign key (Amenity_AmenityID)
references Amenity (AmenityID),
foreign key (AddOn_AddOnID)
references AddOn (AddOnID)
);
create table ReservationDetail (
ReservationDetailID bigint(20) unsigned not null auto_increment,
Reservation_ReservationID bigint(20) unsigned not null,
RoomReservation_RoomReservationID bigint(20) unsigned not null,
RoomReservationAddOn_RoomReservationAddOnID bigint(20) unsigned,
Description varchar(45),
Rate decimal(12,2) unsigned not null,
primary key (ReservationDetailID),
foreign key (Reservation_ReservationID)
references Reservation (ReservationID),
foreign key (RoomReservation_RoomReservationID)
references RoomReservation (RoomReservationID),
foreign key (RoomReservationAddOn_RoomReservationAddOnID)
references RoomReservationAddOn (RoomReservationAddOnID)
);
create table ReservationPromo (
Promo_PromoID bigint(20) unsigned not null,
Reservation_ReservationID bigint(20) unsigned not null,
primary key (Promo_PromoID, Reservation_ReservationID),
foreign key (Promo_PromoID)
references Promo (PromoID),
foreign key (Reservation_ReservationID)
references Reservation (ReservationID)
);
create table Override (
OverrideID bigint(20) unsigned not null auto_increment,
Reservation_ReservationID bigint(20) unsigned not null,
Amount mediumint(15) not null,
Reason varchar(100) not null,
Employee_EmployeeID bigint(20) unsigned not null,
primary key (OverrideID),
foreign key (Reservation_ReservationID)
references Reservation (ReservationID),
foreign key (Employee_EmployeeID)
references Employee (EmployeeID)
);
-- TRIGGERS THAT REQUIRE A BILL DETAIL TO RELATE TO EITHER A ROOM OR AN ADD ON
DELIMITER //
create trigger ReservationDetailInsertTrigger1
before insert on ReservationDetail
for each row
begin
IF (new.RoomReservation_RoomReservationID is null AND new.RoomReservationAddOn_RoomReservationAddOnID is null) THEN
signal sqlstate '45000'
set message_text = 'RoomReservationID and RoomReservationAddOnID cannot both be null';
end if;
end//
create trigger ReservationDetailUpdateTrigger1
before update on ReservationDetail
for each row
begin
IF (new.RoomReservation_RoomReservationID is null AND new.RoomReservationAddOn_RoomReservationAddOnID is null) THEN
signal sqlstate '45000'
set message_text = 'RoomReservationID and RoomReservationAddOnID cannot both be null';
end if;
end//
create trigger ReservationDetailInsertTrigger2
before insert on ReservationDetail
for each row
begin
IF (new.RoomReservation_RoomReservationID is not null AND new.RoomReservationAddOn_RoomReservationAddOnID is not null) THEN
signal sqlstate '45000'
set message_text = 'RoomReservationID and RoomReservationAddOnID cannot both contain data';
end if;
end//
create trigger ReservationDetailUpdateTrigger2
before update on ReservationDetail
for each row
begin
IF (new.RoomReservation_RoomReservationID is not null AND new.RoomReservationAddOn_RoomReservationAddOnID is not null) THEN
signal sqlstate '45000'
set message_text = 'RoomReservationID and RoomReservationAddOnID cannot both contain data';
end if;
end//
DELIMITER ;