Pregunta

The MySQL documentation states that when using INSERT ... ON DUPLICATE KEY UPDATE, the affected-rows value will be 1 when a record was inserted, 2 if an existing record was updated.

We're getting an affected-rows value of 3 when a record is updated, although I'm only seeing this when the updates are performed via MySQL calls using Connector-J from Java; when I invoke the stored procedure from the MySQL Workbench, I get the expected result of 2 rows updated.

Does anyone have any idea what this might mean? Is this perhaps a Connector-J annomaly? I'd be inclined to just run with it, but without a reasonable explanation, I'm left with concerns for my data integrity (which is, after all, one of the reasons to check the affected-rows value).

MySQL server version: 5.1.57; Connector-J version: 5.1.7 (Java 1.6)

Additional details: This is the table being modified:

CREATE TABLE `UserContactProperty` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `UserContactId` int(11) NOT NULL,
  `Property` varchar(45) NOT NULL,
  `Value` tinytext,
  `Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `Contact-Property` (`UserContactId`,`Property`),
  KEY `FK_UserContact` (`UserContactId`),
  CONSTRAINT `FK_UserContact` FOREIGN KEY (`UserContactId`) REFERENCES `UserContact` (`id`) ON   DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=28685 DEFAULT CHARSET=latin1

The stored procedure performing the insert/update is as follows:

CREATE PROCEDURE `setUserContactProperty`(
    UID VARCHAR(50),
    CID INT,
    Prop VARCHAR(45),
    Val TINYTEXT
)
BEGIN
    INSERT INTO UserContactProperty ( UserContactId, Property, Value )
        VALUES ( CID, Prop, Val )
        ON DUPLICATE KEY UPDATE Value = Val, Date = CURRENT_TIMESTAMP;
END
¿Fue útil?

Solución

It seems as a bug. please check the following link:

http://bugs.mysql.com/bug.php?id=46675

As reported there, the bug is related to another bug where the generated key list is wrong and the count is wrong too. (the second bug originated from here http://slava-technical.blogspot.co.il/2011/05/mysql-on-duplicate-key-update-breaks.html)

I think you need to find a workaround. probably by querying the database prior to inserting to see if the row with this key exists or not. or insert and catch duplicate key exception and then make the update in that case.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top