MariaDB [subconsultas]> Delimiter // MariaDB [subconsultas]> create trigger valorventa after insert on articulo -> for each row -> begin -> update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo=detalle.codigo); -> end -> // Query OK, 0 rows affected (0.086 sec) MariaDB [subconsultas]> show triggers; -> // +------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation | +------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | valorventa | INSERT | articulo | begin update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo=detalle.codigo); end | AFTER | 2025-03-08 16:23:03.27 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | +------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 1 row in set (0.040 sec) MariaDB [subconsultas]> select * from detalle ; -> // +----+------------+------------+----------+------------+-------+--------+ | id | nrofactura | fecha | cantidad | valorventa | total | codigo | +----+------------+------------+----------+------------+-------+--------+ | 1 | 1200 | 2010-01-30 | 3 | 0 | 0 | 150 | | 2 | 1250 | 2010-02-13 | 5 | 0 | 0 | 150 | | 3 | 1250 | 2010-02-13 | 7 | 0 | 0 | 250 | | 4 | 1300 | 2010-03-02 | 1 | 0 | 0 | 350 | | 5 | 1300 | 2010-03-02 | 2 | 0 | 0 | 300 | | 6 | 1400 | 2010-03-11 | 3 | 0 | 0 | 200 | | 7 | 1500 | 2010-03-21 | 5 | 0 | 0 | 250 | +----+------------+------------+----------+------------+-------+--------+ 7 rows in set (0.000 sec) MariaDB [subconsultas]> delimiter ; MariaDB [subconsultas]> insert into articulo values('450','Secador Remington 1600',10,250000,0); Query OK, 1 row affected (0.040 sec) MariaDB [subconsultas]> select * from articulo; +--------+------------------------+----------+---------------+------------+ | codigo | articulo | cantidad | valorunitario | existencia | +--------+------------------------+----------+---------------+------------+ | 150 | nevera | 25 | 950000 | 0 | | 200 | televisor | 11 | 1200000 | 0 | | 250 | estufa | 30 | 750000 | 0 | | 300 | ventilador | 17 | 110000 | 0 | | 350 | lavadora | 13 | 980000 | 0 | | 450 | Secador Remington 1600 | 10 | 250000 | 0 | +--------+------------------------+----------+---------------+------------+ 6 rows in set (0.001 sec) MariaDB [subconsultas]> select * from detalle; +----+------------+------------+----------+------------+-------+--------+ | id | nrofactura | fecha | cantidad | valorventa | total | codigo | +----+------------+------------+----------+------------+-------+--------+ | 1 | 1200 | 2010-01-30 | 3 | 1168500 | 0 | 150 | | 2 | 1250 | 2010-02-13 | 5 | 1168500 | 0 | 150 | | 3 | 1250 | 2010-02-13 | 7 | 922500 | 0 | 250 | | 4 | 1300 | 2010-03-02 | 1 | 1205400 | 0 | 350 | | 5 | 1300 | 2010-03-02 | 2 | 135300 | 0 | 300 | | 6 | 1400 | 2010-03-11 | 3 | 1476000 | 0 | 200 | | 7 | 1500 | 2010-03-21 | 5 | 922500 | 0 | 250 | +----+------------+------------+----------+------------+-------+--------+ 7 rows in set (0.000 sec) MariaDB [subconsultas]> delimiter // MariaDB [subconsultas]> create trigger actualizar_existenciap after insert on detalle -> for each row -> begin -> update articulo set existencia = cantidad - (select sum(cantidad) from detalle where detalle.codigo = articulo.codigo); -> end -> // Query OK, 0 rows affected (0.074 sec) MariaDB [subconsultas]> delimiter ; MariaDB [subconsultas]> show triggers; +------------------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation | +------------------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | valorventa | INSERT | articulo | begin update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo=detalle.codigo); end | AFTER | 2025-03-08 16:23:03.27 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | | actualizar_existenciap | INSERT | detalle | begin update articulo set existencia = cantidad - (select sum(cantidad) from detalle where detalle.codigo = articulo.codigo); end | AFTER | 2025-03-08 16:34:52.02 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | +------------------------+--------+----------+-----------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 2 rows in set (0.067 sec) MariaDB [subconsultas]> insert into detalle(nrofactura,fecha,cantidad,valorventa,total,codigo) values('2001','2020-08-29',1,0,0,450); Query OK, 1 row affected (0.003 sec) MariaDB [subconsultas]> select * from detalle; +----+------------+------------+----------+------------+-------+--------+ | id | nrofactura | fecha | cantidad | valorventa | total | codigo | +----+------------+------------+----------+------------+-------+--------+ | 1 | 1200 | 2010-01-30 | 3 | 1168500 | 0 | 150 | | 2 | 1250 | 2010-02-13 | 5 | 1168500 | 0 | 150 | | 3 | 1250 | 2010-02-13 | 7 | 922500 | 0 | 250 | | 4 | 1300 | 2010-03-02 | 1 | 1205400 | 0 | 350 | | 5 | 1300 | 2010-03-02 | 2 | 135300 | 0 | 300 | | 6 | 1400 | 2010-03-11 | 3 | 1476000 | 0 | 200 | | 7 | 1500 | 2010-03-21 | 5 | 922500 | 0 | 250 | | 8 | 2001 | 2020-08-29 | 1 | 0 | 0 | 450 | +----+------------+------------+----------+------------+-------+--------+ 8 rows in set (0.000 sec) MariaDB [subconsultas]> select * from articulo; +--------+------------------------+----------+---------------+------------+ | codigo | articulo | cantidad | valorunitario | existencia | +--------+------------------------+----------+---------------+------------+ | 150 | nevera | 25 | 950000 | 17 | | 200 | televisor | 11 | 1200000 | 8 | | 250 | estufa | 30 | 750000 | 18 | | 300 | ventilador | 17 | 110000 | 15 | | 350 | lavadora | 13 | 980000 | 12 | | 450 | Secador Remington 1600 | 10 | 250000 | 9 | +--------+------------------------+----------+---------------+------------+ 6 rows in set (0.000 sec) MariaDB [subconsultas]> drop trigger valorVenta; Query OK, 0 rows affected (0.002 sec) MariaDB [subconsultas]> show triggers; +------------------------+--------+---------+-----------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | Trigger | Event | Table | Statement | Timing | Created | sql_mode | Definer | character_set_client | collation_connection | Database Collation | +------------------------+--------+---------+-----------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ | actualizar_existenciap | INSERT | detalle | begin update articulo set existencia = cantidad - (select sum(cantidad) from detalle where detalle.codigo = articulo.codigo); end | AFTER | 2025-03-08 16:34:52.02 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | +------------------------+--------+---------+-----------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 1 row in set (0.034 sec) MariaDB [subconsultas]> exit