How to modify a product table record from product_product table where the number of product entries is modified

0

Excuse me, someone could guide me with a problem I had when creating a table association since I created two tables (product (product_code, name, detail, existence), purchase_detail (purchase_code, product_code, detail, quantity)) and when wanting to register a purchase in detail_buy using a code_product already existing and to add the quantity in stock of the product table to it, as long as the product_code that is entered in detail_product is equal to the product table but I get this error :

Error
consulta SQL:
INSERT INTO detalle_compra values(1,2,'peces',5)
MySQL ha dicho:
#1062 - Entrada duplicada '1' para la clave 'PRIMARY'

and I'm trying to do that procedure with this code:

INSERT INTO detalle_compra values(1,2,'peces',5);
UPDATE producto SET producto.codigo_producto=detalle_compra.codigo_producto+1
WHERE producto.codigo_producto=detalle_compra.codigo_producto

and I have modified it many times to achieve it but I do not have it left maybe someone can help me, thanks in advance!

    
asked by Oryon 24.10.2018 в 00:33
source

1 answer

0

This error occurs when the primary key already has a record with the indicated value (Duplicated entry '1'), in this case you already have a record with the value 1 in the primary key, which by the order of the data of your insert I suppose it is the field code_buy.
There are 2 solutions:
- If your primary key is NOT autoincremental or identity try this: INSERT INTO detail_buy values (2,2, 'fish', 5). And every time you insert a new record you will have to change the value for the primary key.
- If your primary key is autoincremental or identity, then omit the first value like this: INSERT INTO detail_buy values (2, 'fish', 5). The value of the primary key will be generated automatically.

    
answered by 24.10.2018 в 00:59