I have a table distributor_warehouse which has an auto-incremented coulmn dpID. I cannot insert row after inserting first row. I had seen similar queries like this but everywhere got answers that the column needs to be auto-incremented which is already done in mine.
mysql> desc distributor_warehouse;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| dpID | varchar(12) | NO | PRI | | |
| cpID | varchar(12) | YES | | NULL | |
| QTY | int(6) | YES | | NULL | |
| COST_PRICE | decimal(10,2) | YES | | NULL | |
| SELL_PRICE | decimal(10,2) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> select * from distributor_warehouse;
+------+------+------+------------+------------+
| dpID | cpID | QTY | COST_PRICE | SELL_PRICE |
+------+------+------+------------+------------+
| DP1 | CP5 | 10 | 3000.00 | 3100.00 |
+------+------+------+------------+------------+
1 row in set (0.00 sec)
mysql> INSERT INTO distributor_warehouse(cpID,QTY,COST_PRICE,SELL_PRICE) VALUES ('CP6',150,999,1500);
ERROR 1062 (23000): Duplicate entry 'DP1' for key 'PRIMARY'
dpID is made auto-incremented by using a trigger.
This is my table. I had inserted Row 1 successfully then while inserting Row 2 the problem is occurring.
mysql> desc autoid;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| ID | int(10) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.00 sec)
| tg_distributor_warehouse_id | INSERT | distributor_warehouse | BEGIN
INSERT INTO autoid VALUES(NULL);
SET NEW.dpID = CONCAT('DP',LPAD(LAST_INSERT_ID(),1,''));
END |
Your code appears to be fine