-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.sql
More file actions
337 lines (262 loc) ยท 10.5 KB
/
Copy pathdatabase.sql
File metadata and controls
337 lines (262 loc) ยท 10.5 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
-- =========================
-- CREATION BASE DRIVE
-- =========================
CREATE DATABASE IF NOT EXISTS drive;
USE drive;
-- =========================
-- TABLE CATEGORIES
-- =========================
CREATE TABLE categories (
id INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(100) NOT NULL,
descriptif TEXT
);
-- =========================
-- TABLE PRODUITS
-- =========================
CREATE TABLE produits (
id INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(150) NOT NULL,
date_peremption DATE,
photo VARCHAR(255),
marque VARCHAR(100),
prix DECIMAL(10,2) NOT NULL,
categorie_id INT NOT NULL,
FOREIGN KEY (categorie_id)
REFERENCES categories(id)
ON DELETE CASCADE
);
-- =========================
-- TABLE CLIENTS
-- =========================
CREATE TABLE clients (
numero_client INT AUTO_INCREMENT PRIMARY KEY,
nom VARCHAR(100) NOT NULL,
prenom VARCHAR(100) NOT NULL,
date_inscription DATE NOT NULL,
adresse TEXT NOT NULL
);
-- =========================
-- TABLE COMMANDES
-- =========================
CREATE TABLE commandes (
numero_commande INT AUTO_INCREMENT PRIMARY KEY,
client_id INT NOT NULL,
date_commande DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id)
REFERENCES clients(numero_client)
ON DELETE CASCADE
);
-- =========================
-- TABLE LIGNES COMMANDE
-- =========================
CREATE TABLE lignes_commande (
id INT AUTO_INCREMENT PRIMARY KEY,
commande_id INT NOT NULL,
produit_id INT NOT NULL,
quantite INT NOT NULL,
FOREIGN KEY (commande_id)
REFERENCES commandes(numero_commande)
ON DELETE CASCADE,
FOREIGN KEY (produit_id)
REFERENCES produits(id)
ON DELETE CASCADE
);
-- =========================
-- INSERTION CATEGORIES
-- =========================
INSERT INTO categories (nom, descriptif) VALUES
('Boissons', 'Boissons fraiches'),
('Fruits', 'Fruits frais'),
('Snacks', 'Produits aperitif');
-- =========================
-- INSERTION PRODUITS
-- =========================
INSERT INTO produits
(nom, date_peremption, photo, marque, prix, categorie_id)
VALUES
('Coca Cola 1L', '2026-12-31', 'coca.jpg', 'Coca Cola', 2.50, 1),
('Jus Orange', '2026-10-15', 'jus.jpg', 'Tropicana', 3.20, 1),
('Pommes Golden', '2026-06-15', 'pommes.jpg', 'Vergers France', 1.99, 2),
('Chips Nature', '2026-09-01', 'chips.jpg', 'Lays', 2.10, 3);
-- =========================
-- INSERTION CLIENTS
-- =========================
INSERT INTO clients
(nom, prenom, date_inscription, adresse)
VALUES
('Dupont', 'Jean', '2026-01-10', '12 rue des Fleurs Mulhouse'),
('Martin', 'Claire', '2026-02-15', '5 avenue de Colmar Mulhouse'),
('Bernard', 'Lucas', '2026-03-20', '8 rue des Vosges Colmar');
-- =========================
-- INSERTION COMMANDES
-- =========================
INSERT INTO commandes (client_id)
VALUES
(1),
(2);
-- =========================
-- INSERTION LIGNES COMMANDE
-- =========================
INSERT INTO lignes_commande
(commande_id, produit_id, quantite)
VALUES
(1, 1, 2),
(1, 3, 5),
(2, 2, 1),
(2, 4, 3);
-- =========================
-- AFFICHAGE FICHE COMMANDE
-- =========================
SELECT
c.numero_commande,
cl.nom,
cl.prenom,
p.nom AS produit,
p.prix,
lc.quantite,
(p.prix * lc.quantite) AS total_ligne
FROM lignes_commande lc
JOIN commandes c
ON lc.commande_id = c.numero_commande
JOIN clients cl
ON c.client_id = cl.numero_client
JOIN produits p
ON lc.produit_id = p.id
WHERE c.numero_commande = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CRUD COMPLETS SQL โ
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐งฉ CRUD โ CATรGORIES
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CREATE CATEGORIE
-- INSERT INTO categories (nom, descriptif)
-- VALUES ('Boissons', 'Sodas et boissons fraรฎches');
-- ๐ READ ALL CATEGORIES
-- SELECT * FROM categories;
-- ๐ READ BY ID
-- SELECT * FROM categories WHERE id = 1;
-- โ๏ธ UPDATE CATEGORIE
-- UPDATE categories
-- SET nom = 'Boissons froides',
-- descriptif = 'Sodas, jus, eaux'
-- WHERE id = 1;
-- โ DELETE CATEGORIE
-- DELETE FROM categories
-- WHERE id = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐ฆ CRUD โ PRODUITS
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CREATE PRODUIT
-- INSERT INTO produits (nom, date_peremption, photo, marque, prix, categorie_id)
-- VALUES ('Coca 1L', '2026-12-31', 'coca.jpg', 'Coca Cola', 2.50, 1);
-- ๐ READ ALL PRODUITS
-- SELECT * FROM produits;
-- ๐ READ ALL PRODUITS AVEC CATEGORIE
-- SELECT p.*, c.nom AS categorie
-- FROM produits p
-- JOIN categories c ON p.categorie_id = c.id;
-- ๐ READ PRODUIT BY ID
-- SELECT * FROM produits WHERE id = 1;
-- โ๏ธ UPDATE PRODUIT
-- UPDATE produits
-- SET nom = 'Coca Zero',
-- prix = 2.70
-- WHERE id = 1;
-- โ DELETE PRODUIT
-- DELETE FROM produits
-- WHERE id = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐ค CRUD โ CLIENTS
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CREATE CLIENT
-- INSERT INTO clients (nom, prenom, date_inscription, adresse)
-- VALUES ('Dupont', 'Jean', '2026-01-10', '12 rue des Fleurs');
-- ๐ READ ALL CLIENTS
-- SELECT * FROM clients;
-- ๐ READ CLIENT BY ID
-- SELECT * FROM clients WHERE numero_client = 1;
-- โ๏ธ UPDATE CLIENT
-- UPDATE clients
-- SET adresse = '5 avenue de Colmar'
-- WHERE numero_client = 1;
-- โ DELETE CLIENT
-- DELETE FROM clients
-- WHERE numero_client = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐งพ CRUD โ COMMANDES
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CREATE COMMANDE
-- INSERT INTO commandes (client_id)
-- VALUES (1);
-- ๐ READ ALL COMMANDES
-- SELECT * FROM commandes;
-- ๐ READ COMMANDES AVEC CLIENT
-- SELECT c.numero_commande, cl.nom, cl.prenom, c.date_commande
-- FROM commandes c
-- JOIN clients cl ON c.client_id = cl.numero_client;
-- ๐ READ COMMANDE BY ID
-- SELECT * FROM commandes WHERE numero_commande = 1;
-- โ๏ธ UPDATE COMMANDE
-- UPDATE commandes
-- SET client_id = 2
-- WHERE numero_commande = 1;
-- โ DELETE COMMANDE
-- DELETE FROM commandes
-- WHERE numero_commande = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐งฎ CRUD โ LIGNES DE COMMANDE
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- โ CREATE LIGNE COMMANDE
-- INSERT INTO lignes_commande (commande_id, produit_id, quantite)
-- VALUES (1, 2, 3);
-- ๐ READ ALL LIGNES COMMANDE
-- SELECT * FROM lignes_commande;
-- ๐ READ DETAIL COMMANDE
-- SELECT lc.*, p.nom, p.prix
-- FROM lignes_commande lc
-- JOIN produits p ON lc.produit_id = p.id;
-- ๐ READ LIGNES BY COMMANDE ID
-- SELECT lc.*, p.nom, p.prix
-- FROM lignes_commande lc
-- JOIN produits p ON lc.produit_id = p.id
-- WHERE lc.commande_id = 1;
-- โ๏ธ UPDATE LIGNE COMMANDE
-- UPDATE lignes_commande
-- SET quantite = 5
-- WHERE id = 1;
-- โ DELETE LIGNE COMMANDE
-- DELETE FROM lignes_commande
-- WHERE id = 1;
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐ QUERIES AVANCรES
-- โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
-- ๐ฐ Montant total par commande
-- SELECT
-- c.numero_commande,
-- cl.nom,
-- cl.prenom,
-- SUM(p.prix * lc.quantite) AS montant_total
-- FROM lignes_commande lc
-- JOIN commandes c ON lc.commande_id = c.numero_commande
-- JOIN clients cl ON c.client_id = cl.numero_client
-- JOIN produits p ON lc.produit_id = p.id
-- GROUP BY c.numero_commande, cl.nom, cl.prenom;
-- ๐ Produit le plus commandรฉ
-- SELECT
-- p.nom,
-- SUM(lc.quantite) AS total_vendu
-- FROM lignes_commande lc
-- JOIN produits p ON lc.produit_id = p.id
-- GROUP BY p.nom
-- ORDER BY total_vendu DESC;
-- ๐จโ๐ผ Clients avec nombre de commandes
-- SELECT
-- cl.numero_client,
-- cl.nom,
-- cl.prenom,
-- COUNT(c.numero_commande) AS nombre_commandes
-- FROM clients cl
-- LEFT JOIN commandes c ON cl.numero_client = c.client_id
-- GROUP BY cl.numero_client, cl.nom, cl.prenom;