MySql 批量插入,解决重复值问题

MySql 批量插入

使用场景:
在需要批量插入大量的数据,判断数据是否重复。

1. insert ignore into

当插入数据时,如果出现错误时,如重复数据,将不返回错误,只是以警告的形式返回。所以在使用 ignore前请确保语句本身没有问题,否则也会被忽略掉

例如:

insert ignore into user(name) values('admin');

有可能会导致不是因为重复数据报错, 而是因为其他原因报错而被忽略掉了

2. on duplicate key update

注意:当primary 或者 unique 重复时,则执行 update 语句,如update后为无用语句。如 id = id,则同1 功能相同,但错误不会被忽略掉。

例如:
为了实现 name 重复的数据插入不报错,可以使用以下语句

insert into user(name) value('admin') on duplicate key update id = id

注意:这种方法有个前提条件,就是需要约束。主键或者唯一约束,来确保当数据重复时能根据约束来更新值。

3. insert … select … where not exist

根据select 的条件判断是否插入

insert into user(name) select 'admin' from 另外一张表 where not exists (select id from user where id = 1)

注意:这里使用的是子查询(临时表的方式),效率会有影响

4. replace into

如果存在 primary or unique 相同的记录,则先删除掉,再插入记录。

replace into user select 1,'admin' fom user

注意:不管原来有没有相同的记录,都会删除掉然后再插入

附带一下笔记:
创建索引的三种方式

1. 创建表的同时创建索引

例如:

create table user(
	id int primary key,
	name varchar(50),
	phone varchar(50),
	unique index unique_index_phone(索引名) (phone(字段名)) //唯一索引
	index index_id(id) // 普通索引
)

2. 在已经建好的表中添加索引

例如:

// 普通索引
create index 索引名 on 表名(字段名);

// 唯一索引
create unique index 索引名 on 表名(字段名);

// 全文索引
create fulltext index 索引名 on 表名(字段名)

// 多列索引,复合索引
create index 索引名 on 表名(字段1,字段2...);

3. 以修改表的方式添加索引

例如:

// 普通索引
alter table 表名 add index 索引名(字段名);

// 唯一索引
alter table 表名 add unique index 索引名(字段名);

// 全文索引
alter table 表名 add fulltext index 索引名(字段名);

查看某张表的索引:

show index from 表名;

4、duplicate key

<insert id="batchInsertOrUpdateUserStage">
        INSERT INTO user_flow_stage_info(user_gid, phone, user_stage, credit_stage, register_time, create_time, update_time,channel,sub_channel)
        values
        <foreach collection="list" item="item" index="index" separator="," >
             (#{item.userGid},#{item.phone},#{item.userStage},#{item.creditStage},#{item.registerTime},#{item.createTime},#{item.updateTime},#{item.channel},#{item.subChannel})
        </foreach>
        ON DUPLICATE KEY UPDATE user_gid = VALUES(user_gid),
        phone = VALUES(phone),
        user_stage = VALUES(user_stage),
        credit_stage = VALUES(credit_stage),
        update_time = VALUES(update_time)
    </insert>

ON DUPLICATE KEY UPDATE 可以达到以下目的:

向数据库中插入一条记录:

若该数据的主键值/ UNIQUE KEY 已经在表中存在,则执行更新操作, 即UPDATE 后面的操作。

否则插入一条新的记录。

使用注意事项!!!!
使用要点:

  1. 表要求必须有主键或唯一索引才能起效果,否则insert或update无效;

  2. 注意语法on duplicate key update后面应为需要更新字段,不需要更新的字段不用罗列;

  3. 相较于replace into(insert加强版,不存在时insert,存在时先delete后insert)虽然也能达到批量更新目的,但因为删除和添加需要重复维护索引,所以大批量比on duplicate key update性能要差,小量可忽略,自选为主。

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中使用MySql进行批量插入重复数据,可以通过以下步骤实现: 1. 首先,你需要连接到MySql数据库。你可以使用MySql.Data.MySqlClient命名空间中的MySqlConnection类来建立连接。例如: ```csharp string connectionString = "your_connection_string"; using (MySqlConnection connection = new MySqlConnection(connectionString)) { connection.Open(); // 执行插入操作 } ``` 2. 接下来,你需要准备要插入的数据。你可以使用INSERT INTO语句来插入数据,同时使用ON DUPLICATE KEY UPDATE子句来处理重复数据。这样,当插入的数据已经存在时,将会更新已存在的数据。例如: ```csharp string insertQuery = "INSERT INTO your_table (column1, column2) VALUES (@value1, @value2) ON DUPLICATE KEY UPDATE column1 = @value1"; using (MySqlCommand command = new MySqlCommand(insertQuery, connection)) { // 设置参数 command.Parameters.AddWithValue("@value1", value1); command.Parameters.AddWithValue("@value2", value2); // 执行插入操作 command.ExecuteNonQuery(); } ``` 3. 最后,你可以使用循环来批量插入多条数据。例如: ```csharp List<Data> dataList = new List<Data>(); // 假设你有一个包含要插入的数据的列表 foreach (Data data in dataList) { using (MySqlCommand command = new MySqlCommand(insertQuery, connection)) { // 设置参数 command.Parameters.AddWithValue("@value1", data.Value1); command.Parameters.AddWithValue("@value2", data.Value2); // 执行插入操作 command.ExecuteNonQuery(); } } ``` 这样,你就可以使用C#和MySql实现批量插入重复数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值