Oracle_One-Alura_Latam/011_mysql/proyecto_scripts/triggers.sql.old
2023-10-29 03:55:17 -03:00

50 lines
980 B
MySQL

CREATE TABLE facturacion(
fecha DATE NULL,
venta_total FLOAT
);
DELIMITER //
CREATE TRIGGER tg_facturacion_insert
AFTER INSERT ON items
FOR EACH ROW BEGIN
DELETE FROM facturacion;
INSERT INTO facturacion
SELECT A.FECHA, SUM(B.cantidad * B.precio) AS VENTA_TOTAL
FROM facturas A
INNER JOIN
items B
ON A.NUMERO = B.NUMERO
GROUP BY A.FECHA;
END //
DELIMITER //
CREATE TRIGGER tg_facturacion_delete
AFTER DELETE ON items
FOR EACH ROW BEGIN
DELETE FROM facturacion;
INSERT INTO facturacion
SELECT A.fecha, SUM(B.cantidad * B.precio) AS VENTA_TOTAL
FROM facturas A
INNER JOIN
items B
ON A.numero = B.numero
GROUP BY A.fecha;
END //
DELIMITER //
CREATE TRIGGER tg_facturacion_update
AFTER UPDATE ON items
FOR EACH ROW BEGIN
DELETE FROM facturacion;
INSERT INTO facturacion
SELECT A.fecha, SUM(B.cantidad * B.precio) AS VENTA_TOTAL
FROM facturas A
INNER JOIN
items B
ON A.numero = B.numero
GROUP BY A.fecha;
END //
DELIMITER ;