-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_query_do.sql
240 lines (206 loc) · 8.02 KB
/
sql_query_do.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
-- Nom du fichier : sql_query_do.sql
-- Ce fichier permet la création de la base
-- de données ERM_DB, d'un utilisateur ainsi
-- que ses tables, ses vues et ses triggers
-- Auteur : Maxence Guindon
-- Équipe : Carlens Belony et Maxence Guindon
CREATE DATABASE IF NOT EXISTS ERM_DB
CHARACTER SET utf8
COLLATE utf8_general_ci;
USE ERM_DB;
-- Création de l'utilisateur
CREATE USER 'erm_user'@'localhost' IDENTIFIED by 'EscapeRoomManager';
GRANT ALl on erm_db.* TO 'erm_user'@'localhost';
-- Supression des tables
DROP TABLE IF EXISTS emp_cent;
DROP TABLE IF EXISTS hor_salle;
DROP TABLE IF EXISTS reservations;
DROP TABLE IF EXISTS employes;
DROP TABLE IF EXISTS typeclient;
DROP TABLE IF EXISTS rabais;
DROP TABLE IF EXISTS salles;
DROP TABLE IF EXISTS horaires;
DROP TABLE IF EXISTS centres;
DROP TABLE IF EXISTS compagnies;
-- suppression des vues
DROP VIEW IF EXISTS view_salles_compagnie;
DROP VIEW IF EXISTS view_employes_lieu;
DROP VIEW IF EXISTS view_salle_horaire;
DROP VIEW IF EXISTS view_reservation_compagnie;
-- suppression des triggers
DROP TRIGGER IF EXISTS centre_root;
DROP TRIGGER IF EXISTS employe_root;
-- Création des tables
CREATE TABLE compagnies(
id INT NOT NULL AUTO_INCREMENT,
nom VARCHAR(100) NOT NULL,
info_paiement TEXT NOT NULL,
courriel VARCHAR(50) NOT NULL,
mot_de_passe TEXT NOT NULL,
PRIMARY KEY pk_compagnie(id),
UNIQUE (courriel),
UNIQUE (nom)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE typeclient(
id INT NOT NULL AUTO_INCREMENT,
categorie VARCHAR(50) NOT NULL,
prix FLOAT NOT NULL,
compagnie INT NOT NULL,
PRIMARY KEY pk_tc(id),
FOREIGN KEY fk_tc_compfk_tc_comp(compagnie) REFERENCES compagnies(id) ON DELETE CASCADE,
UNIQUE (categorie)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE rabais(
id INT NOT NULL AUTO_INCREMENT,
nom TEXT NOT NULL,
pourcentage FLOAT NOT NULL,
compagnie INT NOT NULL,
isActive INT NOT NULL DEFAULT 0,
date_fin TEXT NOT NULL,
PRIMARY KEY pk_rab(id),
FOREIGN KEY fk_rab_comp(compagnie) REFERENCES compagnies(id) ON DELETE CASCADE,
CONSTRAINT const_bool CHECK(isActive=0 OR isActive = 1)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE horaires(
id INT NOT NULL AUTO_INCREMENT,
id_salle INT NOT NULL,
heure_debut VARCHAR(10) NOT NULL,
heure_fin VARCHAR(10) NOT NULL,
PRIMARY KEY pk_horaire(id),
FOREIGN KEY fk_salle(salles) REFERENCES salles(id) ON DELETE CASCADE,
UNIQUE(id_salle, heure_debut, heure_fin)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE centres(
id INT NOT NULL AUTO_INCREMENT,
nom TEXT NOT NULL,
compagnie INT NOT NULL,
adresse TEXT NOT NULL,
ville TEXT NOT NULL,
pays TEXT NOT NULL,
code_postal TEXT NOT NULL,
PRIMARY KEY pk_centre(id),
FOREIGN KEY fk_c_comp(compagnie) REFERENCES compagnies(id) ON DELETE CASCADE
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE salles(
id INT NOT NULL AUTO_INCREMENT,
Nom TEXT NOT NULL,
description TEXT NOT NULL,
centre INT NOT NULL,
nb_max_joueur INT NOT NULL,
prix_unitaire FLOAT NOT NULL,
privee INT NOT NULL,
PRIMARY KEY pk_salle(id),
FOREIGN KEY fk_s_centre(centre) REFERENCES centres(id) ON DELETE CASCADE
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE hor_salle(
id_horaire INT NOT NULL,
id_salle INT NOT NULL,
PRIMARY KEY(id_horaire, id_salle),
FOREIGN KEY fk_lien_horaire(id_horaire) REFERENCES horaires(id) ON DELETE CASCADE,
FOREIGN KEY fk_lien_salle(id_salle) REFERENCES salles(id) ON DELETE CASCADE
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE employes(
id INT NOT NULL AUTO_INCREMENT,
compagnie INT NOT NULL,
nom TEXT NOT NULL,
prenom TEXT NOT NULL,
salaire FLOAT NULL,
num_telephone TEXT NULL,
niveau_acces INT NOT NULL DEFAULT 1,
courriel VARCHAR(50) NOT NULL,
num_ass INT NULL,
mot_de_passe TEXT NOT NULL,
PRIMARY KEY pk_employe(id),
FOREIGN KEY fk_e_compagnie(compagnie) REFERENCES compagnies(id) ON DELETE CASCADE,
UNIQUE(courriel)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE emp_cent(
id_emp INT NOT NULL,
id_centre INT NOT NULL,
PRIMARY KEY(id_emp, id_centre),
FOREIGN KEY fk_emp(id_emp) REFERENCES employes(id) ON DELETE CASCADE,
FOREIGN KEY fk_cent(id_centre) REFERENCES centres(id) ON DELETE CASCADE
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE reservations(
id INT NOT NULL AUTO_INCREMENT,
nom_client TEXT NOT NULL,
num_telephone TEXT NOT NULL,
statut_reservation INT NOT NULL,
salle INT NOT NULL,
nb_personnes INT NOT NULL,
courriel TEXT NOT NULL,
prix_total FLOAT NOT NULL,
date DATETIME NOT NULL,
PRIMARY KEY pk_resa(id),
FOREIGN KEY fk_r_salle(salle) REFERENCES salles(id) ON DELETE CASCADE,
CONSTRAINT const_statut CHECK(statut_reservation = 0 OR statut_reservation = 1),
UNIQUE(date, salle)
)ENGINE = innoDB CHARACTER SET utf8 COLLATE utf8_general_ci;
-- Création de views
CREATE VIEW view_salles_compagnie AS
SELECT
salles.id AS 'id_salle',
salles.nom AS 'salle',
salles.description AS 'description',
salles.nb_max_joueur AS 'nb_joueur_max',
salles.prix_unitaire AS 'prix',
salles.privee AS "privee",
compagnies.nom AS 'compagnie',
compagnies.id AS 'id_compagnie',
centres.nom AS 'centre',
centres.id AS 'id_centre'
FROM salles
INNER JOIN centres ON centres.id = salles.centre
INNER JOIN compagnies ON centres.compagnie = compagnies.id;
CREATE VIEW view_employes_lieu AS
SELECT concat(employes.prenom, " ",employes.nom) AS 'employe', centres.nom AS 'Lieu de Travail',
centres.id AS 'id_centre', employes.compagnie AS 'id_compagnie'
FROM employes
INNER JOIN emp_cent ON employes.id = emp_cent.id_emp
INNER JOIN centres ON emp_cent.id_centre = centres.id;
CREATE VIEW view_salle_horaire AS
SELECT salles.id AS "id_salle", salles.nom AS 'salle', horaires.heure_debut AS 'depart', horaires.heure_fin AS 'fin'
FROM salles
INNER JOIN horaires ON horaires.id_salle = hor_salle.id_horaire;
CREATE VIEW view_reservation_compagnie AS
SELECT reservations.id AS 'id_reservation',
reservations.courriel AS 'courriel',
centres.nom AS 'Centre',
salles.nom AS 'Salle',
reservations.date AS 'Date',
reservations.nom_client AS 'Client',
reservations.nb_personnes AS 'Participants',
reservations.num_telephone AS 'Telephone',
reservations.statut_reservation AS 'Statut',
reservations.prix_total AS 'Prix',
salles.id AS 'id_salle',
centres.id AS 'id_centre',
centres.compagnie AS 'id_compagnie'
FROM reservations
INNER JOIN salles ON salles.id = reservations.salle
INNER JOIN centres ON centres.id = salles.centre;
-- Trigger
DELIMITER //
CREATE TRIGGER centre_root
AFTER INSERT
ON compagnies
FOR EACH ROW
INSERT INTO centres(nom, compagnie, adresse, ville, pays, code_postal)
VALUES (new.nom, new.id, '*** rue', 'ville', 'Canada', 'H1H 1H1')//
DELIMITER ;
DELIMITER //
CREATE TRIGGER employe_root
AFTER INSERT
ON compagnies
FOR EACH ROW
INSERT INTO employes(compagnie, nom, prenom, courriel, mot_de_passe)
VALUES (new.id, 'root', 'propriétaire', new.courriel, new.mot_de_passe)//
DELIMITER ;
DELIMITER //
CREATE TRIGGER lier_e_c
AFTER INSERT
ON employes
FOR EACH ROW
INSERT INTO emp_cent(id_emp, id_centre)
VALUES (new.id, (SELECT id from centres WHERE compagnie = new.compagnie LIMIT 1))//
DELIMITER ;