4

I am using MySql with JDBC...the 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;

At the moment i am using statement in order to batch insert rows to that table with RETURN_GENERATED_KEYS option in order to relate the id to other table.

I want the ability to perform that operation but when / if DUPLICATE ENTRY occur ( the same combination of type & value ) to continue with the transaction as like nothing happens but still to get the generated keys, and if DUPLICATE ENTRY occur retrieve the existing key.

Thanks

4

1 回答 1

1

使用IGNORE关键字

INSET IGNORE ....

如引用Mysql

如果使用 IGNORE 关键字,则执行 INSERT 语句时发生的错误将被视为警告。例如,如果没有 IGNORE,复制表中现有 UNIQUE 索引或 PRIMARY KEY 值的行会导致重复键错误并且语句被中止。使用 IGNORE,行仍然没有插入,但不会发出错误。

替代语法

插入 ... 在重复密钥更新时

于 2012-04-06T12:48:59.293 回答