MariaDB [(none)]> create database TiendadeAnita; Query OK, 1 row affected (0.002 sec) MariaDB [(none)]> use tiendadeanita; Database changed MariaDB [tiendadeanita]> show tables; Empty set (0.001 sec) MariaDB [tiendadeanita]> create table productos -> (codigo varchar(5) not null primary key, -> producto varchar(20) not null, -> cantidad varchar(5) not null, -> valorunitario int(10) not null, -> stock int(10) not null); Query OK, 0 rows affected (0.022 sec) MariaDB [tiendadeanita]> create table detalle -> (Id int(9) not null primary key, -> N_Factura varchar(10) not null, -> Fecha date not null, -> Cantidad int(10) not null, -> valorventa int(10) not null, -> iva int(10) not null, -> total int(10) not null, -> codigo varchar(10)not null); Query OK, 0 rows affected (0.021 sec) MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("01","LipGloss Rojo","30","4000","0"); Query OK, 1 row affected (0.013 sec) MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("01","Pesta ina MaxiLashVOLUMEN","25","10000","0"); ERROR 1062 (23000): Duplicate entry '01' for key 'PRIMARY' MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("02","Pesta ina MaxiLashVOLUMEN","25","10000","0"); Query OK, 1 row affected, 1 warning (0.006 sec) MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("03","BarraRuborROSAS","15","8000","0"); Query OK, 1 row affected (0.007 sec) MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("04","LocionCANDY_UHMM","20","20000","0"); Query OK, 1 row affected (0.006 sec) MariaDB [tiendadeanita]> insert into productos(codigo,producto,cantidad,valorunitario,stock) values ("05","Crema corporalFRUTOSAZULES","35","15500","0"); Query OK, 1 row affected, 1 warning (0.006 sec) MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 10000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | +--------+----------------------+----------+---------------+-------+ 5 rows in set (0.001 sec) MariaDB [tiendadeanita]> select*from detalle; Empty set (0.009 sec) MariaDB [tiendadeanita]> describe detalle; +------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+-------+ | Id | int(9) | NO | PRI | NULL | | | N_Factura | varchar(10) | NO | | NULL | | | Fecha | date | NO | | NULL | | | Cantidad | int(10) | NO | | NULL | | | valorventa | int(10) | NO | | NULL | | | iva | int(10) | NO | | NULL | | | total | int(10) | NO | | NULL | | | codigo | varchar(10) | NO | | NULL | | +------------+-------------+------+-----+---------+-------+ 8 rows in set (0.029 sec) MariaDB [tiendadeanita]> insert into detalle(Id,N_Factura,Fecha,valorventa,iva,total,codigo) values ("1","210","2025-07-10","0","0","0","01"); Query OK, 1 row affected, 1 warning (0.006 sec) MariaDB [tiendadeanita]> insert into detalle(Id,N_Factura,Fecha,valorventa,iva,total,codigo) values ("2","111","2025-06-18","0","0","0","02"); Query OK, 1 row affected, 1 warning (0.006 sec) MariaDB [tiendadeanita]> insert into detalle(Id,N_Factura,Fecha,valorventa,iva,total,codigo) values ("3","231","2025-07-13","0","0","0","03"); Query OK, 1 row affected, 1 warning (0.005 sec) MariaDB [tiendadeanita]> insert into detalle(Id,N_Factura,Fecha,valorventa,iva,total,codigo) values ("4","208","2025-07-10","0","0","0","01"); Query OK, 1 row affected, 1 warning (0.005 sec) MariaDB [tiendadeanita]> insert into detalle(Id,N_Factura,Fecha,valorventa,iva,total,codigo) values ("5","305","2025-08-10","0","0","0","05"); Query OK, 1 row affected, 1 warning (0.004 sec) MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-----+-------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-----+-------+--------+ | 1 | 210 | 2025-07-10 | 0 | 0 | 0 | 0 | 01 | | 2 | 111 | 2025-06-18 | 0 | 0 | 0 | 0 | 02 | | 3 | 231 | 2025-07-13 | 0 | 0 | 0 | 0 | 03 | | 4 | 208 | 2025-07-10 | 0 | 0 | 0 | 0 | 01 | | 5 | 305 | 2025-08-10 | 0 | 0 | 0 | 0 | 05 | +----+-----------+------------+----------+------------+-----+-------+--------+ 5 rows in set (0.001 sec) MariaDB [tiendadeanita]> update detalle set Cantidad='5' where Id='1'; Query OK, 1 row affected (0.008 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> update detalle set Cantidad='3' where Id='2'; Query OK, 1 row affected (0.007 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> update detalle set Cantidad='6' where Id='3'; Query OK, 1 row affected (0.007 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> update detalle set Cantidad='8' where Id='4'; Query OK, 1 row affected (0.007 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> update detalle set Cantidad='10' where Id='5'; Query OK, 1 row affected (0.009 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-----+-------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-----+-------+--------+ | 1 | 210 | 2025-07-10 | 5 | 0 | 0 | 0 | 01 | | 2 | 111 | 2025-06-18 | 3 | 0 | 0 | 0 | 02 | | 3 | 231 | 2025-07-13 | 6 | 0 | 0 | 0 | 03 | | 4 | 208 | 2025-07-10 | 8 | 0 | 0 | 0 | 01 | | 5 | 305 | 2025-08-10 | 10 | 0 | 0 | 0 | 05 | +----+-----------+------------+----------+------------+-----+-------+--------+ 5 rows in set (0.001 sec) MariaDB [tiendadeanita]> create table produc_n -> (Cod varchar(10), -> prod varchar(20), -> cant int(10), -> val_u int(10), -> sstock int(10), -> usuario varchar(20), -> fecha_modif datetime); Query OK, 0 rows affected (0.020 sec) MariaDB [tiendadeanita]> show tables; +-------------------------+ | Tables_in_tiendadeanita | +-------------------------+ | detalle | | produc_n | | productos | +-------------------------+ 3 rows in set (0.001 sec) MariaDB [tiendadeanita]> describe produc_n -> ; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | Cod | varchar(10) | YES | | NULL | | | prod | varchar(20) | YES | | NULL | | | cant | int(10) | YES | | NULL | | | val_u | int(10) | YES | | NULL | | | sstock | int(10) | YES | | NULL | | | usuario | varchar(20) | YES | | NULL | | | fecha_modif | datetime | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 7 rows in set (0.035 sec) MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 10000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | +--------+----------------------+----------+---------------+-------+ 5 rows in set (0.001 sec) MariaDB [tiendadeanita]> Delimiter // MariaDB [tiendadeanita]> CREATE TRIGGER VALORVENTA AFTER INSERT ON PRODUCTOS -> FOR EACH ROW -> BEGIN -> insert into produc_n(Cod,prod,cant,val_u,sstock,usuario,fecha_modif) values (New.codigo,New.producto,New.cantidad,New.valorunitario,New.stock,current_user(),now()); -> update detalle set valorventa = (select valorunitario + (valorunitario * 0.25)from productos where productos.codigo = detalle.codigo); -> UPDATE DETALLE -> SET iva=(valorventa*cantidad)*0.18; -> UPDATE DETALLE -> SET total = (valorventa*cantidad)+iva; -> END // Query OK, 0 rows affected (0.025 sec) MariaDB [tiendadeanita]> Delimiter ; MariaDB [tiendadeanita]> insert into productos values("06","GelFJCEJAS","35","13000","0"); Query OK, 1 row affected (0.008 sec) MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 10000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.001 sec) MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-------+--------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-------+--------+--------+ | 1 | 210 | 2025-07-10 | 5 | 5000 | 4500 | 29500 | 01 | | 2 | 111 | 2025-06-18 | 3 | 12500 | 6750 | 44250 | 02 | | 3 | 231 | 2025-07-13 | 6 | 10000 | 10800 | 70800 | 03 | | 4 | 208 | 2025-07-10 | 8 | 5000 | 7200 | 47200 | 01 | | 5 | 305 | 2025-08-10 | 10 | 19375 | 34875 | 228625 | 05 | +----+-----------+------------+----------+------------+-------+--------+--------+ 5 rows in set (0.000 sec) MariaDB [tiendadeanita]> select*from produc_n; +------+------------+------+-------+--------+----------------+---------------------+ | Cod | prod | cant | val_u | sstock | usuario | fecha_modif | +------+------------+------+-------+--------+----------------+---------------------+ | 06 | GelFJCEJAS | 35 | 13000 | 0 | root@localhost | 2025-08-29 00:50:40 | +------+------------+------+-------+--------+----------------+---------------------+ 1 row in set (0.001 sec) MariaDB [tiendadeanita]> create table productos_actuales -> (ant_codigo char(10), -> ant_product char(25), -> ant_cantd int(10), -> ant_valorunitario int(10), -> ant_stock int(10), -> nuevo_cod char(10), -> nuevo_prod char(10), -> nuevo_cant int(10), -> nuevo_valoru int(10), -> nuevo_stock int(10), -> usuario varchar(20), -> fecha_modif datetime); Query OK, 0 rows affected (0.022 sec) MariaDB [tiendadeanita]> Delimiter // MariaDB [tiendadeanita]> CREATE TRIGGER ACTUALIZA_PRODUCTOS BEFORE UPDATE ON PRODUCTOS -> FOR EACH ROW -> BEGIN -> insert into productos_actuales -> (ant_codigo,ant_product,ant_cantd,ant_valorunitario,ant_stock,nuevo_cod,nuevo_prod,nuevo_cant,nuevo_valoru,nuevo_stock,usuario,fecha_modif) -> Values -> (OLD.codigo,OLD.producto,OLD.cantidad,OLD.valorunitario,OLD.stock,New.codigo,New.producto,New.cantidad,New.valorunitario,New.stock,current_user(),now()); -> End -> // Query OK, 0 rows affected (0.010 sec) MariaDB [tiendadeanita]> update productos set valorunitario=valorunitario+5000 where codigo=02; -> // Query OK, 1 row affected (0.008 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [tiendadeanita]> Delimiter ; MariaDB [tiendadeanita]> show tables; +-------------------------+ | Tables_in_tiendadeanita | +-------------------------+ | detalle | | produc_n | | productos | | productos_actuales | +-------------------------+ 4 rows in set (0.001 sec) MariaDB [tiendadeanita]> select*from productos actuales; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.001 sec) MariaDB [tiendadeanita]> select*from productos_actuales; +------------+----------------------+-----------+-------------------+-----------+-----------+------------+------------+--------------+-------------+----------------+---------------------+ | ant_codigo | ant_product | ant_cantd | ant_valorunitario | ant_stock | nuevo_cod | nuevo_prod | nuevo_cant | nuevo_valoru | nuevo_stock | usuario | fecha_modif | +------------+----------------------+-----------+-------------------+-----------+-----------+------------+------------+--------------+-------------+----------------+---------------------+ | 02 | Pesta ina MaxiLashVO | 25 | 10000 | 0 | 02 | Pesta ina | 25 | 15000 | 0 | root@localhost | 2025-08-29 01:05:42 | +------------+----------------------+-----------+-------------------+-----------+-----------+------------+------------+--------------+-------------+----------------+---------------------+ 1 row in set (0.001 sec) MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.001 sec) MariaDB [tiendadeanita]> update productos set stock = cantidad - (select sum(cantidad) from detalle where detalle.cantidad = productos.cantidad); Query OK, 0 rows affected, 6 warnings (0.015 sec) Rows matched: 6 Changed: 0 Warnings: 6 MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 0 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 0 | | 03 | BarraRuborROSAS | 15 | 8000 | 0 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 0 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.000 sec) MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-------+--------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-------+--------+--------+ | 1 | 210 | 2025-07-10 | 5 | 5000 | 4500 | 29500 | 01 | | 2 | 111 | 2025-06-18 | 3 | 12500 | 6750 | 44250 | 02 | | 3 | 231 | 2025-07-13 | 6 | 10000 | 10800 | 70800 | 03 | | 4 | 208 | 2025-07-10 | 8 | 5000 | 7200 | 47200 | 01 | | 5 | 305 | 2025-08-10 | 10 | 19375 | 34875 | 228625 | 05 | +----+-----------+------------+----------+------------+-------+--------+--------+ 5 rows in set (0.000 sec) MariaDB [tiendadeanita]> update productos set stock = cantidad - (select sum(cantidad) from detalle where detalle.codigo = productos.codigo); Query OK, 4 rows affected, 2 warnings (0.014 sec) Rows matched: 6 Changed: 4 Warnings: 2 MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 17 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 22 | | 03 | BarraRuborROSAS | 15 | 8000 | 9 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 25 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.000 sec) MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-------+--------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-------+--------+--------+ | 1 | 210 | 2025-07-10 | 5 | 5000 | 4500 | 29500 | 01 | | 2 | 111 | 2025-06-18 | 3 | 12500 | 6750 | 44250 | 02 | | 3 | 231 | 2025-07-13 | 6 | 10000 | 10800 | 70800 | 03 | | 4 | 208 | 2025-07-10 | 8 | 5000 | 7200 | 47200 | 01 | | 5 | 305 | 2025-08-10 | 10 | 19375 | 34875 | 228625 | 05 | +----+-----------+------------+----------+------------+-------+--------+--------+ 5 rows in set (0.001 sec) MariaDB [tiendadeanita]> update detalle set cantidad='8' where codigo='04'; Query OK, 0 rows affected (0.001 sec) Rows matched: 0 Changed: 0 Warnings: 0 MariaDB [tiendadeanita]> update detalle set cantidad='3' where codigo='06'; Query OK, 0 rows affected (0.001 sec) Rows matched: 0 Changed: 0 Warnings: 0 MariaDB [tiendadeanita]> select*from detalle; +----+-----------+------------+----------+------------+-------+--------+--------+ | Id | N_Factura | Fecha | Cantidad | valorventa | iva | total | codigo | +----+-----------+------------+----------+------------+-------+--------+--------+ | 1 | 210 | 2025-07-10 | 5 | 5000 | 4500 | 29500 | 01 | | 2 | 111 | 2025-06-18 | 3 | 12500 | 6750 | 44250 | 02 | | 3 | 231 | 2025-07-13 | 6 | 10000 | 10800 | 70800 | 03 | | 4 | 208 | 2025-07-10 | 8 | 5000 | 7200 | 47200 | 01 | | 5 | 305 | 2025-08-10 | 10 | 19375 | 34875 | 228625 | 05 | +----+-----------+------------+----------+------------+-------+--------+--------+ 5 rows in set (0.000 sec) MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 17 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 22 | | 03 | BarraRuborROSAS | 15 | 8000 | 9 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 25 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.001 sec) MariaDB [tiendadeanita]> update productos set stock = cantidad - (select sum(cantidad) from detalle where detalle.codigo = productos.codigo); Query OK, 0 rows affected, 2 warnings (0.007 sec) Rows matched: 6 Changed: 0 Warnings: 2 MariaDB [tiendadeanita]> select*from productos; +--------+----------------------+----------+---------------+-------+ | codigo | producto | cantidad | valorunitario | stock | +--------+----------------------+----------+---------------+-------+ | 01 | LipGloss Rojo | 30 | 4000 | 17 | | 02 | Pesta ina MaxiLashVO | 25 | 15000 | 22 | | 03 | BarraRuborROSAS | 15 | 8000 | 9 | | 04 | LocionCANDY_UHMM | 20 | 20000 | 0 | | 05 | Crema corporalFRUTOS | 35 | 15500 | 25 | | 06 | GelFJCEJAS | 35 | 13000 | 0 | +--------+----------------------+----------+---------------+-------+ 6 rows in set (0.001 sec) MariaDB [tiendadeanita]> exit