Mysql主键的重复条目(AUTO_INCREMENT)

时间:2017-11-18 16:06:34

标签: mysql duplicates primary-key

在我的程序中,我将记录插入表tbl_name。但我得到了一个重复的主键条目'错误,这让我感到困惑,这是我的代码和错误。

表:

CREATE TABLE `tbl_name` (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(255) NOT NULL DEFAULT '' COMMENT 'name',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=514 DEFAULT CHARSET=utf8 COMMENT='table'

代码:

public Integer batchInsert() {
    List<Object[]> updateList = Arrays.asList("name1","name2");

    if (updateList.size() > 0) {
        String insertSql = "insert into tbl_name (`name`) values (?)";
        jdbcTemplate.batchUpdate(insertSql, updateList);
    }
    return updateList.size();
}

错误:

17-11-18.00:26:00.262 [pool-5-thread-1] ERROR ApprovalService  - ERROR message: {"code":1,"message":"PreparedStatementCallback; SQL []; Duplicate entry '501' for key 'tbl_name_id'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '501' for key 'tbl_name_id'","errorStackTraceMessage":null}

1 个答案:

答案 0 :(得分:0)

将主键列值增加到502.它将开始工作。