· 7 years ago · Dec 13, 2018, 08:28 AM
1/*********************
2 DATABASE
3*********************/
4DROP DATABASE IF EXISTS CARRITO;
5
6CREATE DATABASE IF NOT EXISTS CARRITO;
7
8USE CARRITO;
9
10/*==============================================================*/
11/* DBMS name: MySQL 5.0 */
12/* Created on: 15/02/2011 16:12:58 */
13/*==============================================================*/
14
15
16/*********************
17 TABLAS
18*********************/
19drop table if exists LINEA_PEDIDO;
20
21drop table if exists PEDIDO;
22
23drop table if exists PRODUCTO;
24
25drop table if exists USUARIO;
26
27/*==============================================================*/
28/* Table: LINEA_PEDIDO */
29/*==============================================================*/
30create table LINEA_PEDIDO
31(
32 COD_LP numeric(10,0) not null,
33 COD_P numeric(10,0) not null,
34 COD_PR numeric(5,0) not null,
35 CANTIDAD_PR numeric(3,0) default 1,
36 PRECIO_U numeric(6,2),
37 DESCUENTO numeric(6,2) default 0,
38 IVA numeric(4,2) default 18,
39 primary key (COD_LP)
40)
41ENGINE=INNODB;
42
43/*==============================================================*/
44/* Table: PEDIDO */
45/*==============================================================*/
46create table PEDIDO
47(
48 COD_P numeric(10,0) not null,
49 DNI varchar(10) not null,
50 FECHA_P date,
51 IMPORTE_TOTAL numeric(10,2) default 0,
52 CONFIRMADO bool,
53 primary key (COD_P)
54)
55ENGINE=INNODB;
56
57/*==============================================================*/
58/* Table: PRODUCTO */
59/*==============================================================*/
60create table PRODUCTO
61(
62 COD_PR numeric(5,0) not null,
63 NOMBRE_PR varchar(30) not null,
64 PRECIO_PR numeric(6,2),
65 UNIDADES numeric(5,0) default 100,
66 primary key (COD_PR)
67)
68ENGINE=INNODB;
69
70/*==============================================================*/
71/* Table: USUARIO */
72/*==============================================================*/
73create table USUARIO
74(
75 DNI varchar(10) not null,
76 NOMBRE varchar(30) not null,
77 APELLIDOS varchar(30),
78 EMAIL varchar(50) not null,
79 PASSWORD varchar(30) not null,
80 DIRECCION varchar(50) not null,
81 SEXO char(1),
82 FECHA_NAC date,
83 FECHA_ALTA date,
84 ACTIVADO bool,
85 ADM char(1),
86 primary key (DNI)
87)
88ENGINE=INNODB;
89
90alter table LINEA_PEDIDO add constraint FK_FORMADO foreign key (COD_P)
91 references PEDIDO (COD_P) on delete restrict on update restrict;
92
93alter table LINEA_PEDIDO add constraint FK_TIENE foreign key (COD_PR)
94 references PRODUCTO (COD_PR) on delete restrict on update restrict;
95
96alter table PEDIDO add constraint FK_REALIZA foreign key (DNI)
97 references USUARIO (DNI) on delete restrict on update restrict;
98
99
100/*********************
101 DISPARADORES
102*********************/
103drop trigger if exists stock_metelinea;
104
105delimiter ||
106
107create trigger stock_metelinea after insert on LINEA_PEDIDO
108for each row begin
109 update producto set unidades=unidades-new.cantidad_pr where cod_pr=new.cod_pr;
110end
111||
112
113drop trigger if exists stock_sacalinea;
114
115delimiter ||
116
117create trigger stock_sacalinea after delete on LINEA_PEDIDO
118for each row begin
119 update producto set unidades=unidades+old.cantidad_pr where cod_pr=old.cod_pr;
120end
121||
122
123drop trigger if exists stock_actulinea;
124
125delimiter ||
126
127create trigger stock_actulinea after update on LINEA_PEDIDO
128for each row begin
129 update producto set unidades=unidades-(new.cantidad_pr-old.cantidad_pr) where cod_pr=old.cod_pr;
130end
131||
132
133
134/*********************
135 PROCEDURES
136*********************/
137
138/******USUARIOS******/
139drop procedure if exists nuevouser;
140
141delimiter ||
142
143create procedure nuevouser(d varchar(10), name varchar(30), ap varchar(30), em varchar(50), pass varchar(30), dire varchar(50), sex char(1), fec date)
144begin
145 insert into usuario values (d, name, ap, em, pass, dire, sex, fec, curdate(), false, 'c');
146end
147||
148
149drop procedure if exists activuser;
150
151delimiter ||
152
153create procedure activuser(d varchar(10))
154begin
155 update usuario set activado=true where dni=d;
156end
157||
158
159
160drop procedure if exists blockuser;
161
162delimiter ||
163
164create procedure blockuser(d varchar(10))
165begin
166 update usuario set activado=false where dni=d;
167end
168||
169
170drop procedure if exists moduser;
171
172delimiter ||
173
174create procedure moduser(d varchar(10), name varchar(30), ap varchar(30), em varchar(50), pass varchar(30), dire varchar(50), sex char(1), fec date)
175begin
176 update usuario set nombre=name, apellidos=ap, email=em, password=pass, direccion=dire, sexo=sex, fecha_nac=fec where dni=d;
177end
178||
179
180drop procedure if exists admuser;
181
182delimiter ||
183
184create procedure admuser(d varchar(10))
185begin
186 update usuario set adm='a' where dni=d;
187end
188||
189
190drop procedure if exists borrauser;
191
192delimiter ||
193
194create procedure borrauser(d varchar(10))
195begin
196 delete from usuario where dni=d;
197end
198||
199
200
201/******LINEAS******/
202drop procedure if exists metelinea;
203
204delimiter ||
205
206create procedure metelinea(clp numeric(10,0), cp numeric(10,0), cpr numeric(5,0))
207begin
208 declare
209 x numeric(6,2);
210 select precio_pr into x from producto where cod_pr=cpr;
211 insert into linea_pedido (cod_lp, cod_p, cod_pr, precio_u) values (clp, cp, cpr, x);
212end
213||
214
215drop procedure if exists sacalinea;
216
217delimiter ||
218
219create procedure sacalinea(clp numeric(10,0))
220begin
221 delete from linea_pedido where cod_lp=clp;
222end
223||
224
225drop procedure if exists modlinea;
226
227delimiter ||
228
229create procedure modlinea(clp numeric(10,0),cant numeric(3,0), descu numeric(6,2), avi numeric(4,2))
230begin
231 update linea_pedido set cantidad_pr=cant, descuento=descu, iva=avi where cod_lp=clp;
232end
233||
234
235/******PRODUCTOS******/
236drop procedure if exists insertaproducto;
237
238delimiter ||
239
240create procedure insertaproducto(cpr numeric(5,0), nom varchar(30), pr numeric(6,2))
241begin
242 insert into producto (cod_pr, nombre_pr, precio_pr) values (cpr, nom, pr);
243end
244||
245
246drop procedure if exists cambiastock;
247
248delimiter ||
249
250create procedure cambiastock(cpr numeric(5,0), cantidad numeric(5,0), precio numeric(6,2))
251begin
252 update producto set unidades=cantidad, precio_pr=precio where cod_pr=cpr;
253end
254||
255
256drop procedure if exists borraproducto;
257
258delimiter ||
259
260create procedure borraproducto(cpr numeric(5,0))
261begin
262 delete from producto where cod_pr=cpr;
263end
264||
265
266
267drop procedure if exists modproducto;
268
269delimiter ||
270
271create procedure modproducto(cpr numeric(5,0), nom varchar(30), pr numeric(6,2))
272begin
273 update producto set nombre_pr=nom, precio_pr=pr where cod_pr=cpr;
274end
275||
276
277/******PEDIDOS******/
278drop procedure if exists insertapedido;
279
280delimiter ||
281
282create procedure insertapedido(cp numeric(10,0), d varchar(10))
283begin
284 insert into pedido (cod_p, dni, fecha_p, confirmado) values (cp, d, curdate(), false);
285end
286||
287
288drop procedure if exists confpedido;
289
290delimiter ||
291
292create procedure confpedido(cp numeric(10,0))
293begin
294 update pedido set confirmado=true where cod_p=cp;
295end
296||
297
298drop procedure if exist borrapedido;
299
300delimiter ||
301
302create procedure borrapedido(cp numeric(10,0))
303begin
304 delete from pedido where cod_p=cp;
305end
306||
307
308drop procedure if exists calctotped;
309
310delimiter ||
311
312create procedure calctotped(cp numeric(10,0))
313begin
314 update pedido set importe_total=(SELECT sum((cantidad_pr *(precio_u -descuento))+((cantidad_pr *(precio_u -descuento))*(18/100))) FROM linea_pedido WHERE cod_p =cp) where cod_p=cp;
315end
316||
317
318delimiter ;
319/*********************
320 DATOS
321*********************/
322call nuevouser('48921213Y','Jose Miguel','Escobar Garrido','josem.escobar.g@gmail.com','1111','C/ Santa Maria','H','1987/06/03');
323call nuevouser('48921212M','Mariola','Escobar Garrido','mariola.escobar.g@gmail.com','1111','C/ Santa Maria','M','1982/08/16');
324call nuevouser('00000000X','Admin','Carrito','admin@carrito.com','1111','C/ Tesla','H','1982/08/16');
325call admuser('00000000X');
326call nuevouser('11111111Z','Anonimo',' ','anon@anon.com','anon','C/ Oculta','H','1978/03/27');
327update usuario set adm='x' where dni='11111111Z';
328call activuser('48921213Y');
329call activuser('00000000X');
330call activuser('11111111Z');
331call insertaproducto(1, 'Teclado', 12.50);
332call insertaproducto(2, 'Raton', 10.00);
333call insertaproducto(3, 'Cascos', 15.00);
334call insertaproducto(4, 'USB 8Gb', 8.00);
335call insertaproducto(5, 'HDD 500Gb', 100.00);
336call insertapedido(1, '48921213Y');
337call insertapedido(2, '48921213Y');
338call insertapedido(3, '48921213Y');
339call metelinea(1, 1, 1);
340call metelinea(2, 1, 2);
341call metelinea(3, 2, 3);
342call metelinea(4, 2, 4);
343call metelinea(5, 3, 5);
344call metelinea(6, 3, 1);
345call modlinea(1, 15, 2, 18);
346call calctotped(1);
347call calctotped(2);
348call calctotped(3);
349call confpedido(1);
350call confpedido(2);
351call confpedido(3);