当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL批量插入和唯一索引问题的解决方法

MySQL批量插入和唯一索引问题的解决方法

2019年07月19日  | 移动技术网IT编程  | 我要评论
mysql批量插入问题 在开发项目时,因为有一些旧系统的基础数据需要提前导入,所以我在导入时做了批量导入操作 ,但是因为mysql中的一次可接受的sql语句大小受限制所以

mysql批量插入问题

在开发项目时,因为有一些旧系统的基础数据需要提前导入,所以我在导入时做了批量导入操作 ,但是因为mysql中的一次可接受的sql语句大小受限制所以我每次批量虽然只有500条,但依然无法插入,这个时候代码报错如下:

nested exception is com.mysql.jdbc.packettoobigexception: packet for query is too large (5677854 > 1048576).

you can change this value on the server by setting the max_allowed_packet' variable.

根据报错我们很快就可以知道,是sql语句数据包太大导致,我们可以设置mysql服务器参数max_allowed_packet来解决这个问题。

解决办法

1.添加【mysqld】下max_allowed_packet参数,设置的尽量大一些。

#找到my.cnf文件
#whereis my.cnf
#vim my.cnf
----------------------------
[mysqld]
max_connections =3000
max_allowed_packet=1024m

#保存后重启mysql服务,即可生效
#service mysqld restart

2.临时设置max_allowed_packet,通过语句设置

myslq>set global max_allowed_packet = 1024*1024*1024

该种方式重启后就max_allowed_packet失效了

默认情况下mysql参数max_allowed_packet值是1m.

mysql索引不区分大小写问题

当在mysql数据库中,创建索引默认是不区分大小写的,比如说如下语句:

create table `location` (
 `id` int(11) not null auto_increment,
 `rc` varchar(2) default null comment 'r/c',
 `location_code` varchar(4) default null comment '地点编码',
 `location_name` varchar(30) default null comment '地点名称',
 `zip_code` varchar(6) default null comment '邮编',
 `address` varchar(50) default null comment '地址',
 `link_man` varchar(15) default null comment '联系人',
 `link_phone` varchar(30) default null comment '联系电话',
 `fax` varchar(30) default null comment '传真',
 `can_accept_car_time` varchar(40) default null comment '可接车时间',
 `type` varchar(1) default null comment '分类',
 `maintenance_type` varchar(1) default null comment '维护类型',
 `brand` varchar(4) default null comment '品牌',
 `reservation` varchar(40) default null comment '预留',
 `enable` int(1) default '1',
 `msg_code` varchar(64) not null comment '消息编码',
 `receive_on` timestamp not null default current_timestamp comment '接收日期',
 `create_on` timestamp not null default current_timestamp comment '创建日期',
 `modified_on` datetime default current_timestamp on update current_timestamp comment '修改日期',
 primary key (`id`),
 unique key `unique_msg_code` (`msg_code`) using btree,
 unique key `unique_location_code` (`location_code`) using btree
) engine=innodb auto_increment=16325 default charset=utf8 comment='地址表';

但当我插入地址编码分别为h12c与h12c时,就报错了,抛出异常:duplicate entry 'h12c' for key 'normal_localtion_code',这里则说明不区分大小写,所以这个地方需要解决这个问题。

解决方法

1.设置字段为binary,那么索引就可以区分大小写了。

create table `location` (
 `id` int(11) not null auto_increment,
 `rc` char(2) default null comment 'r/c',
 `location_code` varchar(4) character set utf8 collate utf8_bin default null comment '地点编码',
 `location_name` varchar(26) default null comment '地点名称',
 `zip_code` varchar(6) default null comment '邮编',
 `address` varchar(50) default null comment '地址',
 `link_man` varchar(16) default null comment '联系人',
 `link_phone` varchar(30) default null comment '联系电话',
 `fax` varchar(30) default null comment '传真',
 `can_accept_car_time` varchar(40) default null comment '可接车时间',
 `type` varchar(1) default null comment '分类',
 `maintenance_type` varchar(1) default null comment '维护类型',
 `brand` varchar(4) default null comment '品牌',
 `reservation` varchar(40) default null comment '预留',
 `enable` int(1) default '1',
 `msg_code` varchar(64) not null comment '消息编码',
 `receive_on` timestamp not null default current_timestamp comment '接收日期',
 `create_on` timestamp not null default current_timestamp comment '创建日期',
 `modified_on` datetime default current_timestamp on update current_timestamp comment '修改日期',
 primary key (`id`),
 unique key `unique_msg_code` (`msg_code`) using btree,
 unique key `unique_location_code` (`location_code`) using btree
) engine=innodb auto_increment=4092 default charset=utf8 comment='地点表';

// 修改原表字典属性:

alter table `location`
change column `location_code` `location_code` varchar(4) character set 'utf8' binary not null default '' ;

上面方法就解决了。

查询时不区分大小写问题

解决方法


1.查询语句加binary

2.与索引解决方案一致,设置字段属性为binary即可。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网