【问题标题】:ERROR 1062 (23000): Duplicate entry 'DP1' for key 'PRIMARY'错误 1062 (23000): 键 'PRIMARY' 的重复条目 'DP1'
【发布时间】:2020-02-05 10:13:55
【问题描述】:

我有一个表distributor_warehouse,它有一个自动递增的库dpID。插入第一行后我无法插入行。我见过类似的查询,但到处都得到了列需要自动递增的答案,这已经在我的身上完成了。

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 使用触发器自动递增。

这是我的桌子。我已成功插入第 1 行,然后在插入第 2 行时出现问题。

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 |

【问题讨论】:

  • 那么我们需要查看触发器的代码。
  • @BarbarosÖzhan 给定
  • 这个触发器有什么意义?
  • 将字符串 'DP' 与 auto increment 值连接
  • 我建议不要这样做。如果您想显示DP 后跟一些自动增量值,那么在查询时执行此操作。

标签: mysql database primary-key error-code mysql-error-1062


【解决方案1】:

您的代码似乎没问题

drop table if exists t,t1;
create table t(dpid varchar(12), cpid varchar(12), qty int,cost_price decimal(10,2),sell_price decimal(10,2));
create table t1(id int auto_increment primary key);

drop trigger if exists t;
delimiter$$
create trigger t before insert on t
for each row
begin
    insert into t1 values (null);
    SET NEW.dpID = CONCAT('DP',LPAD(LAST_INSERT_ID(),1,''));
end$$
delimiter;

INSERT INTO t(cpID,QTY,COST_PRICE,SELL_PRICE) VALUES ('CP6',150,999,1500);


INSERT INTO t(cpID,QTY,COST_PRICE,SELL_PRICE) VALUES ('CP6',150,999,1500);
select * from t;

+------+------+------+------------+------------+
| dpid | cpid | qty  | cost_price | sell_price |
+------+------+------+------------+------------+
| DP1  | CP6  |  150 |     999.00 |    1500.00 |
| DP2  | CP6  |  150 |     999.00 |    1500.00 |
+------+------+------+------------+------------+
2 rows in set (0.00 sec)

【讨论】:

  • 是的,这就是我感到困惑的地方,就像这张桌子一样,我还有另一个桌子(与这张完全一样),它完全可以正常工作
  • 如果我们无法重现错误,那么我们无能为力。你需要做更多的挖掘。 - 这个错误是否发生在多插入上,autoid 上是否有任何触发器,是否针对 autoid 运行了任何其他可能恢复 id 的操作,这些插入是否在事务中,是否以某种方式在 autoid 上发生回滚...
  • 1.此错误发生在多插入上,2. 是的,autoid 上有 3 个触发器(我在帖子中给出了一个,另外 2 个与另外 2 个表相关联),3. 不理解您的问题,4 . 是的,这些是事务中的插入,5. 不明白你的问题
  • 获得错误的唯一方法是在第一次插入后将 autoid 值设置回 1 - 您将不得不仔细研究代码库以查看可能发生的位置跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多