基本查询【MySQL】-替换

时间:2024-11-03 06:59:23

1 row affected ,表中没有冲突数据,数据被插入

主键或者唯一键没有冲突,则直接插入

mysql> replace into Student (sn,name,qq) values (140,'许攸', '2222') ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from Student;
+----+------+-----------+---------+
| id | sn   | name      | qq      |
+----+------+-----------+---------+
|  1 |  123 | 苏雪卿    | 4567890 |
| 10 |  222 | 林游星    | 12346   |
| 11 |  333 | 林斩令    | 1254546 |
| 12 |  127 | 曹操      | 3515545 |
| 14 |  128 | 许褚      | 545445  |
| 15 |   20 | xuchu     | 8888    |
| 16 |  545 | 单沙禹    | 545     |
| 17 |   22 | 雪如之    | 888     |
| 18 |  140 | 许攸      | 2222    |
+----+------+-----------+---------+
9 rows in set (0.00 sec)

2 rows affected ,表中有冲突数据,删除后重新插入

主键或者唯一键如果冲突,则删除后再插入

mysql> select * from Student;
+----+------+-----------+---------+
| id | sn   | name      | qq      |
+----+------+-----------+---------+
|  1 |  123 | 苏雪卿    | 4567890 |
| 10 |  222 | 林游星    | 12346   |
| 11 |  333 | 林斩令    | 1254546 |
| 12 |  127 | 曹操      | 3515545 |
| 14 |  128 | 许褚      | 545445  |
| 15 |   20 | xuchu     | 8888    |
| 16 |  545 | 单沙禹    | 545     |
| 17 |   22 | 雪如之    | 888     |
| 18 |  140 | 许攸      | 2222    |
+----+------+-----------+---------+
9 rows in set (0.00 sec)

mysql> replace into Student (sn,name,qq) values (140,'许攸1', '2222') ;
Query OK, 2 rows affected (0.00 sec)

mysql> select * from Student;
+----+------+-----------+---------+
| id | sn   | name      | qq      |
+----+------+-----------+---------+
|  1 |  123 | 苏雪卿    | 4567890 |
| 10 |  222 | 林游星    | 12346   |
| 11 |  333 | 林斩令    | 1254546 |
| 12 |  127 | 曹操      | 3515545 |
| 14 |  128 | 许褚      | 545445  |
| 15 |   20 | xuchu     | 8888    |
| 16 |  545 | 单沙禹    | 545     |
| 17 |   22 | 雪如之    | 888     |
| 19 |  140 | 许攸1     | 2222    |
+----+------+-----------+---------+
9 rows in set (0.00 sec)