MYSQL中怎么插入处理重复键值

40次阅读
没有评论

共计 2600 个字符,预计需要花费 7 分钟才能阅读完成。

MYSQL 中怎么插入处理重复键值,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

当 unique 列在一个 UNIQUE 键上插入包含重复值的记录时,默认 insert 的时候会报 1062 错误,MYSQL 有三种不同的处理方法,下面我们分别介绍。

先建立 2 个测试表,在 id 列上创建 unique 约束。
mysql create table test1(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)

mysql create table test2(id int,name varchar(5),type int,primary key(id));
Query OK, 0 rows affected (0.01 sec)

mysql select * from test1;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 101 | aaa |    1 | 
| 102 | bbb |    2 | 
| 103 | ccc |    3 | 
+—–+——+——+
3 rows in set (0.00 sec)

mysql select * from test2;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 201 | aaa |    1 | 
| 202 | bbb |    2 | 
| 203 | ccc |    3 | 
| 101 | xxx |    5 | 
+—–+——+——+
4 rows in set (0.00 sec)

1、REPLACE INTO
发现重复的先删除再插入,如果记录有多个字段,在插入的时候如果有的字段没有赋值,那么新插入的记录这些字段为空。
mysql replace into test1(id,name)(select id,name from test2);
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0

mysql select * from test1;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 101 | xxx | NULL | 
| 102 | bbb |    2 | 
| 103 | ccc |    3 | 
| 201 | aaa | NULL | 
| 202 | bbb | NULL | 
| 203 | ccc | NULL | 
+—–+——+——+
6 rows in set (0.00 sec)

需要注意的是,当你 replace 的时候,如果被插入的表如果没有指定列,会用 NULL 表示,而不是这个表原来的内容。如果插入的内容列和被插入的表列一样,则不会出现 NULL。例如
mysql replace into test1(id,name,type)(select id,name,type from test2);
Query OK, 8 rows affected (0.04 sec)
Records: 4 Duplicates: 4 Warnings: 0

mysql select * from test1;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 101 | xxx |    5 | 
| 102 | bbb |    2 | 
| 103 | ccc |    3 | 
| 201 | aaa |    1 | 
| 202 | bbb |    2 | 
| 203 | ccc |    3 | 
+—–+——+——+
6 rows in set (0.00 sec)

如果 INSERT 的时候,需要保留被插入表的列,只更新指定列,那么就可以使用第二种方法。

2、INSERT INTO ON DUPLICATE KEY UPDATE
发现重复的是更新操作。在原有记录基础上,更新指定字段内容,其它字段内容保留。例如我只想插入 test2 表的 id,name 字段,但是要保留 test1 表的 type 字段:
mysql insert into test1(id,name,type)(select id,name,type from test2) on DUPLICATE KEY UPDATE test1.name=test2.name;
Query OK, 5 rows affected (0.04 sec)
Records: 4 Duplicates: 1 Warnings: 0

mysql select * from test1;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 101 | xxx |    1 | 
| 102 | bbb |    2 | 
| 103 | ccc |    3 | 
| 203 | ccc |    3 | 
| 202 | bbb |    2 | 
| 201 | aaa |    1 | 
+—–+——+——+
6 rows in set (0.00 sec)

如果 INSERT 的时候,只想插入原表没有的数据,那么可以使用第三种方法。

3、IGNORE INTO
判断是否存在,存在不插入,否则插入。很容易理解,当插入的时候,违反唯一性约束,MySQL 不会尝试去执行这条语句。例如:
mysql insert ignore into test1(id,name,type)(select id,name,type from test2);
Query OK, 3 rows affected (0.01 sec)
Records: 4 Duplicates: 1 Warnings: 0

mysql select * from test1;
+—–+——+——+
| id | name | type |
+—–+——+——+
| 101 | aaa |    1 | 
| 102 | bbb |    2 | 
| 103 | ccc |    3 | 
| 203 | ccc |    3 | 
| 202 | bbb |    2 | 
| 201 | aaa |    1 | 
+—–+——+——+

6 rows in set (0.00 sec)

关于 MYSQL 中怎么插入处理重复键值问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注丸趣 TV 行业资讯频道了解更多相关知识。

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-04发表,共计2600字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)