Batch insert with generated keys duplicates the record by unique

I am using MySql with JDBC ... below is my table definition

CREATE TABLE `A` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) NOT NULL,
  `value` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unq` (`type`(50),`value`(50))
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

      

I am currently using a statement to batch insert rows into this table using a parameter RETURN_GENERATED_KEYS

to associate an identifier with another table.

I want to be able to do this operation, but when / if DUPLICATE ENTRY (same combination of type and value) continue with the transaction as if nothing is happening, but still to get the generated keys, and if DUPLICATE ENTRY happens to the existing key.

thank

+3


source to share


1 answer


use IGNORE

keyword

INSET IGNORE ....

      

as stated in Mysql



If you use the IGNORE keyword, errors that occur during the execution of the INSERT statement are treated as warnings. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table raises a duplicate key error and the statement is aborted. With IGNORE, the row is still not inserted, but no error is thrown.

alternative syntax

INSERT ... ON UPDATING THE DUPLICATE KEY

+1


source







All Articles