douhao3562 2012-09-13 09:59
浏览 32
已采纳

在mysql中插入单行会导致行重复

In a PHP script I perform the following:

SELECT t1.*, t2.flag_contenuto
FROM lin_98_4_sgs_formazione_elearning_allegati as t1
JOIN agews_elearning_allegati as t2 USING(id_allegato, id_elearning)
WHERE id_dipendente='432'
AND id_sgs_formazione='7'
AND id_azienda='3'
AND id_sede='12'
AND revisione_documento='0'
AND id_allegato='7'

to retrieve some specific informations I need, then I determine the current timestamp ad date('Y-m-d H:i:s') and do this:

INSERT INTO lin_98_4_sgs_formazione_elearning_statistiche (
  `id_allegato`,
  `id_elearning`,
  `id_dipendente`,
  `id_sgs_formazione`,
  `id_azienda`,
  `id_sede`,
  `revisione_documento`,
  `data_letto`
) VALUES (
  7,
  6,
  432,
  7,
  3,
  12,
  0,
  '2012-09-13 11:50:39' -- timestamp derived as described above
)
ON DUPLICATE KEY UPDATE data_letto=VALUES(data_letto)

Lastly, I perform this select and then var_dump() its output:

SELECT MAX(id_statistica)
FROM lin_98_4_sgs_formazione_elearning_statistiche
WHERE `id_allegato` = 7
AND `id_elearning` = 6
AND `id_dipendente` = 432
AND `id_sgs_formazione` = 7
AND `id_azienda` = 3
AND `id_sede` = 12
AND `revisione_documento` = 0
AND `data_letto` = '2012-09-13 11:50:39'

Which should get me the ID of the last inserted row.

I'm doing this specific query instead of using some more specific function because I noticed that sometimes the script would insert two rows insted of one, as I would expect.

In fact, the var_dump() of the result of the last select might be something like: 1, 2, 4, 5, 6, 7, 9 and so on, randomly performing a double insert sometimes.

What could be causing this issue? Here is the full table definition:

CREATE TABLE `lin_98_4_sgs_formazione_elearning_statistiche` (
  `id_statistica` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `id_allegato` int(10) unsigned DEFAULT '0',
  `id_elearning` int(10) unsigned DEFAULT '0',
  `id_dipendente` int(10) unsigned DEFAULT '0',
  `id_sgs_formazione` int(10) unsigned DEFAULT '0',
  `id_azienda` int(10) unsigned NOT NULL DEFAULT '1',
  `id_sede` int(10) unsigned NOT NULL DEFAULT '1',
  `revisione_documento` int(10) unsigned NOT NULL DEFAULT '0',
  `data_letto` datetime DEFAULT NULL,
  `tempo_letto` smallint(5) unsigned DEFAULT '0',
  PRIMARY KEY (`id_statistica`,`id_azienda`,`id_sede`,`revisione_documento`),
  UNIQUE KEY `data_letto` (`data_letto`,`id_allegato`,`id_elearning`,`id_dipendente`,`id_sgs_formazione`,`id_azienda`,`id_sede`,`revisione_documento`),
  CONSTRAINT `fk_id_allegato_lin_98_4_sgs_formazione_elearning_statistiche` FOREIGN KEY (`id_allegato`, `id_elearning`, `id_dipendente`, `id_sgs_formazione`, `id_azienda`, `id_sede`, `revisione_documento`) REFERENCES `lin_98_4_sgs_formazione_elearning_allegati` (`id_allegato`, `id_elearning`, `id_dipendente`, `id_sgs_formazione`, `id_azienda`, `id_sede`, `revisione_documento`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

I'm using MySQL 5.1.41 on Windows 7 32-bit with 3 GB RAM, if this can be of help.

  • 写回答

2条回答 默认 最新

  • donglefu6195 2012-09-13 15:20
    关注

    In the end this turned out being a MySQL bug in dealing with AUTO_INCREMENT generation when the ON DUPLICATE KEY UPDATE clause was used: even if the INSERT hits a unique key, and gets "converted" to an UPDATE, the AUTO_INCREMENT would still be incremented.

    This has already been reported: http://bugs.mysql.com/bug.php?id=66807 but the answer it that this is not a bug.

    In fact, reading this http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html points out that it all depends on your InnoDB configuration, that is, default configuration would always increment the counter even if no actual insert was performed, whereas the traditional configuration would keep the counter unchanged if there were no real inserts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog