Sql批量插入时如果遇到相同的数据怎么处理
测试数据
-- 创建测试表1
CREATE TABLE `testtable1` (
`Id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`UserId` INT(11) DEFAULT NULL,
`UserName` VARCHAR(10) DEFAULT NULL,
`UserType` INT(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `IX_UserId` (`UserId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
-- 创建测试表2
CREATE TABLE `testtable2` (
`Id` INT(11) NULL AUTO_INCREMENT,
`UserId` INT(11) DEFAULT NULL,
`UserName` VARCHAR(10) DEFAULT NULL,
`UserType` INT(11) DEFAULT NULL,
PRIMARY KEY (`Id`),
UNIQUE KEY `IX_UserId` (`UserId`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
-- 插入测试数据1
INSERT INTO testtable1(Id,UserId,UserName,UserType)
VALUES(1,101,'aa',1),(2,102,'bbb',2),(3,103,'ccc',3);
-- 插入测试数据2
INSERT INTO testtable2(Id,UserId,UserName,UserType)
VALUES(1,201,'aaa',1),(2,202,'bbb',2),(3,203,'ccc',3),(4,101,'xxxx',5);
可以看到上边的数据中会有userid为重复的数据 userid=101
mysql> show tables;
+---------------+
| Tables_in_dev |
+---------------+
| testtable1 |
| testtable2 |
+---------------+
mysql> select * from testtable1;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 101 | aa | 1 |
| 2 | 102 | bbb | 2 |
| 3 | 103 | ccc | 3 |
+----+--------+----------+----------+
3 rows in set (0.04 sec)
mysql> select * from testtable2;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 201 | aaa | 1 |
| 2 | 202 | bbb | 2 |
| 3 | 203 | ccc | 3 |
| 4 | 101 | xxxx | 5 |
+----+--------+----------+----------+
4 rows in set (0.04 sec)
### 当执行以下sql时,会报1062错误,提示有重复的key
mysql> insert into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2;
1062 - Duplicate entry '101' for key 'IX_UserId'
- 如果想让上边的sql执行成功的话,可以使用
IGNORE关键字
mysql> insert ignore into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2;
Query OK, 3 rows affected (0.12 sec)
Records: 4 Duplicates: 1 Warnings:1
mysql> select * from testtable1;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 1 | 101 | aa | 1 |
| 2 | 102 | bbb | 2 |
| 3 | 103 | ccc | 3 |
| 11 | 201 | aaa | 1 |
| 12 | 202 | bbb | 2 |
| 13 | 203 | ccc | 3 |
+----+--------+----------+----------+
6 rows in set (0.05 sec)
查询sql,显示testtable2表中的数据插入到了表1中(除了重复key的那条信息)
另外注意到主键id为11,12,13开始的,这个是因为之前insert的sql失败导致的自增主键不连续
导入并覆盖重复数据,REPLACE INTO
上边那个是没有插入重复key的数据
回滚之前testtable1表的数据
mysql> truncate table testtable1;
Query OK, 0 rows affected (0.62 sec) mysql> select * from testtable1;
Empty set
mysql> -- 插入测试数据1
INSERT INTO testtable1(Id,UserId,UserName,UserType)
VALUES(1,101,'aa',1),(2,102,'bbb',2),(3,103,'ccc',3);
Query OK, 3 rows affected (0.09 sec)
mysql> replace into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2;
Query OK, 5 rows affected (0.10 sec)
Records: 4 Duplicates: 1 Warnings: 0
mysql> select * from testtable1;
+----+--------+----------+----------+
| Id | UserId | UserName | UserType |
+----+--------+----------+----------+
| 2 | 102 | bbb | 2 |
| 3 | 103 | ccc | 3 |
| 4 | 201 | aaa | 1 |
| 5 | 202 | bbb | 2 |
| 6 | 203 | ccc | 3 |
| 7 | 101 | xxxx | 5 |
+----+--------+----------+----------+
可以看到表1中的101的username被覆盖为表2中的数据,这个是因为replace是现将原来表一中重复的数据删除掉,然后再执行插入新的数据
导入重复数据,保留未指定的值
mysql> insert into testtable1 (userid,username,usertype)
-> select userid,username,usertype from testtable2
-> on duplicate key update
-> testtable1.username = testtable2.username;
以上sql对于重复的数据,只是将username进行了覆盖,其他的值还是表一中的数据
Sql批量插入时如果遇到相同的数据怎么处理的更多相关文章
- SQL批量插入表类 SqlBulkInsert
ado.net已经有了sqlBulkCopy, 但是那个用xml格式,网络传输数据量太大. 自己实现了一个,传输尽量少的字节. 性能没对比过,有需要的自己拿去测试. using System.Data ...
- Delphi中SQL批量插入记录
http://www.cnblogs.com/azhqiang/p/4050331.html 在进行数据库操作时, 我们经常会遇到批量向数据库中写入记录的情况. 在这里我提供3种操作方式: 1. ...
- mybatis oracle mysql 批量插入时的坑爹问题--需谨记
mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...
- insert into select 与select into from -- sql 批量插入
参考资料:http://www.w3school.com.cn/sql/sql_union.asp UNION:操作符用于合并两个或多个select语句的结果集. ...
- 使用SQL批量插入数据到数据库 以及一些SQL函数的语法
批量插入100条记录 set nocount on declare @i int=1; while @i<=100 begin Insert into Client(id,ClientCode, ...
- sql批量插入缓慢
1.有一个普通的表t_asset,只有2个字段id,ip 没有索引 2.当用insert into t_asset(id,ip) values(?,?),(?,?) 1200多条记录时,发现竟然用了3 ...
- Sql批量插入方法
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- sql 批量插入
create PROCEDURE insertinto as begindeclare @id int;set @id=1;while @id<10begininsert into perso ...
- SQL 批量插入数据
后面进行完善修改. /*批量插入数据*/ 这个比较完善.直接插入数据库表. INSERT INTO `goods_transverter` ( `code`,`es_id`,`barcode`, `n ...
随机推荐
- hihocoder 1582 : Territorial Dispute (计算几何)(2017 北京网络赛E)
题目链接 题意:给出n个点.用两种颜色来给每个点染色.问能否存在一种染色方式,使不同颜色的点不能被划分到一条直线的两侧. 题解:求个凸包(其实只考虑四个点就行.但因为有板子,所以感觉这样写更休闲一些. ...
- css3 宽度百分比减去固定宽度 无效问题
一定要注意中间横线的间距才有效果 正确 width: calc(50% - 10px); 错误 width:calc(50%-10px);
- SQL读取表中不重复字段
通关关键字 distinct 将AlbumName字段中所以不重复的内容读出来.
- Java数据结构之排序---冒泡排序
冒泡排序的基本思想: 通过对待排序序列从前到后(从下标小的元素开始),依次比较相邻位置的元素的值,若发现与给定的次序冲突,则交换位置(假设数值大的数放在序列的后面),使数值较大的元素逐渐从前移动到后部 ...
- Java heap size
今天在性能诊断工作中遇到 Java heap size, 下面是它的相关的概念. 什么是Java heap size ? Java heap size 堆栈大小, 指Java 虚拟机的内存大小.我的理 ...
- native-echarts 组件封装
CommunalChart.js /** * 封装 图表组件 */ import React, { Component } from 'react'; import { StyleSheet, Tex ...
- ORACLE异机增量备份恢复
PROD异机增量备份恢复验证实施文档 准备工作:source 源库:PROD数据库备份策略:周日0级RMAN备份,周一至周六1级差异增量备份0 4 * * 0 /data/rmanlev0.sh &g ...
- something about motorcycle and automobile
cycle: 循环, 周期, 自行车. 摩托车: motorcycle, motor cycle 轮胎 continent(al): 大陆的, (七)大洲的; 德国的大陆轮胎, 马牌轮胎; 如吉普的c ...
- 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_14_静态static的内存图
输出room的时候,推荐用类名称点的形式 方法区内有,有个独立的空间叫做静态区,专门用来存储静态static的数据 下图红色箭头的部分,全程和对象没有关系.
- Windows Server 2008 R2 为用户“IIS APPPOOL\DefaultAppPool”授予的权限不足,无法执行此操作
报表开发与部署好后,也嵌入到aspx页面中了,使用VS自带的Web服务器组件,一切正常,当部署到IIS中的时,出现了如下错误: 为用户“IIS APPPOOL\DefaultAppPool”授予的权限 ...