· 8 years ago · Jan 14, 2018, 11:26 AM
1drop table if exists updatepedidos;
2create table updatepedidos (
3 CodigoPedido integer NOT NULL,
4 FechaPedido date NOT NULL,
5 FechaEsperada date NOT NULL,
6 FechaEntrega date DEFAULT NULL,
7 Estado varchar(15) NOT NULL,
8 Comentarios text,
9 CodigoCliente integer NOT NULL,
10 PRIMARY KEY (CodigoPedido),
11 CONSTRAINT Pedidos_Cliente FOREIGN KEY (CodigoCliente) REFERENCES Clientes (CodigoCliente)
12);
13
14
15drop trigger if exists controlpedidos;
16
17DELIMITER //
18
19create trigger controlpedidos before update on pedidos for each row
20begin
21
22if new.CodigoPedido<>old.CodigoPedido then
23insert into updatepedidos values (new.CodigoPedido, old.FechaPedido, old.FechaEsperada, old.FechaEntrega, old.Estado, old.Comentarios, old.CodigoCliente);
24end if;
25
26if new.FechaPedido<>old.FechaPedido then
27insert into updatepedidos values (old.CodigoPedido, new.FechaPedido, old.FechaEsperada, old.FechaEntrega, old.Estado, old.Comentarios, old.CodigoCliente);
28end if;
29
30if new.FechaEsperada<>old.FechaEsperada then
31insert into updatepedidos values (old.CodigoPedido, old.FechaPedido, new.FechaEsperada, old.FechaEntrega, old.Estado, old.Comentarios, old.CodigoCliente);
32end if;
33
34if new.FechaEntrega<>old.FechaEntrega then
35insert into updatepedidos values (old.CodigoPedido, old.FechaPedido, old.FechaEsperada, new.FechaEntrega, old.Estado, old.Comentarios, old.CodigoCliente);
36end if;
37
38if new.Estado<>old.Estado then
39insert into updatepedidos values (old.CodigoPedido, old.FechaPedido, old.FechaEsperada, old.FechaEntrega, new.Estado, old.Comentarios, old.CodigoCliente);
40end if;
41
42if new.Comentarios<>old.Comentarios then
43insert into updatepedidos values (old.CodigoPedido, old.FechaPedido, old.FechaEsperada, old.FechaEntrega, old.Estado, new.Comentarios, old.CodigoCliente);
44end if;
45
46if new.CodigoCliente<>old.CodigoCliente then
47insert into updatepedidos values (old.CodigoPedido, new.FechaPedido, old.FechaEsperada, old.FechaEntrega, old.Estado, old.Comentarios, new.CodigoCliente);
48end if;
49
50end//
51DELIMITER ;
52
53update pedidos set CodigoCliente=22 where CodigoPedido=1;