Hi, I did it like this:
Product table (products):
id_prod(varchar(11), NOT NULL, PK), name(varchar(40))
Product Sequence Table (productidseq):
id(AI, PK, NOT NULL)
Before inserting a trigger into the product table:
CREATE DEFINER=`root`@`localhost` TRIGGER `dbname`.`products_BEFORE_INSERT` BEFORE INSERT ON `products` FOR EACH ROW
BEGIN
insert into productidseq (id) values(NULL);
set new.id_prod = concat('PROD or any prefix here',last_insert_id());
set @productId = new.id_prod;
END
When you run the query below:
insert into products (name) values('Bat');
the data inside the tables will be like this:
products:
id | name
---|-----
1 | Bat
productidseq:
id
---
1
If there is any better way than this or any minus with this, please comment below. Thank.
source
share