-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDL_Script.sql
More file actions
520 lines (453 loc) · 15.3 KB
/
DDL_Script.sql
File metadata and controls
520 lines (453 loc) · 15.3 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS CookingContest;
CREATE SCHEMA CookingContest;
USE CookingContest;
CREATE TABLE Images (
id INT PRIMARY KEY AUTO_INCREMENT,
image LONGBLOB NOT NULL,
descr TEXT
);
CREATE TABLE Cuisines (
id INT PRIMARY KEY AUTO_INCREMENT,
country_name VARCHAR(45) NOT NULL,
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE Recipes (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
short_desc TEXT,
recipe_type ENUM("pastry","gastronomy"), /* Only two types possible */
difficulty_level TINYINT NOT NULL,
/* Every recipe can have up to 3 tips.*/
TIP1 TINYTEXT,
TIP2 TINYTEXT,
TIP3 TINYTEXT,
/* Consraint for the difficulty level:
1 - very easy , 2 - easy, 3 - medium, 4 - hard, 5 - very hard
*/
CONSTRAINT chk_difficulty_level CHECK (difficulty_level BETWEEN 1 AND 5),
/*Times need to be positive!*/
prep_time TIME(0) NOT NULL,
CONSTRAINT chk_prep_time CHECK (prep_time >= '00:00:00'),
cook_time TIME(0) NOT NULL,
CONSTRAINT chk_cook_time CHECK (cook_time >= '00:00:00'),
total_time TIME(0) AS (ADDTIME(prep_time, cook_time)) NOT NULL,
cuisine_id INT NOT NULL,
image_id INT,
FOREIGN KEY (cuisine_id) REFERENCES Cuisines(id),
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE Chefs (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
surname VARCHAR(255) NOT NULL,
/* VARCHAR was chosen to accomodate foriegn numbers 0049 ...*/
phone_number VARCHAR(15),
CONSTRAINT chk_phone_number CHECK (phone_number REGEXP '^[0-9]{0,15}$'),
birth_date DATE NOT NULL,
/* You must have been born and not be dead to participate. */
/* CONSTRAINT chk_birth_date CHECK (birth_date <= CURDATE() AND birth_date >= '1900-01-01'), Make this as trigger. */
/* Age should be returned at the time of query based on birth date. */
work_experience TINYINT,
prof_certification TINYINT NOT NULL,
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id),
CONSTRAINT chk_work_experience CHECK (work_experience >= 0),
CONSTRAINT chk_prof_certification CHECK (prof_certification BETWEEN 0 AND 5 AND NOT NULL)
);
/* Function that takes the number of prof_certification and returns the corresponding title */
DELIMITER //
CREATE FUNCTION get_title(prof_certification INT) RETURNS VARCHAR(45)
DETERMINISTIC
BEGIN
DECLARE title VARCHAR(45);
CASE prof_certification
WHEN 0 THEN SET title = 'None';
WHEN 1 THEN SET title = 'C Cook';
WHEN 2 THEN SET title = 'B Cook';
WHEN 3 THEN SET title = 'A Cook';
WHEN 4 THEN SET title = 'Sous Chef';
WHEN 5 THEN SET title = 'Chef';
END CASE;
RETURN title;
END //
DELIMITER ;
DELIMITER //
CREATE FUNCTION age(birth_date DATE) RETURNS INT
DETERMINISTIC
BEGIN
DECLARE age INT;
SET age = YEAR(CURDATE()) - YEAR(birth_date);
RETURN age;
END //
DELIMITER ;
CREATE TABLE Episodes (
id INT PRIMARY KEY AUTO_INCREMENT,
episode_number INT NOT NULL,
CONSTRAINT chk_episode_number CHECK (episode_number > 0),
/*The winner should be dynamically calculated based on the ratings and other criteria.
Remember to implement the logic in the application.
*/
winner_id INT,
year_played YEAR NOT NULL,
image_id INT,
CONSTRAINT CHECK (year_played >= 2015),
CONSTRAINT no_two_same_episodes UNIQUE (year_played,episode_number),
FOREIGN KEY (winner_id) REFERENCES Chefs(id),
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE Themes (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
descr TEXT NOT NULL,
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE FoodGroups (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
descr TEXT NOT NULL,
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE Ingredients (
id INT PRIMARY KEY AUTO_INCREMENT,
food_group_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
calories_per_100_units INT NOT NULL,
CONSTRAINT chk_calories CHECK (calories_per_100_units >= 0),
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id),
FOREIGN KEY (food_group_id) REFERENCES FoodGroups(id)
);
CREATE TABLE Kitchenware (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
usage_desc TEXT NOT NULL,
image_id INT,
FOREIGN KEY (image_id) REFERENCES Images(id)
);
CREATE TABLE Meals (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45) NOT NULL
);
/* Creating a stored procedure to calculate the calories of a recipe */
DELIMITER //
CREATE FUNCTION calculate_calories(recipe_id INT) RETURNS INT
DETERMINISTIC
BEGIN
/* The function calculates the total calories of a recipe
by summing the calories of each ingredient multiplied
by the quantity of the ingredient in the recipe */
DECLARE total_calories INT;
SELECT SUM(Ingredients.calories_per_100_units * requires.num_units) INTO total_calories
FROM requires
JOIN Ingredients ON requires.ingredient_id = Ingredients.id
WHERE requires.recipe_id = recipe_id;
RETURN total_calories;
END //
DELIMITER ;
CREATE TABLE DietaryInfo (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
fat_content INT NOT NULL,
protein_content INT NOT NULL,
hydrocarbon_content INT NOT NULL,
calories INT,
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
CONSTRAINT chk_fat_content CHECK (fat_content >= 0),
CONSTRAINT chk_protein_content CHECK (protein_content >= 0),
CONSTRAINT chk_hydrocarbon_content CHECK (hydrocarbon_content >= 0)
);
CREATE TABLE Steps (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT,
step_number TINYINT NOT NULL,
CONSTRAINT chk_step_number CHECK (step_number > 0),
CHECK (step_number > 0),
step_desc TEXT NOT NULL,
/* The combination of recipe_id and step_number must be unique
Obviously one recipe can't have the same step two times. */
CONSTRAINT chk_step_number UNIQUE (recipe_id, step_number),
FOREIGN KEY (recipe_id) REFERENCES Recipes(id)
);
CREATE TABLE Quantities (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
quantity INT NOT NULL,
CONSTRAINT pos_quantity CHECK (quantity > 0),
FOREIGN KEY (recipe_id) REFERENCES Recipes(id)
);
CREATE TABLE Tags (
id INT PRIMARY KEY AUTO_INCREMENT,
/* We expect tags to be short*/
tag_name VARCHAR(45) NOT NULL UNIQUE
);
CREATE TABLE chef_recipes (
id INT PRIMARY KEY AUTO_INCREMENT,
chef_id INT NOT NULL,
recipe_id INT NOT NULL,
CONSTRAINT chk_unique_pair UNIQUE (chef_id, recipe_id),
FOREIGN KEY (chef_id) REFERENCES Chefs(id),
FOREIGN KEY (recipe_id) REFERENCES Recipes(id)
);
CREATE TABLE episode_cuisines (
id INT PRIMARY KEY AUTO_INCREMENT,
episode_id INT NOT NULL,
cuisine_id INT NOT NULL,
CONSTRAINT cuisine_chosen_once UNIQUE (episode_id, cuisine_id),
FOREIGN KEY (episode_id) REFERENCES Episodes(id),
FOREIGN KEY (cuisine_id) REFERENCES Cuisines(id)
);
CREATE TABLE recipe_theme (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
theme_id INT NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
FOREIGN KEY (theme_id) REFERENCES Themes(id)
);
CREATE TABLE recipe_meal (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
meal_id INT NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
FOREIGN KEY (meal_id) REFERENCES Meals(id)
);
CREATE TABLE recipe_tags (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
tag_id INT NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
FOREIGN KEY (tag_id) REFERENCES Tags(id)
);
CREATE TABLE requires (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
ingredient_id INT NOT NULL,
quantity VARCHAR(255) NOT NULL,
num_units INT NOT NULL,
main_ingredient BOOLEAN,
ingr_type VARCHAR(45),
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
FOREIGN KEY (ingredient_id) REFERENCES Ingredients(id)
);
CREATE TABLE kitchenware_for_recipe (
id INT PRIMARY KEY AUTO_INCREMENT,
recipe_id INT NOT NULL,
kitchenware_id INT NOT NULL,
FOREIGN KEY (recipe_id) REFERENCES Recipes(id),
FOREIGN KEY (kitchenware_id) REFERENCES Kitchenware(id)
);
CREATE TABLE specialises_in (
id INT PRIMARY KEY AUTO_INCREMENT,
chef_id INT NOT NULL,
cuisine_id INT NOT NULL,
/* A chef can't specialise in the same cuisine twice */
CONSTRAINT chk_specialisation UNIQUE (chef_id, cuisine_id),
FOREIGN KEY (chef_id) REFERENCES Chefs(id),
FOREIGN KEY (cuisine_id) REFERENCES Cuisines(id)
);
CREATE TABLE rates (
id INT PRIMARY KEY AUTO_INCREMENT,
episode_id INT NOT NULL,
judge_id INT NOT NULL,
contestant_id INT NOT NULL,
score TINYINT NOT NULL,
/* Score must be between 1 and 5 */
CONSTRAINT chk_score CHECK (score BETWEEN 1 AND 5),
/* A judge can't rate the same contestant twice */
CONSTRAINT chk_unique_rating UNIQUE (episode_id,judge_id, contestant_id),
FOREIGN KEY (episode_id) REFERENCES Episodes(id),
FOREIGN KEY (judge_id) REFERENCES Chefs(id),
FOREIGN KEY (contestant_id) REFERENCES Chefs(id)
);
CREATE TABLE chefs_recipes_episode (
id INT PRIMARY KEY AUTO_INCREMENT,
episode_id INT NOT NULL,
chef_id INT NOT NULL,
recipe_id INT NOT NULL,
/* The triplet (episode_id, chef_id, recipe_id) must be unique */
CONSTRAINT chk_unique_participation UNIQUE (episode_id, chef_id, recipe_id),
FOREIGN KEY (episode_id) REFERENCES Episodes(id),
FOREIGN KEY (chef_id) REFERENCES Chefs(id),
FOREIGN KEY (recipe_id) REFERENCES Recipes(id)
);
CREATE TABLE is_judge (
id INT PRIMARY KEY AUTO_INCREMENT,
judge_id INT NOT NULL,
episode_id INT NOT NULL,
/* A chef can't be a judge in the same episode twice */
CONSTRAINT chk_judge UNIQUE (judge_id, episode_id),
FOREIGN KEY (judge_id) REFERENCES Chefs(id),
FOREIGN KEY (episode_id) REFERENCES Episodes(id)
);
DELIMITER //
CREATE TRIGGER `CalculateCalories` BEFORE INSERT ON `DietaryInfo`
FOR EACH ROW
BEGIN
SET NEW.calories = calculate_calories(NEW.recipe_id);
END //
CREATE TRIGGER `ReCalculateCalories` BEFORE UPDATE ON `DietaryInfo`
FOR EACH ROW
BEGIN
SET NEW.calories = calculate_calories(NEW.recipe_id);
END //
/* Trigger to set the winner of an episode based on the ratings */
CREATE TRIGGER `SetEpisodeWinner` AFTER INSERT ON `rates`
FOR EACH ROW
BEGIN
DECLARE winner_id INT;
SELECT contestant_id INTO winner_id
FROM rates JOIN chefs
WHERE episode_id = NEW.episode_id AND chefs.id = contestant_id
GROUP BY contestant_id
ORDER BY SUM(score) DESC, prof_certification DESC /* If scores are equal, the chef with the highest certification wins */
LIMIT 1;
UPDATE Episodes
SET winner_id = winner_id
WHERE id = NEW.episode_id;
END //
CREATE TRIGGER `FixEpisodeWinner` AFTER UPDATE ON `rates`
FOR EACH ROW
BEGIN
DECLARE winner_id INT;
SELECT contestant_id INTO winner_id
FROM rates JOIN chefs
WHERE episode_id = NEW.episode_id AND chefs.id = contestant_id
GROUP BY contestant_id
ORDER BY SUM(score) DESC, prof_certification DESC /* If scores are equal, the chef with the highest certification wins */
LIMIT 1;
UPDATE Episodes
SET winner_id = winner_id
WHERE id = NEW.episode_id;
END //
CREATE TRIGGER `SetEpisodeCuisine` AFTER INSERT ON `chefs_recipes_episode`
FOR EACH ROW
BEGIN
DECLARE cus_id INT;
SELECT cuisine_id INTO cus_id
FROM chef_recipes
JOIN Recipes
ON chef_recipes.recipe_id = Recipes.id
WHERE chef_recipes.chef_id = NEW.chef_id
AND Recipes.id = NEW.recipe_id;
INSERT INTO episode_cuisines (episode_id, cuisine_id)
VALUES (NEW.episode_id, cus_id);
END //
CREATE TRIGGER `CheckParticipation` BEFORE INSERT ON `chefs_recipes_episode`
FOR EACH ROW
BEGIN
DECLARE new_cuisine_id INT;
SELECT cuisine_id INTO new_cuisine_id
FROM Recipes
WHERE id = NEW.recipe_id;
/*Check if a chef has taken part in the 3 previous episodes */
IF (
SELECT COUNT(*) FROM chefs_recipes_episode
WHERE chef_id = NEW.chef_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A chef can only participate in 3 consecutive episodes';
END IF;
/* Check if a recipe has been used in the 3 previous episodes */
IF (
SELECT COUNT(*) FROM chefs_recipes_episode
WHERE recipe_id = NEW.recipe_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A recipe can only be used in 3 consecutive episodes';
END IF;
END //
CREATE TRIGGER `CheckParticipationOnUpdate` BEFORE UPDATE ON `chefs_recipes_episode`
FOR EACH ROW
BEGIN
DECLARE new_cuisine_id INT;
SELECT cuisine_id INTO new_cuisine_id
FROM Recipes
WHERE id = NEW.recipe_id;
IF (
SELECT COUNT(*) FROM chefs_recipes_episode
WHERE chef_id = NEW.chef_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A chef can only participate in 3 consecutive episodes';
END IF;
/* Check if a recipe has been used in the 3 previous episodes */
IF (
SELECT COUNT(*) FROM chefs_recipes_episode
WHERE recipe_id = NEW.recipe_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A recipe can only be used in 3 consecutive episodes';
END IF;
END //
CREATE TRIGGER `CheckJudgeParticipation` BEFORE INSERT ON `is_judge`
FOR EACH ROW
BEGIN
/* Check if a judge has taken part as judge in the 3 previous episodes */
IF (
SELECT COUNT(*) FROM is_judge
WHERE judge_id = NEW.judge_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A judge can only participate in 3 consecutive episodes';
END IF;
END //
CREATE TRIGGER `CheckJudgeParticipationOnUpdate` BEFORE UPDATE ON `is_judge`
FOR EACH ROW
BEGIN
/* Check if a judge has taken part as judge in the 3 previous episodes */
IF (
SELECT COUNT(*) FROM is_judge
WHERE judge_id = NEW.judge_id
AND episode_id IN
(
new.episode_id - 1,
new.episode_id - 2,
new.episode_id - 3,
new.episode_id - 4
)
) > 3 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'A judge can only participate in 3 consecutive episodes';
END IF;
END //
DELIMITER ;