0

I have the products table with id(auto_increment) and product fields.

After each INSERT into the products table, I want the product name to be prefixed with the id value

Using the instructions below, I manage to rename the "product" field, however, the id value returned is always 0.

DELIMITER //
CREATE TRIGGER prefixo_produtos
BEFORE INSERT ON products
FOR EACH ROW
BEGIN
    SET NEW.product = CONCAT(NEW.id, '_', NEW.product);
END; //
  • `NEW.id` has no value as the AI id is not assigned yet. Why would you want to do that? Prefix it dynamically when retrieving via view/select or add a [generated column](https://dev.mysql.com/doc/refman/8.0/en/create-table-generated-columns.html). – user1191247 Feb 23 '23 at 16:29

0 Answers0