-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotel.sql
300 lines (244 loc) · 8.62 KB
/
hotel.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
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Czas generowania: 04 Cze 2016, 21:35
-- Wersja serwera: 10.1.13-MariaDB
-- Wersja PHP: 5.6.20
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Baza danych: `hotel`
--
DELIMITER $$
--
-- Procedury
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `addklient` (IN `name` VARCHAR(30), IN `address` VARCHAR(60)) begin
set @newcid = (select if((select max(x.nr) from (select cid as nr from customer)x) is null, '0', (select max(x.nr) from (select cid as nr from customer)x) + 1));
insert into customer values(@newcid, name, address);
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `addroom` (IN `branch` VARCHAR(30), IN `capacity` INT(2), IN `beds` INT(2), IN `balcony` BINARY(1), IN `typename` TEXT, IN `price` DOUBLE(6,2)) begin
set @newrid = (select if((select max(x.nr) from (select rid as nr from room)x) is null, '0', (select max(x.nr) from (select rid as nr from room)x) + 1));
set @newbid := 0;
insert into room values(@newrid, @newbid, capacity, beds, balcony, typename, price);
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `cancelreservation` (`resid` INT(6)) begin
delete from reservation where reservation.resid = resid;
delete from room_reservation where room_reservation.resid =resid;
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `deletecustomer` (`cid` INT(6)) begin
delete from customer where customer.cid = cid;
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `deleteroom` (`rid` INT(6)) begin
delete from room where room.rid = rid;
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `fire` (`sid` INT(6)) begin
delete from staff where staff.sid = sid;
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `hire` (IN `branch` INT(6), IN `e_name` VARCHAR(30), IN `e_address` VARCHAR(60), IN `position` TEXT) begin
set @newsid = (select if((select max(x.nr) from (select sid as nr from staff)x) is null, '0', (select max(x.nr) from (select sid as nr from staff)x) + 1));
set @newbid = (select bid from branch where name = branch);
insert into staff values(@newsid, @newbid, e_name, e_address, position);
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `isfree` (`rid` INT(6), `startd` DATE, `endd` DATE) begin
select if(
(
select count(x.y) from
(
SELECT r.resID as y FROM room_reservation rr join reservation r on rr.resID = r.resID where rr.rid = rid and
(
(
startd >= r.startdate and startd <= r.enddate
)
or
(
endd >= r.startdate and endd <= r.enddate
)
or
(
startd <= r.startdate and endd >= r.enddate
)
)
)x
order by x.y
)
> 0, 'false', 'true'
) as 'if';
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `reserve` (IN `cid` INT(6), IN `rid` INT(6), IN `startdate` DATE, IN `enddate` DATE) begin
set @newresid = (select if((select max(x.nr) from (select resid as nr from reservation)x) is null, '0', (select max(x.nr) from (select resid as nr from reservation)x) + 1));
set @duration = (select datediff(enddate, startdate));
set @bill = (select price from room where room.rid = rid) * @duration;
insert into reservation values(@newresid, cid, startdate, enddate, @bill);
insert into room_reservation values (rid, @newresid);
end$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `showbill` (IN `resid` INT(6)) begin
select bill as bill from reservation r where r.resid = resid;
end$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `branch`
--
CREATE TABLE `branch` (
`bID` int(6) NOT NULL,
`name` varchar(30) NOT NULL,
`address` varchar(60) NOT NULL,
`capacity` int(6) NOT NULL,
`noofstaff` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Zrzut danych tabeli `branch`
--
INSERT INTO `branch` (`bID`, `name`, `address`, `capacity`, `noofstaff`) VALUES
(0, 'Hotel Qtaz', 'Zadupie', 100, 50);
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `customer`
--
CREATE TABLE `customer` (
`cID` int(6) NOT NULL,
`name` varchar(30) NOT NULL,
`address` varchar(60) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Zrzut danych tabeli `customer`
--
INSERT INTO `customer` (`cID`, `name`, `address`) VALUES
(0, 'Maciaszek Karol', 'Debowa 890');
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `reservation`
--
CREATE TABLE `reservation` (
`resID` int(6) NOT NULL,
`cID` int(6) NOT NULL,
`startDate` date NOT NULL,
`enddate` date NOT NULL,
`bill` double(6,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Zrzut danych tabeli `reservation`
--
INSERT INTO `reservation` (`resID`, `cID`, `startDate`, `enddate`, `bill`) VALUES
(0, 0, '2016-06-06', '2016-06-10', 200.00),
(1, 0, '2016-06-08', '2016-07-10', 1600.00),
(2, 0, '2016-07-08', '2016-08-10', 1650.00),
(3, 0, '2016-09-08', '2016-10-10', 1600.00),
(4, 0, '2016-11-08', '2016-12-10', 1600.00);
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `room`
--
CREATE TABLE `room` (
`rID` int(6) NOT NULL,
`bID` int(6) NOT NULL,
`capacity` int(2) DEFAULT NULL,
`beds` int(2) NOT NULL,
`balcony` tinyint(1) DEFAULT NULL,
`type` enum('apartment','royal','normal') NOT NULL,
`price` double(6,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Zrzut danych tabeli `room`
--
INSERT INTO `room` (`rID`, `bID`, `capacity`, `beds`, `balcony`, `type`, `price`) VALUES
(0, 0, 5, 5, 1, 'royal', 50.00);
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `room_reservation`
--
CREATE TABLE `room_reservation` (
`rID` int(6) NOT NULL,
`resID` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Zrzut danych tabeli `room_reservation`
--
INSERT INTO `room_reservation` (`rID`, `resID`) VALUES
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(0, 4);
-- --------------------------------------------------------
--
-- Struktura tabeli dla tabeli `staff`
--
CREATE TABLE `staff` (
`sID` int(6) NOT NULL,
`bID` int(6) NOT NULL,
`name` varchar(30) NOT NULL,
`address` varchar(60) NOT NULL,
`status` enum('waiter','maid','cook','reception','porter','provider','manager') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indeksy dla zrzutów tabel
--
--
-- Indexes for table `branch`
--
ALTER TABLE `branch`
ADD PRIMARY KEY (`bID`);
--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
ADD PRIMARY KEY (`cID`);
--
-- Indexes for table `reservation`
--
ALTER TABLE `reservation`
ADD PRIMARY KEY (`resID`),
ADD KEY `cID` (`cID`);
--
-- Indexes for table `room`
--
ALTER TABLE `room`
ADD PRIMARY KEY (`rID`),
ADD KEY `bID` (`bID`);
--
-- Indexes for table `room_reservation`
--
ALTER TABLE `room_reservation`
ADD PRIMARY KEY (`rID`,`resID`),
ADD KEY `resID` (`resID`);
--
-- Indexes for table `staff`
--
ALTER TABLE `staff`
ADD PRIMARY KEY (`sID`),
ADD KEY `bID` (`bID`);
--
-- Ograniczenia dla zrzutów tabel
--
--
-- Ograniczenia dla tabeli `reservation`
--
ALTER TABLE `reservation`
ADD CONSTRAINT `reservation_ibfk_1` FOREIGN KEY (`cID`) REFERENCES `customer` (`cID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ograniczenia dla tabeli `room`
--
ALTER TABLE `room`
ADD CONSTRAINT `room_ibfk_1` FOREIGN KEY (`bID`) REFERENCES `branch` (`bID`);
--
-- Ograniczenia dla tabeli `room_reservation`
--
ALTER TABLE `room_reservation`
ADD CONSTRAINT `room_reservation_ibfk_3` FOREIGN KEY (`rID`) REFERENCES `room` (`rID`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `room_reservation_ibfk_4` FOREIGN KEY (`resID`) REFERENCES `reservation` (`resID`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Ograniczenia dla tabeli `staff`
--
ALTER TABLE `staff`
ADD CONSTRAINT `staff_ibfk_1` FOREIGN KEY (`bID`) REFERENCES `branch` (`bID`);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;