-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAulas udemy mysql_1.sql
352 lines (284 loc) · 10.9 KB
/
Aulas udemy mysql_1.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
SELECT C.NOME, C.EMAIL, E.ESTADO, T.NUMERO
FROM CLIENTE C
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE;
+---------+-------------------+--------+----------+
| NOME | EMAIL | ESTADO | NUMERO |
+---------+-------------------+--------+----------+
| JORGE | JORGE@IG.COM | ES | 78458743 |
| JORGE | JORGE@IG.COM | ES | 56576876 |
| JOAO | JOAOA@IG.COM | RJ | 87866896 |
| CARLOS | CARLOSA@IG.COM | RJ | 54768899 |
| JOAO | JOAOA@IG.COM | RJ | 99667587 |
| ANA | ANA@IG.COM | SP | 78989765 |
| ANA | ANA@IG.COM | SP | 99766676 |
| JOAO | JOAOA@IG.COM | RJ | 66687899 |
| JORGE | JORGE@IG.COM | ES | 89986668 |
| CARLOS | CARLOSA@IG.COM | RJ | 88687909 |
| FLAVIO | FLAVIO@IG.COM | MG | 68976565 |
| FLAVIO | FLAVIO@IG.COM | MG | 99656675 |
| GIOVANA | NULL | RJ | 33567765 |
| GIOVANA | NULL | RJ | 88668786 |
| GIOVANA | NULL | RJ | 55689654 |
| KARLA | KARLA@GMAIL.COM | RJ | 88687979 |
| DANIELE | DANIELE@GMAIL.COM | ES | 88965676 |
| EDUARDO | NULL | PR | 89966809 |
| ANTONIO | ANTONIO@IG.COM | SP | 88679978 |
| ANTONIO | ANTONIO@UOL.COM | PR | 99655768 |
| ELAINE | ELAINE@GLOBO.COM | SP | 89955665 |
| CARMEM | CARMEM@IG.COM | RJ | 77455786 |
| CARMEM | CARMEM@IG.COM | RJ | 89766554 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 77755785 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 44522578 |
+---------+-------------------+--------+----------+
IFNULL()
SELECT C.NOME,
IFNULL(C.EMAIL,'NAO TEM EMAIL'),
E.ESTADO,
T.NUMERO
FROM CLIENTE C
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE;
SELECT C.NOME,
IFNULL(C.EMAIL,'NAO TEM EMAIL'),
E.ESTADO,
T.NUMERO
FROM CLIENTE C
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE;
+---------+---------------------------------+--------+----------+
| NOME | IFNULL(C.EMAIL,'NAO TEM EMAIL') | ESTADO | NUMERO |
+---------+---------------------------------+--------+----------+
| JORGE | JORGE@IG.COM | ES | 78458743 |
| JORGE | JORGE@IG.COM | ES | 56576876 |
| JOAO | JOAOA@IG.COM | RJ | 87866896 |
| CARLOS | CARLOSA@IG.COM | RJ | 54768899 |
| JOAO | JOAOA@IG.COM | RJ | 99667587 |
| ANA | ANA@IG.COM | SP | 78989765 |
| ANA | ANA@IG.COM | SP | 99766676 |
| JOAO | JOAOA@IG.COM | RJ | 66687899 |
| JORGE | JORGE@IG.COM | ES | 89986668 |
| CARLOS | CARLOSA@IG.COM | RJ | 88687909 |
| FLAVIO | FLAVIO@IG.COM | MG | 68976565 |
| FLAVIO | FLAVIO@IG.COM | MG | 99656675 |
| GIOVANA | NAO TEM EMAIL | RJ | 33567765 |
| GIOVANA | NAO TEM EMAIL | RJ | 88668786 |
| GIOVANA | NAO TEM EMAIL | RJ | 55689654 |
| KARLA | KARLA@GMAIL.COM | RJ | 88687979 |
| DANIELE | DANIELE@GMAIL.COM | ES | 88965676 |
| EDUARDO | NAO TEM EMAIL | PR | 89966809 |
| ANTONIO | ANTONIO@IG.COM | SP | 88679978 |
| ANTONIO | ANTONIO@UOL.COM | PR | 99655768 |
| ELAINE | ELAINE@GLOBO.COM | SP | 89955665 |
| CARMEM | CARMEM@IG.COM | RJ | 77455786 |
| CARMEM | CARMEM@IG.COM | RJ | 89766554 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 77755785 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 44522578 |
+---------+---------------------------------+--------+----------+
SELECT C.NOME,
IFNULL(C.EMAIL,'*****************'),
E.ESTADO,
T.NUMERO
FROM CLIENTE C
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE;
+---------+-------------------------------------+--------+----------+
| NOME | IFNULL(C.EMAIL,'*****************') | ESTADO | NUMERO |
+---------+-------------------------------------+--------+----------+
| JORGE | JORGE@IG.COM | ES | 78458743 |
| JORGE | JORGE@IG.COM | ES | 56576876 |
| JOAO | JOAOA@IG.COM | RJ | 87866896 |
| CARLOS | CARLOSA@IG.COM | RJ | 54768899 |
| JOAO | JOAOA@IG.COM | RJ | 99667587 |
| ANA | ANA@IG.COM | SP | 78989765 |
| ANA | ANA@IG.COM | SP | 99766676 |
| JOAO | JOAOA@IG.COM | RJ | 66687899 |
| JORGE | JORGE@IG.COM | ES | 89986668 |
| CARLOS | CARLOSA@IG.COM | RJ | 88687909 |
| FLAVIO | FLAVIO@IG.COM | MG | 68976565 |
| FLAVIO | FLAVIO@IG.COM | MG | 99656675 |
| GIOVANA | ***************** | RJ | 33567765 |
| GIOVANA | ***************** | RJ | 88668786 |
| GIOVANA | ***************** | RJ | 55689654 |
| KARLA | KARLA@GMAIL.COM | RJ | 88687979 |
| DANIELE | DANIELE@GMAIL.COM | ES | 88965676 |
| EDUARDO | ***************** | PR | 89966809 |
| ANTONIO | ANTONIO@IG.COM | SP | 88679978 |
| ANTONIO | ANTONIO@UOL.COM | PR | 99655768 |
| ELAINE | ELAINE@GLOBO.COM | SP | 89955665 |
| CARMEM | CARMEM@IG.COM | RJ | 77455786 |
| CARMEM | CARMEM@IG.COM | RJ | 89766554 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 77755785 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 44522578 |
+---------+-------------------------------------+--------+----------++---------+-------------------------------------+--------+----------+
SELECT C.NOME,
IFNULL(C.EMAIL,'*****************') AS "E-MAIL",
E.ESTADO,
T.NUMERO
FROM CLIENTE C
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE;
+---------+-------------------+--------+----------+
| NOME | E-MAIL | ESTADO | NUMERO |
+---------+-------------------+--------+----------+
| JORGE | JORGE@IG.COM | ES | 78458743 |
| JORGE | JORGE@IG.COM | ES | 56576876 |
| JOAO | JOAOA@IG.COM | RJ | 87866896 |
| CARLOS | CARLOSA@IG.COM | RJ | 54768899 |
| JOAO | JOAOA@IG.COM | RJ | 99667587 |
| ANA | ANA@IG.COM | SP | 78989765 |
| ANA | ANA@IG.COM | SP | 99766676 |
| JOAO | JOAOA@IG.COM | RJ | 66687899 |
| JORGE | JORGE@IG.COM | ES | 89986668 |
| CARLOS | CARLOSA@IG.COM | RJ | 88687909 |
| FLAVIO | FLAVIO@IG.COM | MG | 68976565 |
| FLAVIO | FLAVIO@IG.COM | MG | 99656675 |
| GIOVANA | ***************** | RJ | 33567765 |
| GIOVANA | ***************** | RJ | 88668786 |
| GIOVANA | ***************** | RJ | 55689654 |
| KARLA | KARLA@GMAIL.COM | RJ | 88687979 |
| DANIELE | DANIELE@GMAIL.COM | ES | 88965676 |
| EDUARDO | ***************** | PR | 89966809 |
| ANTONIO | ANTONIO@IG.COM | SP | 88679978 |
| ANTONIO | ANTONIO@UOL.COM | PR | 99655768 |
| ELAINE | ELAINE@GLOBO.COM | SP | 89955665 |
| CARMEM | CARMEM@IG.COM | RJ | 77455786 |
| CARMEM | CARMEM@IG.COM | RJ | 89766554 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 77755785 |
| ADRIANA | ADRIANA@GMAIL.COM | RJ | 44522578 |
+---------+-------------------+--------+----------+
/* VIEW */
CREATE VIEW V_RELATORIO AS
SELECT C.NOME,
C.SEXO,
IFNULL(C.EMAIL,'CLIENTE SEM EMAIL') AS "E-MAIL",
T.TIPO,
T.NUMERO,
E.BAIRRO,
E.CIDADE,
E.ESTADO
FROM CLIENTE C
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE;
SELECT * FROM RELATORIO;
SHOW TABLES;
SHOW DATABASES;
SHOW VIEWS; --- NAO EXISTE
CREATE VIEW V_RELATORIO AS
SELECT C.NOME,
C.SEXO,
IFNULL(C.EMAIL,'CLIENTE SEM EMAIL') AS "E-MAIL",
T.TIPO,
T.NUMERO,
E.BAIRRO,
E.CIDADE,
E.ESTADO
FROM CLIENTE C
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE;
SELECT NOME, NUMERO, ESTADO
FROM V_RELATORIO;
/*OPERACOES DE DML EM VIEWS */
/* UPDATE INSERT E DELETE */
INSERT INTO V_RELATORIO VALUES(
'ANDREIA','F','ANDREIA@UOL.COM.BR','CEL','873547864','CENTRO','VITORIA','ES'
);
CREATE TABLE JOGADORES(
IDJOGADOR INT,
NOME VARCHAR(30),
ESTADO CHAR(2)
);
INSERT INTO JOGADORES VALUES(1,'GUERRERO','RS');
INSERT INTO JOGADORES VALUES(2,'GABIGOL','RJ');
INSERT INTO JOGADORES VALUES(3,'GANSO','RJ');
INSERT INTO JOGADORES VALUES(4,'NENÊ', 'RJ');
INSERT INTO JOGADORES VALUES(5,'LOVE','SP');
CREATE VIEW V_JOGADORES AS
SELECT NOME, ESTADO
FROM JOGADORES;
SELECT * FROM V_JOGADORES;
/* NEM INSERT,DELETE FUNCIONAM COM VIEW, POREM UPDATE FUNCIONA (ISSO COM JOIN) */
DELETE FROM V_JOGADORES
WHERE NOME = 'GUERRERO'
INSERT INTO V_JOGADORES VALUES('GUERRERO','RS');
SELECT * FROM JOGADORES;
SELECT * FROM V_RELATORIO
WHERE SEXO = 'F';
/* ORDER BY */
CREATE TABLE ALUNOS(
NUMERO INT,
NOME VARCHAR(30)
);
INSERT INTO ALUNOS VALUES(1,'JOAO');
INSERT INTO ALUNOS VALUES(1,'MARIA');
INSERT INTO ALUNOS VALUES(2,'ZOE');
INSERT INTO ALUNOS VALUES(2,'ANDRE');
INSERT INTO ALUNOS VALUES(3,'CLARA');
INSERT INTO ALUNOS VALUES(1,'CLARA');
INSERT INTO ALUNOS VALUES(4,'MAFRA');
INSERT INTO ALUNOS VALUES(5,'JANAINA');
INSERT INTO ALUNOS VALUES(1,'JANAINA');
INSERT INTO ALUNOS VALUES(3,'MARCELO');
INSERT INTO ALUNOS VALUES(4,'GIOVANI');
INSERT INTO ALUNOS VALUES(5,'ANTONIO');
INSERT INTO ALUNOS VALUES(6,'ANA');
INSERT INTO ALUNOS VALUES(6,'VIVIANE');
SELECT * FROM ALUNOS
ORDER BY NUMERO;
SELECT * FROM ALUNOS
ORDER BY 1;
SELECT * FROM ALUNOS
ORDER BY 2;
/* ORDENANDO POR MAIS DE UMA COLUNA */
SELECT * FROM ALUNOS
ORDER BY 1;
SELECT * FROM ALUNOS
ORDER BY NUMERO, NOME;
SELECT * FROM ALUNOS
ORDER BY 1, 2;
/* MESCLANDO ORDER BY COM PROJECAO */
SELECT NOME FROM ALUNOS
ORDER BY 1, 2;
SELECT NOME FROM ALUNOS
ORDER BY NUMERO, NOME;
/* ORDER BY DESC / ASC */
SELECT NOME FROM ALUNOS
ORDER BY 1 ASC;
SELECT NOME FROM ALUNOS
ORDER BY 1 DESC;
SELECT NOME FROM ALUNOS
ORDER BY 1, 2 DESC;
SELECT NOME FROM ALUNOS
ORDER BY 1 DESC, 2 DESC;
/* ORDENANDO COM JOINS */
SELECT C.NOME,
C.SEXO,
IFNULL(C.EMAIL,'CLIENTE SEM EMAIL') AS "E-MAIL",
T.TIPO,
T.NUMERO,
E.BAIRRO,
E.CIDADE,
E.ESTADO
FROM CLIENTE C
INNER JOIN TELEFONE T
ON C.IDCLIENTE = T.ID_CLIENTE
INNER JOIN ENDERECO E
ON C.IDCLIENTE = E.ID_CLIENTE
ORDER BY 1;
SHOW TABLES;
SELECT * FROM V_RELATORIO
ORDER BY 1;