· 7 years ago · Nov 14, 2018, 12:30 AM
1drop database if exists quat_test;
2
3create database if not exists quat_test;
4
5use quat_test;
6
7-- tablas --
8
9-- gasto
10-- ref: proyecto
11-- ref: tipo_gasto
12-- ref: archivo
13create table gasto
14(
15 id int primary key,
16 id_proyecto int not null,
17 id_tipo_gasto int not null,
18 id_subtipo_gasto int,
19 factura bool,
20 id_archivo_factura int,
21 id_archivo_xml int,
22 fecha timestamp,
23 subtotal double,
24 total double not null,
25 forma_pago varchar(255),
26 comentarios text
27);
28
29-- archivo
30create table archivo
31(
32 id int primary key,
33 nombre varchar(255) not null,
34 ruta varchar(255) not null,
35 extension varchar(20),
36 tipo varchar(60) not null,
37 fecha timestamp,
38 archivo longblob
39);
40
41-- proyecto
42create table proyecto
43(
44 id int primary key,
45 nombre varchar(255) not null unique,
46 descripcion text,
47 fecha_inicio timestamp not null,
48 fecha_entrega timestamp,
49 presupuesto double
50);
51
52-- tipo_gasto
53create table tipo_gasto
54(
55 id int primary key,
56 nombre varchar(255) not null unique,
57 descripcion text,
58 requiere_factura bool
59);
60
61-- generadores --
62
63-- tipo_gasto
64insert into tipo_gasto
65(id, nombre)
66values
67(1, "equipo de cómputo"),
68(2, "computadora de escritorio"),
69(3, "computadora laptop"),
70(4, "mouse"),
71(5, "teclado"),
72(6, "servidor"),
73(7, "proyector");
74
75-- proyecto
76insert into proyecto
77(id, nombre, fecha_inicio)
78values
79(1, "beca cobol", "2018-11-13"),
80(2, "beca java", "2018-09-13"),
81(3, "administrador de proyectos", "2018-10-13");
82
83-- gasto
84delimiter //
85create procedure generar_gasto_aleatorio
86(
87 in id_proyecto int,
88 in id_tipo_gasto int
89)
90begin
91 set @id = (select count(*) from gasto) + 1;
92 set @total = 1000 * rand() + 1000;
93 insert into gasto
94 (id, id_proyecto, id_tipo_gasto, total)
95 values
96 (@id, id_proyecto, id_tipo_gasto, @total);
97end //
98delimiter ;
99
100-- reportes --
101
102select * from tipo_gasto;
103
104select * from proyecto;
105
106call generar_gasto_aleatorio(1, 1);
107call generar_gasto_aleatorio(1, 2);
108call generar_gasto_aleatorio(1, 3);
109
110call generar_gasto_aleatorio(2, 2);
111call generar_gasto_aleatorio(2, 3);
112call generar_gasto_aleatorio(2, 4);
113
114select * from gasto;
115
116delimiter //
117create procedure generar_gastos
118(
119 in n int
120)
121begin
122 set @i = 0;
123 while @i < n do
124 set @id_proyecto = (select id from proyecto order by rand() limit 1);
125 set @id_tipo_gasto = (select id from tipo_gasto order by rand() limit 1);
126 call generar_gasto_aleatorio(@id_proyecto, @id_tipo_gasto);
127 set @i = @i + 1;
128 end while;
129end //
130delimiter ;
131
132call generar_gastos(1000);
133
134select * from gasto;
135
136-- consultas --
137
138select id, id_proyecto, id_tipo_gasto, total from gasto;
139
140create view view_gasto_resumen as
141select id, id_proyecto, id_tipo_gasto, total from gasto;
142
143select * from view_gasto_resumen where total >= 1200 and total <= 1300;
144
145select count(total), min(total), max(total), sum(total), avg(total) from view_gasto_resumen where id_tipo_gasto=1;