MariaDB [subconsultas]> update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo = detall -> e.codigo); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'e.codigo)' at line 2 MariaDB [subconsultas]> update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo = detall -> e.codigo); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'e.codigo)' at line 2 MariaDB [subconsultas]> update detalle set valorventa = (select valorunitario + (valorunitario * 0.23) from articulo where articulo.codigo = detalle.codigo); Query OK, 7 rows affected (0.004 sec) Rows matched: 7 Changed: 7 Warnings: 0 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]> update detalle set total = cantidad * valorventa; Query OK, 7 rows affected (0.000 sec) Rows matched: 7 Changed: 7 Warnings: 0 MariaDB [subconsultas]> select * from detalle; +----+------------+------------+----------+------------+---------+--------+ | id | nrofactura | fecha | cantidad | valorventa | total | codigo | +----+------------+------------+----------+------------+---------+--------+ | 1 | 1200 | 2010-01-30 | 3 | 1168500 | 3505500 | 150 | | 2 | 1250 | 2010-02-13 | 5 | 1168500 | 5842500 | 150 | | 3 | 1250 | 2010-02-13 | 7 | 922500 | 6457500 | 250 | | 4 | 1300 | 2010-03-02 | 1 | 1205400 | 1205400 | 350 | | 5 | 1300 | 2010-03-02 | 2 | 135300 | 270600 | 300 | | 6 | 1400 | 2010-03-11 | 3 | 1476000 | 4428000 | 200 | | 7 | 1500 | 2010-03-21 | 5 | 922500 | 4612500 | 250 | +----+------------+------------+----------+------------+---------+--------+ 7 rows in set (0.000 sec) MariaDB [subconsultas]> update articulo set existencia = cantidad - (select sum(cantidad) from detalle where detalle.codigo = articulo.codigo); Query OK, 5 rows affected (0.001 sec) Rows matched: 5 Changed: 5 Warnings: 0 MariaDB [subconsultas]> select * from detalle; +----+------------+------------+----------+------------+---------+--------+ | id | nrofactura | fecha | cantidad | valorventa | total | codigo | +----+------------+------------+----------+------------+---------+--------+ | 1 | 1200 | 2010-01-30 | 3 | 1168500 | 3505500 | 150 | | 2 | 1250 | 2010-02-13 | 5 | 1168500 | 5842500 | 150 | | 3 | 1250 | 2010-02-13 | 7 | 922500 | 6457500 | 250 | | 4 | 1300 | 2010-03-02 | 1 | 1205400 | 1205400 | 350 | | 5 | 1300 | 2010-03-02 | 2 | 135300 | 270600 | 300 | | 6 | 1400 | 2010-03-11 | 3 | 1476000 | 4428000 | 200 | | 7 | 1500 | 2010-03-21 | 5 | 922500 | 4612500 | 250 | +----+------------+------------+----------+------------+---------+--------+ 7 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 | +--------+------------+----------+---------------+------------+ 5 rows in set (0.001 sec) MariaDB [subconsultas]> delete from articulo where codigo = (select codigo from detalle where cantidad between 2 and 5 and detalle.codigo = articulo.codigo group by articulo.codigo); Query OK, 4 rows affected (0.001 sec) MariaDB [subconsultas]> create table copia like detalle; Query OK, 0 rows affected (0.011 sec) MariaDB [subconsultas]> select * from articulo; +--------+----------+----------+---------------+------------+ | codigo | articulo | cantidad | valorunitario | existencia | +--------+----------+----------+---------------+------------+ | 350 | lavadora | 13 | 980000 | 12 | +--------+----------+----------+---------------+------------+ 1 row in set (0.000 sec) MariaDB [subconsultas]> drop database subconsultas; Query OK, 3 rows affected (0.023 sec) MariaDB [(none)]> source C:\Users\A01-1-0504-17\Downloads/subconsultas.sql Query OK, 0 rows affected (0.001 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 1 row affected (0.001 sec) Database changed Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.010 sec) Query OK, 0 rows affected (0.001 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 5 rows affected (0.001 sec) Records: 5 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.006 sec) Query OK, 0 rows affected (0.001 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 7 rows affected (0.000 sec) Records: 7 Duplicates: 0 Warnings: 0 Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) Query OK, 0 rows affected (0.000 sec) MariaDB [subconsultas]> select * fromr -> -> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'fromr' at line 1 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 | +--------+------------+----------+---------------+------------+ 5 rows in set (0.002 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.002 sec) 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.011 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:20:38.52 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | +------------+--------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 1 row in set (0.013 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 | +--------+------------+----------+---------------+------------+ 5 rows in set (0.003 sec) MariaDB [subconsultas]> insert into articulo values('450','Secador Remington 1600',10,250000,0); Query OK, 1 row affected (0.003 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.000 sec) MariaDB [subconsultas]> select * from detalles; ERROR 1146 (42S02): Table 'subconsultas.detalles' doesn't exist 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.028 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:20:38.52 | 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:35:07.13 | NO_ZERO_IN_DATE,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION | root@localhost | cp850 | cp850_general_ci | utf8_general_ci | +------------------------+--------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------------------+-----------------------------------------------------+----------------+----------------------+----------------------+--------------------+ 2 rows in set (0.013 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.004 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.001 sec)