Como prometido, segue exemplo completo e funcional explicando a praticidade e segurança das triggers.
PARTE 1 : MODELAGEM
CREATE DATABASE IF NOT EXISTS treino
/*!40100 DEFAULT CHARACTER SET utf8 */;
USE treino
;
– MySQL dump 10.13 Distrib 5.6.13, for Win32 (x86)
– Host: 192.168.25.7 Database: treino
– Server version 5.1.61
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/!40101 SET NAMES utf8 /;
/!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE /;
/!40103 SET TIME_ZONE=’+00:00’ /;
/!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 /;
/!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 /;
/!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=‘NO_AUTO_VALUE_ON_ZERO’ /;
/!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
–
– Table structure for table mat_prima
DROP TABLE IF EXISTS mat_prima
;
/*!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 */;
CREATE TABLE mat_prima
(
id_materia_prima
int(11) NOT NULL AUTO_INCREMENT,
desc_materia_prima
varchar(100) DEFAULT NULL,
genero_materia_prima
varchar(45) DEFAULT NULL,
preco_compra_mp
decimal(10,3) DEFAULT NULL,
icms_mp
decimal(10,2) DEFAULT NULL,
pis_mp
decimal(10,2) DEFAULT NULL,
cofins_mp
decimal(10,2) DEFAULT NULL,
ipi_mp
decimal(10,2) DEFAULT NULL,
ii_mp
decimal(10,2) DEFAULT NULL,
frete_mp
decimal(10,2) DEFAULT NULL,
comissao_mp
decimal(10,2) DEFAULT NULL,
preco_final_mp
decimal(10,3) DEFAULT NULL,
PRIMARY KEY (id_materia_prima
)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
/!50003 SET sql_mode = @saved_sql_mode / ;
/!50003 SET character_set_client = @saved_cs_client / ;
/!50003 SET character_set_results = @saved_cs_results / ;
/!50003 SET collation_connection = @saved_col_connection / ;
/!50003 SET @saved_cs_client = @@character_set_client / ;
/!50003 SET @saved_cs_results = @@character_set_results / ;
/!50003 SET @saved_col_connection = @@collation_connection / ;
/!50003 SET character_set_client = utf8 / ;
/!50003 SET character_set_results = utf8 / ;
/!50003 SET collation_connection = utf8_general_ci / ;
/!50003 SET @saved_sql_mode = @@sql_mode / ;
/!50003 SET sql_mode = ‘’ / ;
DELIMITER ;;
/!50003 CREATE/ /!50017 DEFINER=root
@%
/ /!50003 TRIGGER mat_prima_AUPD
AFTER UPDATE ON mat_prima
FOR EACH ROW
begin
UPDATE prod_matprima SET total_pf=per_pfNEW.preco_final_mp WHERE id_mat_prima=NEW.id_materia_prima;
END /;;
DELIMITER ;
/!50003 SET sql_mode = @saved_sql_mode / ;
/!50003 SET character_set_client = @saved_cs_client / ;
/!50003 SET character_set_results = @saved_cs_results / ;
/!50003 SET collation_connection = @saved_col_connection */ ;
–
– Table structure for table prod_acabado
DROP TABLE IF EXISTS prod_acabado
;
/*!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 /;
CREATE TABLE prod_acabado
(
id_produto_final
int(11) NOT NULL AUTO_INCREMENT,
desc_pf
varchar(60) DEFAULT NULL,
genero_pf
varchar(45) DEFAULT NULL,
custo_total_pf
decimal(10,2) DEFAULT NULL,
PRIMARY KEY (id_produto_final
)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/!40101 SET character_set_client = @saved_cs_client */;
–
– Table structure for table prod_matprima
DROP TABLE IF EXISTS prod_matprima
;
/!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 /;
CREATE TABLE prod_matprima
(
id_prod_acabado
int(11) NOT NULL,
id_mat_prima
varchar(45) NOT NULL,
per_pf
decimal(10,4) DEFAULT NULL,
total_pf
decimal(10,2) DEFAULT NULL,
PRIMARY KEY (id_prod_acabado
,id_mat_prima
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
/!40101 SET character_set_client = @saved_cs_client /;
/!50003 SET @saved_cs_client = @@character_set_client / ;
/!50003 SET @saved_cs_results = @@character_set_results / ;
/!50003 SET @saved_col_connection = @@collation_connection / ;
/!50003 SET character_set_client = utf8 / ;
/!50003 SET character_set_results = utf8 / ;
/!50003 SET collation_connection = utf8_general_ci / ;
/!50003 SET @saved_sql_mode = @@sql_mode / ;
/!50003 SET sql_mode = ‘’ / ;
DELIMITER ;;
/!50003 CREATE/ /!50017 DEFINER=root
@%
/ /!50003 TRIGGER prod_matprima_AINS
AFTER INSERT ON prod_matprima
FOR EACH ROW
begin
UPDATE prod_acabado
SET custo_total_pf=
(SELECT SUM(total_pf)
FROM prod_matprima
WHERE id_prod_acabado=NEW.id_prod_acabado)
WHERE id_produto_final=NEW.id_prod_acabado;
end /;;
DELIMITER ;
/!50003 SET sql_mode = @saved_sql_mode / ;
/!50003 SET character_set_client = @saved_cs_client / ;
/!50003 SET character_set_results = @saved_cs_results / ;
/!50003 SET collation_connection = @saved_col_connection / ;
/!50003 SET @saved_cs_client = @@character_set_client / ;
/!50003 SET @saved_cs_results = @@character_set_results / ;
/!50003 SET @saved_col_connection = @@collation_connection / ;
/!50003 SET character_set_client = utf8 / ;
/!50003 SET character_set_results = utf8 / ;
/!50003 SET collation_connection = utf8_general_ci / ;
/!50003 SET @saved_sql_mode = @@sql_mode / ;
/!50003 SET sql_mode = ‘’ / ;
DELIMITER ;;
/!50003 CREATE/ /!50017 DEFINER=root
@%
/ /!50003 TRIGGER prod_matprima_AUPD
AFTER UPDATE ON prod_matprima
FOR EACH ROW
begin
UPDATE prod_acabado
SET custo_total_pf=
(SELECT SUM(total_pf)
FROM prod_matprima
WHERE id_prod_acabado=NEW.id_prod_acabado)
WHERE id_produto_final=NEW.id_prod_acabado;
end /;;
DELIMITER ;
/!50003 SET sql_mode = @saved_sql_mode / ;
/!50003 SET character_set_client = @saved_cs_client / ;
/!50003 SET character_set_results = @saved_cs_results / ;
/!50003 SET collation_connection = @saved_col_connection / ;
/!50003 SET @saved_cs_client = @@character_set_client / ;
/!50003 SET @saved_cs_results = @@character_set_results / ;
/!50003 SET @saved_col_connection = @@collation_connection / ;
/!50003 SET character_set_client = utf8 / ;
/!50003 SET character_set_results = utf8 / ;
/!50003 SET collation_connection = utf8_general_ci / ;
/!50003 SET @saved_sql_mode = @@sql_mode / ;
/!50003 SET sql_mode = ‘’ / ;
DELIMITER ;;
/!50003 CREATE/ /!50017 DEFINER=root
@%
/ /*!50003 TRIGGER prod_matprima_ADEL
AFTER DELETE ON prod_matprima
FOR EACH ROW
begin
UPDATE prod_acabado
SET custo_total_pf=
(SELECT SUM(total_pf)
FROM prod_matprima
WHERE id_prod_acabado=OLD.id_prod_acabado)
WHERE id_produto_final=OLD.id_prod_acabado;
end /;;
DELIMITER ;
/!50003 SET sql_mode = @saved_sql_mode / ;
/!50003 SET character_set_client = @saved_cs_client / ;
/!50003 SET character_set_results = @saved_cs_results / ;
/!50003 SET collation_connection = @saved_col_connection */ ;
–
– Dumping events for database ‘treino’
–
– Dumping routines for database ‘treino’
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE /;
/!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS /;
/!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS /;
/!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION /;
/!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
– Dump completed on 2014-01-26 0:37:51