-
Notifications
You must be signed in to change notification settings - Fork 0
/
Relación 7-1 Resuelta (Uso sentencias básicas lenguaje procedimental).sql
449 lines (391 loc) · 7.95 KB
/
Relación 7-1 Resuelta (Uso sentencias básicas lenguaje procedimental).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
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
use empresa_clase;
/* EJERCICIO 6.1 */
/*
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_1(out anio int)
no sql
begin
set anio = year(current_date);
end
$$
delimiter ;
*/
/*
call GBD_2012_2013_U6_EJER1_1(@n);
select @n;
*/
/* EJERCICIO 6.2 */
/*
delimiter $$
create function GBD_2012_2013_U6_EJER1_2()
returns int
begin
return year(current_date);
end
$$
delimiter ;
*/
/*
select GBD_2012_2013_U6_EJER1_2();
*/
/* EJERCICIO 6.3 */
/*
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_3(in cadena varchar(50))
no sql
begin
select upper(left(cadena,3));
end
$$
delimiter ;
*/
/*
call GBD_2012_2013_U6_EJER1_3('En un lugar de la Mancha');
*/
/* EJERCICIO 6.4 */
/*
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_4(in cad1 varchar(50),
in cad2 varchar(50), out cad3 varchar(100) )
begin
set cad3 = upper(concat(cad1,cad2));
end
$$
delimiter ;
*/
/*
call GBD_2012_2013_U6_EJER1_4('cadena1 +', ' cadena2 ', @result);
select @result;
*/
/* EJERCICIO 6.5 */
/*
delimiter $$
create function GBD_2012_2013_U6_EJER1_5(cat1 int, cat2 int)
returns decimal(10,2)
begin
return sqrt(pow(cat1,2)+pow(cat2,2));
end
$$
delimiter ;
*/
/* select GBD_2012_2013_U6_EJER1_5(2,2);
/*
select GBD_2012_2013_U6_EJER1_5(2,2)
*/
/* EJERCICIO 6.6 */
/*
delimiter $$
create function GBD_2012_2013_U6_EJER1_6(dividendo int, divisor int)
returns boolean
begin
declare res boolean;
if mod(dividendo,divisor) = 0 then
set res = 1;
else
set res = 0;
end if;
return res;
end
$$
delimiter ;
*/
/* select GBD_2012_2013_U6_EJER1_6(4,2),
GBD_2012_2013_U6_EJER1_6(5,3),
GBD_2012_2013_U6_EJER1_6(9,3);*/
/* EJERCICIO 6.7 */
/*
delimiter $$
create function GBD_2012_2013_U6_EJER1_7(numero int)
returns varchar(10)
begin
declare res varchar(10);
case numero
when 1 then
set res = 'lunes';
when 2 then
set res = 'martes';
when 3 then
set res = 'miércoles';
when 4 then
set res = 'jueves';
when 5 then
set res = 'viernes';
when 6 then
set res = 'sábado';
when 7 then
set res = 'domingo';
else set res = 'no día semana';
end case;
return res;
end $$
delimiter ;
*/
/* EJERCICIO 6.8 */
drop function GBD_2012_2013_U6_EJER1_8;
delimiter $$
create function GBD_2012_2013_U6_EJER1_8(n1 int, n2 int, n3 int)
returns int
begin
/* select GBD_2012_2013_U6_EJER1_8(2,3,4),
GBD_2012_2013_U6_EJER1_8(5,4,2),
GBD_2012_2013_U6_EJER1_8(34,989,87),
GBD_2012_2013_U6_EJER1_8(1,1,2);
*/
declare max int;
if n1 > n2 then
begin
if n1 > n3 then
begin
set max = n1;
end;
else
begin
set max = n3;
end;
end if;
end;
else
begin
if n2 > n3 then
begin
set max = n2;
end;
else
begin
set max = n3;
end;
end if;
end;
end if;
return max;
end
$$
delimiter ;
/* EJERCICIO 6.9 */
delimiter $$
create function GBD_2012_2013_U6_EJER1_9(palabra varchar(15))
returns bit
begin
/* select GBD_2012_2013_U6_EJER1_9('ANA'),
GBD_2012_2013_U6_EJER1_9('BELEN');*/
DECLARE palindroma bit;
if palabra = reverse(palabra) then
set palindroma= 1;
else
set palindroma= 0;
end if;
return palindroma;
end
$$
delimiter ;
/* EJERCICIO 6.10 */
delimiter $$
create procedure suma_n_con_repeat(in n int)
begin
declare cont,suma int;
set cont = 0;
set suma = 0;
REPEAT
begin
set suma = suma + cont;
set cont = cont + 1;
end;
UNTIL cont>n
end repeat;
select suma;
end
$$
delimiter ;
delimiter $$
create procedure suma_n_con_while(in n int)
begin
declare cont,suma int default 0;
set cont = n;
while cont >= 1 do
begin
set suma = suma + cont;
set cont = cont - 1;
end;
end while;
select suma;
/* empezando desde 0 hasta n ==>
declare cont,suma int default 0;
-- set cont = 0;
-- set suma = 0;
while cont <= n do
begin
set suma = suma + cont;
set cont = cont + 1;
end;
end while;
select suma;
*/
end $$
delimiter ;
/* EJERCICIO 6.11 */
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_11(in m int)
begin
declare cont,
suma decimal (10,2);
if m=0 then
select 'división por cero';
else if m = 1 then
select 1;
else
begin
set cont = 2;
set suma = 0;
while cont <= m do
begin
set suma = suma + 1/cont;
set cont = cont + 1;
end;
end while;
select suma;
end;
end if;
end if;
end
$$
delimiter ;
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_11_con_repeat(in m int)
begin
declare cont,
suma decimal (10,2);
if m=0 then
select 'división por cero';
else if m = 1 then
select 1;
else
begin
set cont = 2;
set suma = 0;
repeat
-- while cont <= m do
begin
set suma = suma + 1/cont;
set cont = cont + 1;
end;
until cont > m end repeat;
-- end while;
select suma;
end;
end if;
end if;
end
$$
delimiter ;
/* EJERCICIO 6.12 */
drop function if exists es_primo;
delimiter $$
-- create function GBD_2012_2013_U6_EJER1_12(n int)
create function es_primo(n int)
returns boolean
begin
declare cont int;
declare es_primo bit default 1;
set cont = 2;
while (cont < n) do
begin
if (n%cont = 0) then
begin
set es_primo = 0;
set cont = n;
end;
else
set cont = cont + 1;
end if;
end;
end while;
return es_primo;
end
$$
delimiter ;
-- select es_primo(2), es_primo(10), es_primo(7);
/* EJERCICIO 6.13 */
drop function if exists GBD_2012_2013_U6_EJER1_13;
delimiter $$
create function GBD_2012_2013_U6_EJER1_13(n int)
returns int
begin
declare cont, suma int;
declare num_primos int default 1;
set cont = 1;
set suma = 0;
while (num_primos <= n) do
begin
if (es_primo (cont)) then
begin
set suma = suma + cont;
set num_primos = num_primos + 1;
end;
end if;
set cont = cont + 1;
end;
end while;
return suma;
end
$$
delimiter ;
-- select GBD_2012_2013_U6_EJER1_13(5)
/* EJERCICIO 6.14 */
drop procedure if exists GBD_2012_2013_U6_EJER1_14;
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_14(n int)
begin
declare cont int;
declare numid int;
declare num_primos int default 0;
drop table if exists primos;
create temporary table if not exists primos
(id int auto_increment primary key,
primo int);
set cont = 1;
while (num_primos< n) do
begin
if es_primo (cont) then
-- set numid = (select ifnull(max(id),0)+1 from primos);
insert primos
(primo)
values
(cont);
set num_primos = num_primos + 1;
end if;
set cont = cont + 1;
end;
end while;
select * from primos;
end
$$
delimiter ;
/* call GBD_2012_2013_U6_EJER1_14(5);
select * from primos;
*/
/* EJERCICIO 6.15 */
/*
delimiter $$
create procedure GBD_2012_2013_U6_EJER1_15(n int, out n_primos int)
begin
declare cont int;
set cont = 1;
delete
from primos;
while (cont <= n) do
begin
if (es_primo (cont)) then
insert primos
(id, primo)
values
((select ifnull(max(id),0)+1 from primos), cont);
end if;
set cont = cont + 1;
end;
end while;
select n_primos = ifnull(count(*),0)
from primos;
end
$$
delimiter ;
*/