-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_tables.sql
334 lines (290 loc) · 9.15 KB
/
create_tables.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
-- Création de la table users
CREATE TABLE "users" (
"id" int NOT NULL,
"pseudo" varchar2(25) NOT NULL,
"mail" varchar2(255) NOT NULL,
"password" varchar2(255) NOT NULL,
"display_name" varchar2(255),
"localisation" varchar2(255),
CONSTRAINT USERS_PK PRIMARY KEY ("id"),
CONSTRAINT USERS_U UNIQUE ("pseudo", "mail"),
CONSTRAINT USERS_MAIL_CK CHECK ("mail" LIKE '%_@_%')
);
-- Création de la table recettes
CREATE TABLE "recettes" (
"id" int NOT NULL,
"nom" varchar2(255) NOT NULL,
"description" CLOB,
"auteur" int NOT NULL,
"difficulte" int NOT NULL,
"prix" int NOT NULL,
"nbre_personnes" int NOT NULL,
"duree_totale" int NOT NULL,
"calories" int NOT NULL,
"lipides" decimal(10, 5) NOT NULL,
"glucides" decimal(10, 5) NOT NULL,
"glucides_dont_sucres" decimal(10, 5) NOT NULL,
"protides" decimal(10, 5) NOT NULL,
CONSTRAINT RECETTES_PK PRIMARY KEY ("id"),
CONSTRAINT RECETTES_DIFF_CK CHECK ("difficulte" BETWEEN 1 AND 5),
CONSTRAINT RECETTES_PRIX_CK CHECK ("prix" BETWEEN 1 AND 5),
CONSTRAINT RECETTES_NBPERS_CK CHECK ("nbre_personnes" > 0),
CONSTRAINT RECETTES_DUREE_CK CHECK ("duree_totale" > 0),
CONSTRAINT RECETTES_CAL_CK CHECK ("calories" >= 0),
CONSTRAINT RECETTES_LIP_CK CHECK ("lipides" >= 0),
CONSTRAINT RECETTES_GLU_CK CHECK ("glucides" >= 0),
CONSTRAINT RECETTES_SUCRES_CK CHECK (
"glucides_dont_sucres" >= 0
AND "glucides_dont_sucres" <= "glucides"
),
CONSTRAINT RECETTES_PRO_CK CHECK ("protides" >= 0)
);
-- Création de la table regime
CREATE TABLE "regime" (
"id" int NOT NULL,
"nom" varchar2(255) NOT NULL,
CONSTRAINT REGIME_PK PRIMARY KEY ("id"),
CONSTRAINT REGIME_U UNIQUE ("nom")
);
-- Création de la table ingredients
CREATE TABLE "ingredients" (
"id" int NOT NULL,
"nom" varchar2(255) NOT NULL,
"auteur" int NOT NULL,
"calories" int NOT NULL,
"lipides" decimal(10, 5) NOT NULL,
"glucides" decimal(10, 5) NOT NULL,
"glucides_dont_sucres" decimal(10, 5) NOT NULL,
"protides" decimal(10, 5) NOT NULL,
"unite" int NOT NULL,
"dispo_par_defaut" number(1) DEFAULT 0 NOT NULL,
"popularite" int NOT NULL,
CONSTRAINT INGREDIENTS_PK PRIMARY KEY ("id"),
CONSTRAINT INGREDIENTS_U UNIQUE ("nom"),
CONSTRAINT INGR_CAL_CK CHECK ("calories" >= 0),
CONSTRAINT INGR_LIP_CK CHECK ("lipides" >= 0),
CONSTRAINT INGR_GLU_CK CHECK ("glucides" >= 0),
CONSTRAINT INGR_SUCRES_CK CHECK (
"glucides_dont_sucres" >= 0
AND "glucides_dont_sucres" <= "glucides"
),
CONSTRAINT INGR_PRO_CK CHECK ("protides" >= 0),
CONSTRAINT INGR_POP_CK CHECK ("popularite" >= 0)
);
-- Création de la table unites
CREATE TABLE "unites" (
"id" int NOT NULL,
"nom" varchar2(100) NOT NULL,
"symbol" varchar2(10),
CONSTRAINT UNITES_PK PRIMARY KEY ("id"),
CONSTRAINT UNITES_U UNIQUE ("nom", "symbol")
);
-- Création de la table categories_ingredients
CREATE TABLE "categories_ingredients" (
"id" int NOT NULL,
"nom" varchar2(120) NOT NULL,
CONSTRAINT CATEGORIES_INGR_PK PRIMARY KEY ("id"),
CONSTRAINT CATEGORIES_INGR_U UNIQUE ("nom")
);
-- Création de la table ingredients_interdits_regime
CREATE TABLE "ingredients_interdits_regime" (
"regime" int NOT NULL,
"ingredient" int NOT NULL,
CONSTRAINT INGR_INTERDITS_REGIME_PK PRIMARY KEY ("regime","ingredient")
);
-- Création de la table ingredients_recette
CREATE TABLE "ingredients_recette" (
"recette" int NOT NULL,
"ingredient" int NOT NULL,
"quantite" decimal(10, 5) NOT NULL,
CONSTRAINT INGREDIENTS_RECETTE_PK PRIMARY KEY ("recette","ingredient"),
CONSTRAINT INGRRECET_QTE_CK CHECK ("quantite" >= 0)
);
-- Création de la table etape_types
CREATE TABLE "etape_types" (
"id" int NOT NULL,
"nom" varchar2(100) NOT NULL,
CONSTRAINT ETAPE_TYPES_PK PRIMARY KEY ("id"),
CONSTRAINT ETAPE_TYPES_U UNIQUE ("nom")
);
-- Création de la table etapes
CREATE TABLE "etapes" (
"id" int NOT NULL,
"recette" int NOT NULL,
"type" int NOT NULL,
"nom" varchar2(255) NOT NULL,
"description" CLOB,
"duree" int NOT NULL,
"ordre" int NOT NULL,
CONSTRAINT ETAPES_PK PRIMARY KEY ("id"),
CONSTRAINT ETAPES_DUR_CK CHECK ("duree" >= 0)
);
-- Création de la table medias
CREATE TABLE "medias" (
"id" int NOT NULL,
"recette" int NOT NULL,
"type" int NOT NULL,
"url" varchar2(255) NOT NULL,
CONSTRAINT MEDIAS_PK PRIMARY KEY ("id")
);
-- Création de la table media_types
CREATE TABLE "media_types" (
"id" int NOT NULL,
"nom" varchar2(100) NOT NULL,
"is_video" number(1) DEFAULT 0 NOT NULL,
CONSTRAINT MEDIA_TYPES_PK PRIMARY KEY ("id"),
CONSTRAINT MEDIA_TYPES_U UNIQUE ("nom")
);
-- Création de la table ingredients_user
CREATE TABLE "ingredients_user" (
"user" int NOT NULL,
"ingredient" int NOT NULL,
"quantite" decimal(10, 5) NOT NULL,
CONSTRAINT INGREDIENTS_USER_PK PRIMARY KEY ("user","ingredient"),
CONSTRAINT INGRUSR_QTE_CK CHECK ("quantite" >= 0)
);
-- Création de la table planning
CREATE TABLE "planning" (
"recette" int NOT NULL,
"user" int NOT NULL,
"at" date NOT NULL,
CONSTRAINT PLANNING_PK PRIMARY KEY ("recette","user", "at")
);
-- Création de la table planning_archive
CREATE TABLE "planning_archive" (
"recette" int NOT NULL,
"user" int NOT NULL,
"at" date NOT NULL,
CONSTRAINT PLANNING_ARCHIVE_PK PRIMARY KEY ("recette","user", "at")
);
-- Création de la table user_achat_ingredients
CREATE TABLE "user_achat_ingredients" (
"user" int NOT NULL,
"ingredient" int NOT NULL,
"quantite" decimal(10, 5) NOT NULL,
"date" date NOT NULL,
CONSTRAINT USER_ACHAT_INGR_PK PRIMARY KEY ("user","ingredient"),
CONSTRAINT USRACHAT_QTE_CK CHECK ("quantite" >= 0)
);
-- Création de la table user_achat_ingredients_archive
CREATE TABLE "user_achat_ingredients_archive" (
"user" int NOT NULL,
"ingredient" int NOT NULL,
"quantite" decimal(10, 5) NOT NULL,
"date" date NOT NULL,
CONSTRAINT USER_ACHAT_INGR_ARCHIVE_PK PRIMARY KEY ("user","ingredient"),
CONSTRAINT USRACHAT_A_QTE_CK CHECK ("quantite" >= 0)
);
-- Création de la table user_regime
CREATE TABLE "user_regime" (
"user" int NOT NULL,
"regime" int NOT NULL,
CONSTRAINT USER_REGIME_PK PRIMARY KEY ("user","regime")
);
-- Création de la table ingredients_categories
CREATE TABLE "ingredients_categories" (
"ingredient" int NOT NULL,
"categorie" int NOT NULL,
CONSTRAINT INGREDIENTS_CATEGORIES_PK PRIMARY KEY ("ingredient","categorie")
);
-- On ajoute les contraintes sur les clés étrangères
ALTER TABLE "recettes"
ADD CONSTRAINT "recettes_fk0"
FOREIGN KEY ("auteur")
REFERENCES "users" ("id");
ALTER TABLE "ingredients"
ADD CONSTRAINT "ingr_fk0"
FOREIGN KEY ("auteur")
REFERENCES "users" ("id");
ALTER TABLE "ingredients"
ADD CONSTRAINT "ingr_fk1"
FOREIGN KEY ("unite")
REFERENCES "unites" ("id");
ALTER TABLE "ingredients_interdits_regime"
ADD CONSTRAINT "ingr_interdits_regime_fk0"
FOREIGN KEY ("regime")
REFERENCES "regime" ("id");
ALTER TABLE "ingredients_interdits_regime"
ADD CONSTRAINT "ingr_interdits_regime_fk1"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "ingredients_recette"
ADD CONSTRAINT "ingr_recette_fk0"
FOREIGN KEY ("recette")
REFERENCES "recettes" ("id");
ALTER TABLE "ingredients_recette"
ADD CONSTRAINT "ingr_recette_fk1"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "etapes"
ADD CONSTRAINT "etapes_fk0"
FOREIGN KEY ("recette")
REFERENCES "recettes" ("id");
ALTER TABLE "etapes"
ADD CONSTRAINT "etapes_fk1"
FOREIGN KEY ("type")
REFERENCES "etape_types" ("id");
ALTER TABLE "medias"
ADD CONSTRAINT "medias_fk0"
FOREIGN KEY ("recette")
REFERENCES "recettes" ("id");
ALTER TABLE "medias"
ADD CONSTRAINT "medias_fk1"
FOREIGN KEY ("type")
REFERENCES "media_types" ("id");
ALTER TABLE "ingredients_user"
ADD CONSTRAINT "ingr_user_fk0"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "ingredients_user"
ADD CONSTRAINT "ingr_user_fk1"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "planning"
ADD CONSTRAINT "planning_fk0"
FOREIGN KEY ("recette")
REFERENCES "recettes" ("id");
ALTER TABLE "planning"
ADD CONSTRAINT "planning_fk1"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "planning_archive"
ADD CONSTRAINT "planning_archive_fk0"
FOREIGN KEY ("recette")
REFERENCES "recettes" ("id");
ALTER TABLE "planning_archive"
ADD CONSTRAINT "planning_archive_fk1"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "user_achat_ingredients"
ADD CONSTRAINT "user_achat_ingr_fk0"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "user_achat_ingredients"
ADD CONSTRAINT "user_achat_ingr_fk1"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "user_achat_ingredients_archive"
ADD CONSTRAINT "user_achat_ingr_archive_fk0"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "user_achat_ingredients_archive"
ADD CONSTRAINT "user_achat_ingr_archive_fk1"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "user_regime"
ADD CONSTRAINT "user_regime_fk0"
FOREIGN KEY ("user")
REFERENCES "users" ("id");
ALTER TABLE "user_regime"
ADD CONSTRAINT "user_regime_fk1"
FOREIGN KEY ("regime")
REFERENCES "regime" ("id");
ALTER TABLE "ingredients_categories"
ADD CONSTRAINT "ingr_categories_fk0"
FOREIGN KEY ("ingredient")
REFERENCES "ingredients" ("id");
ALTER TABLE "ingredients_categories"
ADD CONSTRAINT "ingr_categories_fk1"
FOREIGN KEY ("categorie")
REFERENCES "categories_ingredients" ("id");