-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSEPDatabases.sql
374 lines (343 loc) · 10.6 KB
/
SEPDatabases.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
CREATE SCHEMA viaevents;
CREATE TABLE users(
name VARCHAR(40) NOT NULL,
userid VARCHAR PRIMARY KEY,
password VARCHAR(20),
usertype INT NOT NULL);
CREATE TABLE facilities(
roomnumber VARCHAR(10) PRIMARY KEY,
roomname VARCHAR(40) NOT NULL,
capacity INT NOT NULL);
CREATE TABLE events(
eventid INT PRIMARY KEY ,
eventname VARCHAR(20) NOT NULL,
capacity INT NOT NULL,
eventdescription VARCHAR(300) NOT NULL,
signupdeadline TIMESTAMP,
roomnumber VARCHAR(10) NOT NULL,
FOREIGN KEY(roomnumber) REFERENCES facilities(roomnumber));
CREATE TABLE bookings(
bookingsid INT PRIMARY KEY,
createdby VARCHAR NOT NULL,
eventid INT,
roomnumber VARCHAR,
reservationdate DATE NOT NULL,
hoursfrom TIME NOT NULL,
hoursto TIME NOT NULL,
FOREIGN KEY(roomnumber) REFERENCES facilities(roomnumber),
FOREIGN KEY(eventid) REFERENCES events(eventid),
FOREIGN KEY(createdby) REFERENCES users(userid));
CREATE TABLE subscribe(
userid VARCHAR NOT NULL,
eventid INT NOT NULL,
FOREIGN KEY(eventid) REFERENCES events(eventid),
FOREIGN KEY(userid) REFERENCES users(userid));
-- JUST FOR TESTING
INSERT INTO bookings VALUES
(1, '240124', 2, 'E.201b', '2016-11-10', '08:10:47', '09:11:47');
-- (2, '240309', 3, 'E.201a', '2016-11-10', '10:10:47', '11:11:47');
INSERT INTO users VALUES
('Stefani Ivanova','240124', '1234', 4);
-- ('240309', 'Emilie Michelsen Kuhn', 3, '1234'),
-- ('240190', 'Marton Teperics', 1, '1234'),
-- ('sva','Steffen Vissing Andersen', 2, '1234');
INSERT INTO events VALUES
(1, 'JAVA Workshop', 50, 'For all students who are interested in programming!', NULL, 'E.201b'),
(2, 'JAVABlaa', 50, 'We will held a Java workshop.', NULL, 'E.201a');
INSERT INTO subscribe VALUES
('240124', 2);
SELECT * FROM users;
SELECT * FROM facilities;
SELECT * FROM events;
SELECT * FROM bookings;
-- 1)TRIGGER FOR EVENTS TABLE FOR EVENTID
CREATE SEQUENCE event_sequence START 1 INCREMENT 1;
SELECT nextval('event_sequence');
CREATE OR REPLACE FUNCTION event_id() RETURNS TRIGGER AS $$
BEGIN
NEW.eventid := nextval('event_sequence');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER event_trig BEFORE INSERT ON events
FOR EACH ROW
EXECUTE PROCEDURE event_id();
-- 2)TRIGGER FOR BOOKINGS TABLE FOR BOOKINGID
CREATE SEQUENCE bookings_sequence START 1 INCREMENT 1;
SELECT nextval('bookings_sequence');
CREATE OR REPLACE FUNCTION booking_id() RETURNS TRIGGER AS $$
BEGIN
NEW.bookingsid := nextval('bookings_sequence');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER booking_trig BEFORE INSERT ON bookings
FOR EACH ROW
EXECUTE PROCEDURE booking_id();
-- SELECT *
-- FROM events
-- FULL OUTER JOIN bookings
-- ON events.eventid = bookings.eventid
-- ORDER BY bookings.reservationdate,bookings.hoursfrom;
-- CREATING A VIEW WITH JOIN statements for bookings + events table + order by the reservationdate
CREATE OR REPLACE VIEW firstview AS
SELECT events.eventname, events.capacity, events.eventdescription, events.signupdeadline, events.roomnumber,
bookings.bookingsid, bookings.createdby, bookings.eventid, bookings.reservationdate, bookings.hoursfrom, bookings.hoursto
FROM events
FULL OUTER JOIN bookings
ON events.eventid = bookings.eventid
ORDER BY bookings.reservationdate,bookings.hoursfrom;
SELECT * FROM firstview;
-- CREATING A VIEW WITH JOIN statements for bookings + subscribe + events
CREATE OR REPLACE VIEW secondview AS
SELECT events.eventname, events.capacity, events.eventdescription, events.signupdeadline, events.roomnumber,
bookings.bookingsid, bookings.createdby, bookings.eventid, bookings.reservationdate, bookings.hoursfrom, bookings.hoursto,
subscribe.userid
FROM subscribe
FULL OUTER JOIN bookings
ON subscribe.eventid = bookings.eventid
FULL OUTER JOIN events
ON events.eventid = bookings.eventid
ORDER BY bookings.reservationdate,bookings.hoursfrom;
-- VIEW USERS+BOOKINGS
CREATE OR REPLACE VIEW secondview AS
SELECT events.eventname, events.capacity, events.eventdescription, events.signupdeadline, events.roomnumber,
bookings.bookingsid, bookings.createdby, bookings.eventid, bookings.reservationdate, bookings.hoursfrom, bookings.hoursto,
subscribe.userid
FROM subscribe
FULL OUTER JOIN bookings
ON subscribe.eventid = bookings.eventid
FULL OUTER JOIN events
ON events.eventid = bookings.eventid
ORDER BY bookings.reservationdate,bookings.hoursfrom;
SELECT * FROM secondview;
-- ALL ROOMS IN THE UNIVERSITY
INSERT INTO facilities VALUES
('A.001a', 'Workshop', 25),
('A.001b', 'Workshop', 25),
('A.101a', 'Classroom', 50),
('A.101b', 'Classroom', 50),
('A.102', 'Office', 10),
('A.103', 'Office', 10),
('A.104', 'Office', 10),
('A.105', 'Workshop', 25),
('A.201a', 'Office', 10),
('A.201b', 'Office', 10),
('A.202', 'Computer Room', 50),
('A.203', 'Classroom', 50),
('A.204', 'Classroom', 50),
('A.301a', 'Classroom', 50),
('A.301b', 'Classroom', 50),
('A.302', 'Classroom', 50),
('A.303a', 'Classroom', 50),
('A.303b', 'Classroom', 50),
('B.001', 'Workshop', 25),
('B.002', 'Workshop', 25),
('B.003', 'Workshop', 25),
('B.004', 'Workshop', 25),
('B.101a', 'Classroom', 50),
('B.101b', 'Classroom', 50),
('B.102', 'Classroom', 50),
('B.103', 'Classroom', 50),
('B.104a', 'Classroom', 50),
('B.104b', 'Classroom', 50),
('B.201a', 'Classroom', 50),
('B.201b', 'Classroom', 50),
('B.202', 'Classroom', 50),
('B.203', 'Classroom', 50),
('B.301a', 'Classroom', 50),
('B.301b', 'Classroom', 50),
('B.302', 'Classroom', 50),
('B.303', 'Classroom', 50),
('B.304', 'Classroom', 50),
('C.001', 'Workshop', 25),
('C.003', 'Workshop', 25),
('C.004', 'Workshop', 25),
('C.005', 'Workshop', 25),
('C.101', 'Office', 10),
('C.102', 'Office', 10),
('C.103', 'Office', 10),
('C.104', 'Classroom', 50),
('C.105', 'Classroom', 50),
('C.107', 'Classroom', 50),
('C.201', 'Classroom', 50),
('C.202', 'Classroom', 50),
('C.203', 'Classroom', 50),
('C.204', 'Classroom', 50),
('C.205', 'Classroom', 50),
('C.301', 'Classroom', 50),
('C.302', 'Classroom', 50),
('C.303', 'Classroom', 50),
('C.304', 'Classroom', 50),
('C.305', 'Classroom', 50),
('D.002', 'Workshop', 25),
('D.101', 'Workshop', 25),
('D.201', 'Library', 25),
('D.202', 'Library', 25),
('D.203', 'Library', 25),
('D.301', 'Classroom', 50),
('D.302', 'Classroom', 50),
('D.304', 'Classroom', 50),
('D.305', 'Classroom', 50),
('E.001b', 'Workshop', 25),
('E.002', 'Workshop', 25),
('E.003', 'Workshop', 25),
('E.004', 'Workshop', 25),
('E.005', 'Workshop', 25),
('E.006', 'Workshop', 25),
('E.007', 'Workshop', 25),
('E.101', 'Drosellbo', 10),
('E.101a', 'IT support', 10),
('E.102', 'IT support', 10),
('E.170', 'Guest Canteen', 50),
('E.172', 'Cafe', 30),
('E.201a', 'Classroom', 50),
('E.201b', 'Classroom', 50),
('E.202', 'Office', 10),
('E.203', 'Office', 10),
('E.204', 'Office', 10),
('E.204a', 'Office', 10),
('E.205', 'Office', 10),
('E.301a', 'Classroom', 50),
('E.301b', 'Classroom', 50),
('E.302', 'Classroom', 50),
('E.303', 'Classroom', 50),
('F.001', 'Classroom', 50),
('F.002', 'Classroom', 50),
('F.003a', 'Classroom', 50),
('F.004', 'Classroom', 50),
('F.005', 'Classroom', 50),
('F.006', 'Classroom', 50),
('F.007', 'Classroom', 50),
('F.009', 'Classroom', 50),
('F.010', 'Classroom', 50),
('F.011', 'Classroom', 50),
('F.101a', 'Classroom', 50),
('F.101b', 'Classroom', 50),
('F.102a', 'Classroom', 50),
('F.102b', 'Classroom', 50),
('F.104', 'Classroom', 50),
('F.105a', 'Classroom', 50),
('F.105b', 'Classroom', 50),
('F.106a', 'Classroom', 50),
('F.106b', 'Classroom', 50),
('F.107', 'Classroom', 50),
('F.201a', 'Classroom', 50),
('F.201b', 'Classroom', 50),
('F.202a', 'Classroom', 50),
('F.204', 'Classroom', 50),
('F.205a', 'Classroom', 50),
('F.206a', 'Classroom', 50),
('F.207', 'Classroom', 50),
('F.301a', 'Classroom', 50),
('F.301b', 'Classroom', 50),
('F.302a', 'Office', 10),
('F.302b', 'Classroom', 50),
('F.304', 'Classroom', 50),
('F.305a', 'Classroom', 50),
('F.305b', 'Classroom', 50),
('F.306a', 'Classroom', 50),
('F.306b', 'Classroom', 50),
('F.307', 'Classroom', 50),
('G.001', 'Kitchen', 15),
('G.002', 'Workshop', 25),
('G.003', 'Workshop', 25),
('G.004', 'Workshop', 25),
('G.005', 'Classroom', 50),
('G.006', 'Depot', 1),
('G.020', 'Depot', 1),
('G.021', 'Depot', 1),
('G.022', 'Depot', 1),
('G.023', 'Auditorium', 200),
('G.024', 'Music Room', 10),
('G.025', 'Music Room', 10),
('G.026', 'Depot', 1),
('G.027', 'Depot', 1),
('G.028', 'Group Room', 5),
('G.029', 'Group Room', 5),
('G.030', 'Group Room', 5),
('G.031', 'Group Room', 5),
('G.032', 'Group Room', 5),
('G.033', 'Group Room', 5),
('G.034', 'Group Room', 5),
('G.035', 'Group Room', 5),
('G.036', 'Classroom', 50),
('G.037', 'Classroom', 50),
('G.038', 'Classroom', 50),
('G.039', 'Workshop', 25),
('G.040', 'Workshop', 25),
('G.041', 'Classroom', 50),
('G.042', 'Classroom', 50),
('G.043', 'Depot', 1),
('G.044', 'Depot', 1),
('G.101', 'Office', 10),
('G.102', 'Office', 10),
('G.103', 'Office', 10),
('G.104', 'Office', 10),
('G.105', 'Office', 10),
('G.106', 'Office', 10),
('G.107', 'Office', 10),
('G.108', 'Office', 10),
('G.110', 'Office', 10),
('G.111', 'Office', 10),
('G.112', 'Office', 10),
('G.113', 'Office', 10),
('G.120', 'Office', 10),
('G.121', 'Office', 10),
('G.122', 'Office', 10),
('G.123', 'Office', 10),
('G.124', 'Office', 10),
('G.125', 'Office', 10),
('G.126', 'Office', 10),
('G.127', 'Office', 10),
('G.128', 'Office', 10),
('G.129', 'Office', 10),
('G.130', 'Office', 10),
('G.131', 'Office', 10),
('G.132', 'Office', 10),
('G.133', 'Office', 10),
('G.134', 'Office', 10),
('G.135', 'Office', 10),
('G.136', 'Office', 10),
('G.137', 'Office', 10),
('G.138', 'Office', 10),
('G.201', 'Group Room', 5),
('G.202', 'Office', 10),
('G.203', 'Office', 10),
('G.204', 'Office', 10),
('G.205', 'Office', 10),
('G.206', 'Office', 10),
('G.207', 'Office', 10),
('G.208', 'Office', 10),
('G.209', 'Depot', 1),
('G.210', 'Office', 10),
('G.211', 'Office', 10),
('G.212', 'Office', 10),
('G.213', 'Office', 10),
('G.214', 'Office', 10),
('G.220a', 'Classroom', 50),
('G.220b', 'Classroom', 50),
('G.221a', 'Classroom', 50),
('G.221b', 'Classroom', 50),
('G.222a', 'Classroom', 50),
('G.222b', 'Classroom', 50),
('G.223a', 'Classroom', 50),
('G.301', 'Group Room', 5),
('G.302', 'Group Room', 5),
('G.303', 'Group Room', 5),
('G.304', 'Group Room', 5),
('G.305', 'Group Room', 5),
('G.306', 'Group Room', 5),
('G.307a', 'Classroom', 50),
('G.307b', 'Classroom', 50),
('G.308', 'Group Room', 5),
('G.309', 'Group Room', 5),
('G.310', 'Group Room', 5),
('G.311', 'Classroom', 50),
('G.320a', 'Classroom', 50),
('G.321a', 'Classroom', 50),
('G.321b', 'Classroom', 50),
('G.322a', 'Classroom', 50),
('G.322b', 'Classroom', 50),
('G.323', 'Classroom', 50),
('G.324', 'Classroom', 50);